Home / Function/ test_union_of_lists() — anthropic-sdk-python Function Reference

test_union_of_lists() — anthropic-sdk-python Function Reference

Architecture documentation for the test_union_of_lists() function in test_models.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  3c766f32_0521_63b5_9b12_e04e537c70c9["test_union_of_lists()"]
  7872e60b_4210_5600_119f_c5d08c52091e["test_models.py"]
  3c766f32_0521_63b5_9b12_e04e537c70c9 -->|defined in| 7872e60b_4210_5600_119f_c5d08c52091e
  style 3c766f32_0521_63b5_9b12_e04e537c70c9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

tests/test_models.py lines 329–358

def test_union_of_lists() -> None:
    class SubModel1(BaseModel):
        level: int

    class SubModel2(BaseModel):
        name: str

    class Model(BaseModel):
        items: Union[List[SubModel1], List[SubModel2]]

    # with one valid entry
    m = Model.construct(items=[{"name": "Robert"}])
    assert len(m.items) == 1
    assert isinstance(m.items[0], SubModel2)
    assert m.items[0].name == "Robert"

    # with two entries pointing to different types
    m = Model.construct(items=[{"level": 1}, {"name": "Robert"}])
    assert len(m.items) == 2
    assert isinstance(m.items[0], SubModel1)
    assert m.items[0].level == 1
    assert isinstance(m.items[1], SubModel1)
    assert cast(Any, m.items[1]).name == "Robert"

    # with two entries pointing to *completely* different types
    m = Model.construct(items=[{"level": -1}, 156])
    assert len(m.items) == 2
    assert isinstance(m.items[0], SubModel1)
    assert m.items[0].level == -1
    assert cast(Any, m.items[1]) == 156

Subdomains

Frequently Asked Questions

What does test_union_of_lists() do?
test_union_of_lists() is a function in the anthropic-sdk-python codebase, defined in tests/test_models.py.
Where is test_union_of_lists() defined?
test_union_of_lists() is defined in tests/test_models.py at line 329.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free