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

assert_matches_type() — anthropic-sdk-python Function Reference

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

Function python AnthropicClient SyncAPI calls 2 called by 241

Entity Profile

Dependency Diagram

graph TD
  08e9c9e7_d7f9_615f_5837_b3de69122e37["assert_matches_type()"]
  cf46c35e_ae7e_a652_f32b_5dd703f4d658["utils.py"]
  08e9c9e7_d7f9_615f_5837_b3de69122e37 -->|defined in| cf46c35e_ae7e_a652_f32b_5dd703f4d658
  68c7c786_73bd_6fa0_a552_d7c0b9059c88["test_method_create()"]
  68c7c786_73bd_6fa0_a552_d7c0b9059c88 -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  169ccc96_de26_1819_7693_61f15dd5aa11["test_method_create_with_all_params()"]
  169ccc96_de26_1819_7693_61f15dd5aa11 -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  3ab00ba6_2509_f672_486a_c793b9fd9463["test_raw_response_create()"]
  3ab00ba6_2509_f672_486a_c793b9fd9463 -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  4e7e5727_24c3_26ad_3e88_88d1db075bf8["test_streaming_response_create()"]
  4e7e5727_24c3_26ad_3e88_88d1db075bf8 -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  86f558a1_3aec_9352_3f14_ff71fc3c526b["test_method_retrieve()"]
  86f558a1_3aec_9352_3f14_ff71fc3c526b -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  17174e15_0291_ba12_7953_ba2c45fe96da["test_method_retrieve_with_all_params()"]
  17174e15_0291_ba12_7953_ba2c45fe96da -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  764f1e03_afa8_07a7_62a0_5a8e75231a96["test_raw_response_retrieve()"]
  764f1e03_afa8_07a7_62a0_5a8e75231a96 -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  d4307fc0_7f05_1164_57ca_702e3e7f9784["test_streaming_response_retrieve()"]
  d4307fc0_7f05_1164_57ca_702e3e7f9784 -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  062bb04c_c1e2_5ccd_ca4e_ed3635d411c7["test_method_list()"]
  062bb04c_c1e2_5ccd_ca4e_ed3635d411c7 -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  fdf152a7_5014_1bb8_21a2_eb80bceef2e4["test_method_list_with_all_params()"]
  fdf152a7_5014_1bb8_21a2_eb80bceef2e4 -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  07ea8ecb_5dcb_51c7_7d12_1633816c7df0["test_raw_response_list()"]
  07ea8ecb_5dcb_51c7_7d12_1633816c7df0 -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  0a8cbdda_2edd_08a0_56a6_f0570dcdf188["test_streaming_response_list()"]
  0a8cbdda_2edd_08a0_56a6_f0570dcdf188 -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  7a5c9ef1_ff31_ec51_d170_017a479be4fa["test_method_delete()"]
  7a5c9ef1_ff31_ec51_d170_017a479be4fa -->|calls| 08e9c9e7_d7f9_615f_5837_b3de69122e37
  style 08e9c9e7_d7f9_615f_5837_b3de69122e37 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

tests/utils.py lines 49–142

def assert_matches_type(
    type_: Any,
    value: object,
    *,
    path: list[str],
    allow_none: bool = False,
) -> None:
    if is_type_alias_type(type_):
        type_ = type_.__value__

    # unwrap `Annotated[T, ...]` -> `T`
    if is_annotated_type(type_):
        type_ = extract_type_arg(type_, 0)

    if allow_none and value is None:
        return

    if type_ is None or type_ is NoneType:
        assert value is None
        return

    origin = get_origin(type_) or type_

    if is_list_type(type_):
        return _assert_list_type(type_, value)

    if is_sequence_type(type_):
        assert isinstance(value, Sequence)
        inner_type = get_args(type_)[0]
        for entry in value:  # type: ignore
            assert_type(inner_type, entry)  # type: ignore
        return

    if origin == str:
        assert isinstance(value, str)
    elif origin == int:
        assert isinstance(value, int)
    elif origin == bool:
        assert isinstance(value, bool)
    elif origin == float:
        assert isinstance(value, float)
    elif origin == bytes:
        assert isinstance(value, bytes)
    elif origin == datetime:
        assert isinstance(value, datetime)
    elif origin == date:
        assert isinstance(value, date)
    elif origin == object:
        # nothing to do here, the expected type is unknown
        pass
    elif origin == Literal:
        assert value in get_args(type_)
    elif origin == dict:
        assert is_dict(value)

        args = get_args(type_)
        key_type = args[0]
        items_type = args[1]

        for key, item in value.items():
            assert_matches_type(key_type, key, path=[*path, "<dict key>"])
            assert_matches_type(items_type, item, path=[*path, "<dict item>"])
    elif is_union_type(type_):
        variants = get_args(type_)

        try:
            none_index = variants.index(type(None))
        except ValueError:
            pass
        else:
            # special case Optional[T] for better error messages
            if len(variants) == 2:
                if value is None:
                    # valid
                    return

                return assert_matches_type(type_=variants[not none_index], value=value, path=path)

        for i, variant in enumerate(variants):
            try:
                assert_matches_type(variant, value, path=[*path, f"variant {i}"])

Subdomains

Defined In

Called By

Frequently Asked Questions

What does assert_matches_type() do?
assert_matches_type() is a function in the anthropic-sdk-python codebase, defined in tests/utils.py.
Where is assert_matches_type() defined?
assert_matches_type() is defined in tests/utils.py at line 49.
What does assert_matches_type() call?
assert_matches_type() calls 2 function(s): _assert_list_type, assert_matches_model.
What calls assert_matches_type()?
assert_matches_type() is called by 241 function(s): assert_matches_model, test_method_cancel, test_method_cancel, test_method_cancel, test_method_cancel, test_method_cancel_with_all_params, test_method_cancel_with_all_params, test_method_count_tokens, and 233 more.

Analyze Your Own Codebase

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

Try Supermodel Free