Skip to content

introspection

Class info

🛈 DocStrings

Annotation introspection that survives lazily evaluated annotations (PEP 649).

Since Python 3.14 annotations are evaluated on access. Names that a module imports only under if TYPE_CHECKING: then raise NameError when a signature or the type hints of a user callable are read. The helpers here resolve whatever can be resolved and let only the unresolvable parts degrade to Any (list[Missing] becomes list[Any]).

get_resolved_hints

get_resolved_hints(obj: Any, *, include_extras: bool = True) -> dict[str, Any]

Evaluated type hints of a function or class.

Like typing.get_type_hints, but names that can't be resolved (e.g. imports under TYPE_CHECKING) don't spoil the others: only the unresolvable parts become Any.

Source code in src/schemez/introspection.py
def get_resolved_hints(obj: Any, *, include_extras: bool = True) -> dict[str, Any]:
    """Evaluated type hints of a function or class.

    Like `typing.get_type_hints`, but names that can't be resolved (e.g. imports under
    `TYPE_CHECKING`) don't spoil the others: only the unresolvable parts become `Any`.
    """
    try:
        return typing.get_type_hints(obj, include_extras=include_extras)
    except Exception:  # noqa: BLE001
        try:
            hints = typing.get_type_hints(obj, include_extras=include_extras, format=_FORWARDREF)
        except Exception:  # noqa: BLE001
            return {}
    return {name: resolve_annotation(ann, obj) for name, ann in hints.items()}

get_signature

get_signature(func: Any) -> Signature

inspect.signature, with unresolvable annotation names degraded to Any.

Evaluates the annotations normally first, so nothing changes for callables whose annotations resolve. Errors unrelated to annotations (e.g. ValueError for callables without a signature) are raised as usual.

Source code in src/schemez/introspection.py
def get_signature(func: Any) -> inspect.Signature:
    """`inspect.signature`, with unresolvable annotation names degraded to `Any`.

    Evaluates the annotations normally first, so nothing changes for callables whose
    annotations resolve. Errors unrelated to annotations (e.g. `ValueError` for
    callables without a signature) are raised as usual.
    """
    try:
        return inspect.signature(func)
    except Exception as e:  # noqa: BLE001  (re-raised below if not annotation-related)
        try:
            sig = inspect.signature(func, annotation_format=_FORWARDREF)
        except Exception:  # noqa: BLE001
            raise e from None
    params = [
        p.replace(annotation=resolve_annotation(p.annotation, func))
        if p.annotation is not inspect.Parameter.empty
        else p
        for p in sig.parameters.values()
    ]
    ret = sig.return_annotation
    if ret is not inspect.Signature.empty:
        ret = resolve_annotation(ret, func)
    # no re-validation: a default of `Parameter.empty` looks like "no default" to it
    return type(sig)(params, return_annotation=ret, __validate_parameters__=False)

resolve_annotation

resolve_annotation(annotation: Any, owner: Any = None) -> Any

Resolve forward references inside an annotation, degrading missing names to Any.

Parameters:

Name Type Description Default
annotation Any

An annotation as returned in Format.FORWARDREF (may contain ForwardRef objects, also nested, e.g. list[ForwardRef("X")]).

required
owner Any

The object the annotation belongs to (function or class), used for the namespace when the forward reference doesn't carry its own owner.

None
Source code in src/schemez/introspection.py
def resolve_annotation(annotation: Any, owner: Any = None) -> Any:
    """Resolve forward references inside an annotation, degrading missing names to `Any`.

    Args:
        annotation: An annotation as returned in `Format.FORWARDREF` (may contain
            `ForwardRef` objects, also nested, e.g. `list[ForwardRef("X")]`).
        owner: The object the annotation belongs to (function or class), used for the
            namespace when the forward reference doesn't carry its own owner.
    """
    if not _contains_forward_ref(annotation):
        return annotation
    try:
        resolved = _substitute(annotation, owner)
    except Exception:  # noqa: BLE001
        resolved = Any
    if _contains_forward_ref(resolved):
        resolved = Any
    logger.debug(
        "Annotation %r of %r has names not available at runtime, using %r",
        annotation,
        owner,
        resolved,
    )
    return resolved