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

extract_type_var_from_base() — anthropic-sdk-python Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  095835a6_9442_f7fd_5ffe_1aaab78cedc3["extract_type_var_from_base()"]
  b104416a_b868_41fc_902c_7e2aaf93e2af["_typing.py"]
  095835a6_9442_f7fd_5ffe_1aaab78cedc3 -->|defined in| b104416a_b868_41fc_902c_7e2aaf93e2af
  17854057_420d_9158_b387_597d6c79873e["get_origin()"]
  095835a6_9442_f7fd_5ffe_1aaab78cedc3 -->|calls| 17854057_420d_9158_b387_597d6c79873e
  d2031788_5491_eb7c_0e85_dbabc0a1a067["extract_type_arg()"]
  095835a6_9442_f7fd_5ffe_1aaab78cedc3 -->|calls| d2031788_5491_eb7c_0e85_dbabc0a1a067
  6cc2e1cc_b0c9_ac0f_711b_dc5e92ef8f1b["is_typevar()"]
  095835a6_9442_f7fd_5ffe_1aaab78cedc3 -->|calls| 6cc2e1cc_b0c9_ac0f_711b_dc5e92ef8f1b
  style 095835a6_9442_f7fd_5ffe_1aaab78cedc3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/anthropic/_utils/_typing.py lines 91–156

def extract_type_var_from_base(
    typ: type,
    *,
    generic_bases: tuple[type, ...],
    index: int,
    failure_message: str | None = None,
) -> type:
    """Given a type like `Foo[T]`, returns the generic type variable `T`.

    This also handles the case where a concrete subclass is given, e.g.
    ```py
    class MyResponse(Foo[bytes]):
        ...

    extract_type_var(MyResponse, bases=(Foo,), index=0) -> bytes
    ```

    And where a generic subclass is given:
    ```py
    _T = TypeVar('_T')
    class MyResponse(Foo[_T]):
        ...

    extract_type_var(MyResponse[bytes], bases=(Foo,), index=0) -> bytes
    ```
    """
    cls = cast(object, get_origin(typ) or typ)
    if cls in generic_bases:  # pyright: ignore[reportUnnecessaryContains]
        # we're given the class directly
        return extract_type_arg(typ, index)

    # if a subclass is given
    # ---
    # this is needed as __orig_bases__ is not present in the typeshed stubs
    # because it is intended to be for internal use only, however there does
    # not seem to be a way to resolve generic TypeVars for inherited subclasses
    # without using it.
    if isinstance(cls, InheritsGeneric):
        target_base_class: Any | None = None
        for base in cls.__orig_bases__:
            if base.__origin__ in generic_bases:
                target_base_class = base
                break

        if target_base_class is None:
            raise RuntimeError(
                "Could not find the generic base class;\n"
                "This should never happen;\n"
                f"Does {cls} inherit from one of {generic_bases} ?"
            )

        extracted = extract_type_arg(target_base_class, index)
        if is_typevar(extracted):
            # If the extracted type argument is itself a type variable
            # then that means the subclass itself is generic, so we have
            # to resolve the type argument from the class itself, not
            # the base class.
            #
            # Note: if there is more than 1 type argument, the subclass could
            # change the ordering of the type arguments, this is not currently
            # supported.
            return extract_type_arg(typ, index)

        return extracted

    raise RuntimeError(failure_message or f"Could not resolve inner type variable at index {index} for {typ}")

Subdomains

Frequently Asked Questions

What does extract_type_var_from_base() do?
extract_type_var_from_base() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_utils/_typing.py.
Where is extract_type_var_from_base() defined?
extract_type_var_from_base() is defined in src/anthropic/_utils/_typing.py at line 91.
What does extract_type_var_from_base() call?
extract_type_var_from_base() calls 3 function(s): extract_type_arg, get_origin, is_typevar.

Analyze Your Own Codebase

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

Try Supermodel Free