API reference¶
Complete reference for all public classes and functions in the typing-graph library. This reference is auto-generated from source docstrings using mkdocstrings.
Contents¶
- Core inspection functions - Entry points for type inspection
- Traversal - Graph traversal function
- Configuration - Classes for controlling inspection behavior
- Namespace extraction - Functions for extracting namespaces from objects
- Type nodes - Base classes and node types for representing types
- Structured type nodes - Classes, dataclasses, TypedDicts, and similar
- Function and callable nodes - Functions and signatures
- Helper types - Supporting types for fields, parameters, and locations
- Metadata collection - Working with type metadata
- Edge types - Edge types for graph relationships
- Exceptions - Base, metadata, and traversal exceptions
- Type guards - Type narrowing functions
- Utility functions - Helpers for unions and optional types
- Cache management - Managing the inspection cache
Core inspection functions¶
Entry points for inspecting type annotations, classes, functions, modules, and type parameters.
Functions:
| Name | Description |
|---|---|
inspect_type |
Inspect any type annotation and return the corresponding TypeNode. |
inspect_class |
Inspect a class and return the appropriate TypeNode. |
inspect_dataclass |
Inspect a dataclass specifically. |
inspect_enum |
Inspect an Enum specifically. |
inspect_named_tuple |
Inspect a NamedTuple specifically. |
inspect_protocol |
Inspect a Protocol specifically. |
inspect_typed_dict |
Inspect a TypedDict specifically. |
inspect_function |
Inspect a function and return a FunctionNode. |
inspect_signature |
Inspect a callable's signature. |
inspect_module |
Inspect all public types in a module. |
inspect_type_alias |
Inspect a type alias. |
inspect_type_param |
Inspect a type parameter. |
typing_graph.inspect_type ¶
inspect_type(
annotation: Any,
*,
config: InspectConfig | None = None,
use_cache: bool = True,
source: type | Callable[..., Any] | ModuleType | None = None
) -> TypeNode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
|
Any
|
Any valid type annotation. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. |
None
|
use_cache
|
bool
|
Whether to use the global cache (default True). Note: Cache is only used when config is None or DEFAULT_CONFIG. |
True
|
source
|
type | Callable[..., Any] | ModuleType | None
|
Optional context object for namespace auto-detection. Can be a class, function, or module. When provided and config.auto_namespace is True, namespaces will be extracted from this object for forward reference resolution. When source is provided, the global cache is bypassed regardless of use_cache setting. |
None
|
Returns:
| Type | Description |
|---|---|
TypeNode
|
A TypeNode representing the annotation's structure. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If source is not None and is not a class, callable, or module. |
Source code in src/typing_graph/_inspect_type.py
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |
typing_graph.inspect_class ¶
inspect_class(cls: type, *, config: InspectConfig | None = None) -> ClassInspectResult
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to inspect. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. When config.auto_namespace is True (default), namespaces are automatically extracted from the class for forward reference resolution. User-provided globalns/localns take precedence. |
None
|
Returns:
| Type | Description |
|---|---|
ClassInspectResult
|
A specialized TypeNode based on the class type. |
Source code in src/typing_graph/_inspect_class.py
typing_graph.inspect_dataclass ¶
inspect_dataclass(cls: type, *, config: InspectConfig | None = None) -> DataclassNode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The dataclass to inspect. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. When config.auto_namespace is True (default), namespaces are automatically extracted from the dataclass for forward reference resolution. User-provided globalns/localns take precedence. |
None
|
Returns:
| Type | Description |
|---|---|
DataclassNode
|
A DataclassNode node representing the dataclass. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If cls is not a dataclass. |
Source code in src/typing_graph/_inspect_class.py
typing_graph.inspect_enum ¶
inspect_enum(cls: type, *, config: InspectConfig | None = None) -> EnumNode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The Enum to inspect. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. When config.auto_namespace is True (default), namespaces are automatically extracted from the Enum for forward reference resolution. User-provided globalns/localns take precedence. |
None
|
Returns:
| Type | Description |
|---|---|
EnumNode
|
An EnumNode node representing the Enum. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If cls is not an Enum. |
Source code in src/typing_graph/_inspect_class.py
typing_graph.inspect_named_tuple ¶
inspect_named_tuple(
cls: type, *, config: InspectConfig | None = None
) -> NamedTupleNode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The NamedTuple to inspect. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. When config.auto_namespace is True (default), namespaces are automatically extracted from the NamedTuple for forward reference resolution. User-provided globalns/localns take precedence. |
None
|
Returns:
| Type | Description |
|---|---|
NamedTupleNode
|
A NamedTupleNode node representing the NamedTuple. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If cls is not a NamedTuple. |
Source code in src/typing_graph/_inspect_class.py
typing_graph.inspect_protocol ¶
inspect_protocol(cls: type, *, config: InspectConfig | None = None) -> ProtocolNode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The Protocol to inspect. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. When config.auto_namespace is True (default), namespaces are automatically extracted from the Protocol for forward reference resolution. User-provided globalns/localns take precedence. |
None
|
Returns:
| Type | Description |
|---|---|
ProtocolNode
|
A ProtocolNode node representing the Protocol. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If cls is not a Protocol. |
Source code in src/typing_graph/_inspect_class.py
typing_graph.inspect_typed_dict ¶
inspect_typed_dict(cls: type, *, config: InspectConfig | None = None) -> TypedDictNode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The TypedDict to inspect. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. When config.auto_namespace is True (default), namespaces are automatically extracted from the TypedDict for forward reference resolution. User-provided globalns/localns take precedence. |
None
|
Returns:
| Type | Description |
|---|---|
TypedDictNode
|
A TypedDictNode node representing the TypedDict. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If cls is not a TypedDict. |
Source code in src/typing_graph/_inspect_class.py
typing_graph.inspect_function ¶
inspect_function(
func: Callable[..., Any], *, config: InspectConfig | None = None
) -> FunctionNode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The function to inspect. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. |
None
|
Returns:
| Type | Description |
|---|---|
FunctionNode
|
A FunctionNode representing the function's structure. |
Source code in src/typing_graph/_inspect_function.py
typing_graph.inspect_signature ¶
inspect_signature(
callable_obj: Callable[..., Any],
*,
config: InspectConfig | None = None,
follow_wrapped: bool = True
) -> SignatureNode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callable_obj
|
Callable[..., Any]
|
The callable to inspect. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. |
None
|
follow_wrapped
|
bool
|
Whether to unwrap decorated functions (default True). |
True
|
Returns:
| Type | Description |
|---|---|
SignatureNode
|
A SignatureNode representing the callable's signature. |
Source code in src/typing_graph/_inspect_function.py
typing_graph.inspect_module ¶
inspect_module(
module: ModuleType,
*,
config: InspectConfig | None = None,
include_imported: bool = False
) -> ModuleTypes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
ModuleType
|
The module to inspect. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. |
None
|
include_imported
|
bool
|
Whether to include items imported from other modules (default False). |
False
|
Returns:
| Type | Description |
|---|---|
ModuleTypes
|
A ModuleTypes containing all discovered types organized by category. |
Source code in src/typing_graph/_inspect_module.py
typing_graph.inspect_type_alias ¶
inspect_type_alias(
alias: Any, *, name: str | None = None, config: InspectConfig | None = None
) -> GenericAliasNode | TypeAliasNode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alias
|
Any
|
The type alias to inspect. |
required |
name
|
str | None
|
Optional name for the alias (used for simple aliases). |
None
|
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. |
None
|
Returns:
| Type | Description |
|---|---|
GenericAliasNode | TypeAliasNode
|
A GenericAliasNode for PEP 695 TypeAliasType, or TypeAliasNode for |
GenericAliasNode | TypeAliasNode
|
simple type aliases. |
Source code in src/typing_graph/_inspect_type.py
typing_graph.inspect_type_param ¶
inspect_type_param(
param: TypeVar, *, config: InspectConfig | None = None
) -> TypeVarNode
inspect_type_param(
param: ParamSpec, *, config: InspectConfig | None = None
) -> ParamSpecNode
inspect_type_param(
param: TypeVarTuple, *, config: InspectConfig | None = None
) -> TypeVarTupleNode
inspect_type_param(
param: TypeVar | ParamSpec | TypeVarTuple, *, config: InspectConfig | None = None
) -> TypeVarNode | ParamSpecNode | TypeVarTupleNode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param
|
TypeVar | ParamSpec | TypeVarTuple
|
A TypeVar, ParamSpec, or TypeVarTuple to inspect. |
required |
config
|
InspectConfig | None
|
Introspection configuration. Uses defaults if None. |
None
|
Returns:
| Type | Description |
|---|---|
TypeVarNode | ParamSpecNode | TypeVarTupleNode
|
The corresponding TypeParamNode (TypeVarNode, ParamSpecNode, |
TypeVarNode | ParamSpecNode | TypeVarTupleNode
|
or TypeVarTupleNode). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If param is not a known type parameter type. |
Source code in src/typing_graph/_inspect_type.py
Traversal¶
The primary traversal function for depth-first iteration over type graphs.
Functions:
| Name | Description |
|---|---|
walk |
Traverse the type graph depth-first, yielding unique nodes. |
typing_graph.walk ¶
walk(
node: TypeNode,
*,
predicate: Callable[[TypeNode], bool] | None = None,
max_depth: int | None = None
) -> Iterator[TypeNode]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TypeNode
|
The root node to start traversal from. |
required |
predicate
|
Callable[[TypeNode], bool] | None
|
Optional filter function. If provided, only nodes for which predicate(node) returns True are yielded. When using a TypeIs predicate, the return type is narrowed accordingly. |
None
|
max_depth
|
int | None
|
Maximum traversal depth. If None, no limit is imposed. When depth exceeds this limit, traversal stops descending into that branch but continues with other branches. Depth 0 yields only the root node. |
None
|
Yields:
| Type | Description |
|---|---|
TypeNode
|
TypeNode instances matching the predicate (or all nodes if no predicate). |
Raises:
| Type | Description |
|---|---|
TraversalError
|
If max_depth is negative. |
Examples:
Basic traversal of all nodes:
>>> from typing_graph import inspect_type
>>> from typing_graph import walk
>>> node = inspect_type(list[int])
>>> len(list(walk(node)))
3
Filtered traversal with type narrowing:
>>> from typing_graph import inspect_type, walk, ConcreteNode, TypeNode
>>> from typing_extensions import TypeIs
>>> def is_concrete(n: TypeNode) -> TypeIs[ConcreteNode]:
... return isinstance(n, ConcreteNode)
>>> node = inspect_type(dict[str, int])
>>> concrete_nodes = list(walk(node, predicate=is_concrete))
>>> all(isinstance(n, ConcreteNode) for n in concrete_nodes)
True
Depth-limited traversal:
>>> from typing_graph import inspect_type, walk
>>> node = inspect_type(list[dict[str, int]])
>>> # Depth 0 yields only the root
>>> len(list(walk(node, max_depth=0)))
1
Source code in src/typing_graph/_walk.py
Configuration¶
Classes for controlling inspection behavior.
Classes:
| Name | Description |
|---|---|
InspectConfig |
Configuration for type introspection. |
EvalMode |
How to evaluate annotations during introspection. |
typing_graph.InspectConfig
dataclass
¶
Attributes:
| Name | Type | Description |
|---|---|---|
eval_mode |
EvalMode
|
How to evaluate annotations (default: DEFERRED). |
globalns |
dict[str, Any] | None
|
Global namespace for forward reference resolution. |
localns |
dict[str, Any] | None
|
Local namespace for forward reference resolution. |
auto_namespace |
bool
|
Automatically extract namespaces from source objects (classes, functions, modules) for forward reference resolution. When True, namespaces are extracted from the inspected object and merged with user-provided globalns/localns (user values take precedence). Disable by setting to False for explicit namespace control only. Default: True. |
max_depth |
int | None
|
Maximum recursion depth (None = unlimited). |
include_private |
bool
|
Include private members starting with underscore. |
include_inherited |
bool
|
Include inherited members from base classes. |
include_methods |
bool
|
Include methods in class inspection. |
include_class_vars |
bool
|
Include ClassVar annotations. |
include_instance_vars |
bool
|
Include instance variable annotations. |
hoist_metadata |
bool
|
Move Annotated metadata to node.metadata. |
normalize_unions |
bool
|
Represent all union types as UnionNode regardless
of runtime form. When True (default), both types.UnionType and
typing.Union produce UnionNode, matching Python 3.14 behavior.
Set to False to preserve native runtime representation. Use
:func: |
include_source_locations |
bool
|
Track source file and line numbers (disabled by default as inspect.getsourcelines is expensive). |
Methods:
| Name | Description |
|---|---|
get_format |
Map eval_mode to typing_extensions.Format. |
Source code in src/typing_graph/_config.py
typing_graph.EvalMode ¶
Bases: Enum
Attributes:
| Name | Type | Description |
|---|---|---|
EAGER |
Fully resolve annotations, fail on errors. |
|
DEFERRED |
Use ForwardRef for unresolvable annotations (default). |
|
STRINGIFIED |
Keep annotations as strings, resolve lazily. |
Source code in src/typing_graph/_config.py
Namespace extraction¶
Functions for extracting global and local namespaces from Python objects. These functions support forward reference resolution by providing namespace context from classes, functions, and modules.
For practical usage guidance, see How to configure namespaces. For background on why namespace extraction matters, see Forward references.
Type aliases¶
Attributes:
| Name | Type | Description |
|---|---|---|
NamespacePair |
TypeAlias
|
Type alias for the (globalns, localns) tuple returned by extraction functions. |
NamespaceSource |
TypeAlias
|
Type alias for valid namespace extraction sources (class, function, or module). |
Extraction functions¶
Functions:
| Name | Description |
|---|---|
extract_namespace |
Extract namespaces from any valid source object. |
extract_class_namespace |
Extract global and local namespaces from a class. |
extract_function_namespace |
Extract global and local namespaces from a function. |
extract_module_namespace |
Extract global and local namespaces from a module. |
typing_graph.extract_namespace ¶
extract_namespace(source: NamespaceSource) -> NamespacePair
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
NamespaceSource
|
A class, function, or module. |
required |
Returns:
| Type | Description |
|---|---|
NamespacePair
|
A tuple of (globalns, localns) dicts. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If source is not a class, callable, or module. |
Source code in src/typing_graph/_namespace.py
typing_graph.extract_class_namespace ¶
extract_class_namespace(cls: type[Any]) -> NamespacePair
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[Any]
|
The class to extract namespaces from. |
required |
Returns:
| Type | Description |
|---|---|
NamespacePair
|
A tuple of (globalns, localns) dicts. The globalns is a copy of the |
NamespacePair
|
module's namespace; the localns is a new dict containing the class |
NamespacePair
|
and any type parameters. |
Source code in src/typing_graph/_namespace.py
typing_graph.extract_function_namespace ¶
extract_function_namespace(func: Callable[..., Any]) -> NamespacePair
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The function to extract namespaces from. |
required |
Returns:
| Type | Description |
|---|---|
NamespacePair
|
A tuple of (globalns, localns) dicts. The globalns is a copy of the |
NamespacePair
|
function's globals; the localns is a new dict containing the owning |
NamespacePair
|
class (if a method) and any type parameters. |
Source code in src/typing_graph/_namespace.py
typing_graph.extract_module_namespace ¶
extract_module_namespace(module: ModuleType) -> NamespacePair
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
ModuleType
|
The module to extract namespaces from. |
required |
Returns:
| Type | Description |
|---|---|
NamespacePair
|
A tuple of (globalns, localns) dicts. The globalns is a copy of the |
NamespacePair
|
module's namespace; the localns is always an empty dict for modules. |
Source code in src/typing_graph/_namespace.py
Type nodes¶
Base classes¶
The foundational type node class and type variable.
Classes:
| Name | Description |
|---|---|
TypeNode |
Base class for all type graph nodes. |
Attributes:
| Name | Type | Description |
|---|---|---|
TypeNodeT |
TypeVar bound to TypeNode for generic functions over node types. |
typing_graph.TypeNode
dataclass
¶
Bases: ABC
Attributes:
| Name | Type | Description |
|---|---|---|
source |
SourceLocation | None
|
Optional source location where this type was defined. |
metadata |
MetadataCollection
|
Metadata extracted from an enclosing Annotated[T, ...]. When a type is wrapped in Annotated, the metadata can be hoisted to this field during graph construction, allowing the Annotated wrapper to be elided while preserving the metadata. |
qualifiers |
frozenset[Qualifier]
|
Type qualifiers (ClassVar, Final, Required, NotRequired, ReadOnly, InitVar) extracted from the annotation. Uses the same qualifier type as typing_inspection. |
Methods:
| Name | Description |
|---|---|
children |
Return child type nodes for graph traversal. |
edges |
Return all outgoing edges from this node. |
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
children
abstractmethod
¶
edges
abstractmethod
¶
edges() -> Sequence[TypeEdgeConnection]
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
Concrete and generic types¶
Nodes representing concrete types, generics, and annotated types.
Classes:
| Name | Description |
|---|---|
ConcreteNode |
A non-generic nominal type: int, str, MyClass, etc. |
SubscriptedGenericNode |
Generic with type args applied: List[int], Dict[str, T], etc. |
GenericAliasNode |
Parameterized type alias: type Vector[T] = list[T] (PEP 695). |
GenericTypeNode |
An unsubscripted generic (type constructor): list, Dict, etc. |
AnnotatedNode |
Annotated[T, metadata, ...]. |
NewTypeNode |
NewType('Name', base) - a distinct type alias for type checking. |
TypeAliasNode |
typing.TypeAlias or PEP 695 type statement runtime object. |
typing_graph.ConcreteNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.SubscriptedGenericNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.GenericAliasNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.GenericTypeNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.AnnotatedNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.NewTypeNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.TypeAliasNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
Union and intersection types¶
Nodes representing type unions and intersections.
Classes:
| Name | Description |
|---|---|
UnionNode |
A | B union type. |
IntersectionNode |
Intersection of types (not yet in typing, but used by type checkers). |
typing_graph.UnionNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.IntersectionNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
Type variables¶
Nodes representing type parameters and variance.
Classes:
| Name | Description |
|---|---|
TypeVarNode |
A TypeVar - placeholder for a single type. |
ParamSpecNode |
A ParamSpec - placeholder for callable parameter lists. |
TypeVarTupleNode |
A TypeVarTuple - placeholder for variadic type args (PEP 646). |
Variance |
Variance of a type variable. |
Attributes:
| Name | Type | Description |
|---|---|---|
TypeParamNode |
Type alias for nodes representing type parameters. |
typing_graph.TypeParamNode
module-attribute
¶
TypeParamNode = TypeVarNode | ParamSpecNode | TypeVarTupleNode
typing_graph.TypeVarNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.ParamSpecNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.TypeVarTupleNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
Special types¶
Nodes for special typing constructs.
Classes:
| Name | Description |
|---|---|
AnyNode |
typing.Any - compatible with all types (gradual typing escape hatch). |
NeverNode |
typing.Never / typing.NoReturn - the bottom type (uninhabited). |
SelfNode |
typing.Self - reference to the enclosing class. |
LiteralNode |
Literal[v1, v2, ...] - specific literal values as types. |
LiteralStringNode |
typing.LiteralString - any literal string value (PEP 675). |
TupleNode |
Tuple types in various forms. |
EllipsisNode |
The ... used in Callable[..., R] and Tuple[T, ...]. |
ForwardRefNode |
A string forward reference like 'MyClass'. |
MetaNode |
Type[T] or type[T] - the class object itself, not an instance. |
ConcatenateNode |
Concatenate[X, Y, P] - prepend args to a ParamSpec (PEP 612). |
UnpackNode |
Unpack[Ts] or *Ts - unpack a TypeVarTuple (PEP 646). |
TypeGuardNode |
typing.TypeGuard[T] - narrows type in true branch (PEP 647). |
TypeIsNode |
typing.TypeIs[T] - narrows type bidirectionally (PEP 742). |
typing_graph.AnyNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.NeverNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.SelfNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.LiteralNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.LiteralStringNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.TupleNode
dataclass
¶
Bases: TypeNode
Examples:
tuple[int, str] - heterogeneous (elements=(int, str), homogeneous=False) tuple[int, ...] - homogeneous (elements=(int,), homogeneous=True) tuple[int, *Ts, str] - variadic (contains UnpackNode) tuple[()] - empty tuple (elements=(), homogeneous=False)
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.EllipsisNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.ForwardRefNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 | |
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if unresolvable. |
Source code in src/typing_graph/_node.py
typing_graph.MetaNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.ConcatenateNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.UnpackNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.TypeGuardNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.TypeIsNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
Forward reference state¶
Types representing forward reference resolution states.
Classes:
| Name | Description |
|---|---|
RefResolved |
Successfully resolved to a type. |
RefUnresolved |
Not yet attempted to resolve. |
RefFailed |
Resolution attempted but failed. |
Attributes:
| Name | Type | Description |
|---|---|---|
RefState |
Type alias for forward reference resolution states. |
Structured type nodes¶
Nodes representing classes, dataclasses, TypedDicts, and other structured types.
Classes:
| Name | Description |
|---|---|
StructuredNode |
Base for types with named, typed fields. |
ClassNode |
A class with full type information. |
DataclassNode |
A dataclass with typed fields and configuration. |
TypedDictNode |
TypedDict with named fields. |
NamedTupleNode |
NamedTuple with named fields. |
EnumNode |
An Enum with typed members. |
ProtocolNode |
Protocol defining structural interface. |
typing_graph.StructuredNode
dataclass
¶
Methods:
| Name | Description |
|---|---|
children |
Return child type nodes for graph traversal. |
edges |
Return all outgoing edges from this node. |
resolved |
Return the terminal resolved type, traversing forward reference chains. |
get_fields |
Return the field definitions. |
Source code in src/typing_graph/_node.py
children
abstractmethod
¶
edges
abstractmethod
¶
edges() -> Sequence[TypeEdgeConnection]
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.ClassNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 | |
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.DataclassNode
dataclass
¶
Bases: StructuredNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.TypedDictNode
dataclass
¶
Bases: StructuredNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.NamedTupleNode
dataclass
¶
Bases: StructuredNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.EnumNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.ProtocolNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
Function and callable nodes¶
Nodes representing functions, callables, and signatures.
Classes:
| Name | Description |
|---|---|
FunctionNode |
A function with full type information. |
CallableNode |
Callable[[P1, P2], R] or Callable[P, R] or Callable[..., R]. |
SignatureNode |
A full callable signature with named parameters. |
MethodSig |
A method signature (not a TypeNode itself). |
typing_graph.FunctionNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.CallableNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.SignatureNode
dataclass
¶
Bases: TypeNode
Methods:
| Name | Description |
|---|---|
resolved |
Return the terminal resolved type, traversing forward reference chains. |
Source code in src/typing_graph/_node.py
resolved ¶
resolved() -> TypeNode
Returns:
| Type | Description |
|---|---|
TypeNode
|
The terminal resolved TypeNode, or self if this is already a |
TypeNode
|
concrete (non-ForwardRefNode) type. |
Source code in src/typing_graph/_node.py
typing_graph.MethodSig
dataclass
¶
Source code in src/typing_graph/_node.py
Helper types¶
Supporting types for field definitions, parameters, and source locations.
Classes:
| Name | Description |
|---|---|
FieldDef |
A named field with a type (not a TypeNode itself). |
DataclassFieldDef |
Extended field definition for dataclasses. |
Parameter |
A callable parameter (not a TypeNode itself). |
SourceLocation |
Source code location for a type definition. |
ModuleTypes |
Collection of types discovered in a module. |
Attributes:
| Name | Type | Description |
|---|---|---|
ClassInspectResult |
Type alias for possible class inspection results. |
typing_graph.ClassInspectResult
module-attribute
¶
ClassInspectResult = (
ClassNode | DataclassNode | TypedDictNode | NamedTupleNode | ProtocolNode | EnumNode
)
typing_graph.FieldDef
dataclass
¶
Source code in src/typing_graph/_node.py
typing_graph.DataclassFieldDef
dataclass
¶
Bases: FieldDef
Source code in src/typing_graph/_node.py
typing_graph.Parameter
dataclass
¶
Source code in src/typing_graph/_node.py
typing_graph.SourceLocation
dataclass
¶
typing_graph.ModuleTypes
dataclass
¶
Attributes:
| Name | Type | Description |
|---|---|---|
classes |
dict[str, ClassInspectResult]
|
Mapping of class names to their inspection results. |
functions |
dict[str, FunctionNode]
|
Mapping of function names to their FunctionNode. |
type_aliases |
dict[str, GenericAliasNode | TypeAliasNode]
|
Mapping of type alias names to their nodes. |
type_vars |
dict[str, TypeVarNode | ParamSpecNode | TypeVarTupleNode]
|
Mapping of type variable names to their nodes. |
constants |
dict[str, TypeNode]
|
Mapping of annotated constant names to their TypeNode. |
Source code in src/typing_graph/_inspect_module.py
Metadata collection¶
Classes for working with type metadata.
Classes:
| Name | Description |
|---|---|
MetadataCollection |
Immutable collection of metadata from Annotated types. |
SupportsLessThan |
Protocol for types supporting the < operator. |
typing_graph.MetadataCollection
dataclass
¶
Attributes:
| Name | Type | Description |
|---|---|---|
_items |
tuple[object, ...]
|
The internal tuple storing metadata items. |
Examples:
>>> # Use the EMPTY singleton for empty collections
>>> MetadataCollection.EMPTY is MetadataCollection.EMPTY
True
>>> # Create a collection with items (factory method in later stage)
>>> coll = MetadataCollection(_items=("doc", 42, True))
>>> len(coll)
3
>>> list(coll)
['doc', 42, True]
Methods:
| Name | Description |
|---|---|
__len__ |
Return the number of items in the collection. |
__iter__ |
Yield items in insertion order. |
__contains__ |
Check if an item is in the collection using equality comparison. |
__getitem__ |
Access items by index or slice. |
__bool__ |
Return True if the collection is non-empty. |
__eq__ |
Compare collections for equality. |
__hash__ |
Return hash value if all items are hashable. |
__repr__ |
Return a debug representation of the collection. |
__add__ |
Concatenate two collections. |
__or__ |
Concatenate two collections using the | operator. |
find |
Return the first item that is an instance of the given type. |
find_first |
Return the first item matching any of the given types. |
has |
Check if any item is an instance of any of the given types. |
count |
Count items that are instances of any of the given types. |
find_all |
Return all items that are instances of any of the given types. |
get |
Return the first matching item or a default value. |
get_required |
Return the first matching item or raise MetadataNotFoundError. |
filter |
Return items for which predicate returns True. |
filter_by_type |
Return items of given type for which predicate returns True. |
first |
Return first item for which predicate returns True. |
first_of_type |
Return first item of type matching optional predicate. |
any |
Return True if predicate returns True for any item. |
find_protocol |
Return items that satisfy the given protocol. |
has_protocol |
Return True if any item satisfies the given protocol. |
count_protocol |
Return count of items satisfying the given protocol. |
of |
Create a collection from an iterable. |
from_annotated |
Extract metadata from an Annotated type. |
flatten |
Return new collection with GroupedMetadata expanded (single level). |
flatten_deep |
Return new collection with GroupedMetadata recursively expanded. |
exclude |
Return items that are NOT instances of any of the given types. |
unique |
Return collection with duplicate items removed. |
sorted |
Return collection with items sorted. |
reversed |
Return collection with items in reverse order. |
map |
Apply a function to each item and return results as a tuple. |
partition |
Split collection into matching and non-matching items. |
types |
Return the set of unique types in the collection. |
by_type |
Group items by their type. |
Source code in src/typing_graph/_metadata.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 | |
is_hashable
property
¶
is_hashable: bool
is_empty
property
¶
is_empty: bool
__len__ ¶
__len__() -> int
Returns:
| Type | Description |
|---|---|
int
|
The count of metadata items. |
Examples:
Source code in src/typing_graph/_metadata.py
__iter__ ¶
Yields:
| Type | Description |
|---|---|
object
|
Each metadata item in the order it was added. |
Examples:
Source code in src/typing_graph/_metadata.py
__contains__ ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
object
|
The item to check for membership. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the item is in the collection, False otherwise. |
Examples:
>>> coll = MetadataCollection(_items=("doc", 42))
>>> "doc" in coll
True
>>> "missing" in coll
False
Source code in src/typing_graph/_metadata.py
__getitem__ ¶
__getitem__(index: slice) -> MetadataCollection
__getitem__(index: int | slice) -> object | MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | slice
|
An integer index or slice object. |
required |
Returns:
| Type | Description |
|---|---|
object | MetadataCollection
|
The item at the index (for int) or a new MetadataCollection |
object | MetadataCollection
|
containing the sliced items (for slice). |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the integer index is out of range. |
Examples:
>>> coll = MetadataCollection(_items=("a", "b", "c", "d"))
>>> coll[0]
'a'
>>> coll[-1]
'd'
>>> list(coll[1:3])
['b', 'c']
>>> coll[::2] # Returns MetadataCollection
MetadataCollection(['a', 'c'])
Source code in src/typing_graph/_metadata.py
__bool__ ¶
__bool__() -> bool
Returns:
| Type | Description |
|---|---|
bool
|
True if the collection contains items, False if empty. |
Examples:
Source code in src/typing_graph/_metadata.py
__eq__ ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
The object to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if other is a MetadataCollection with equal items, |
bool
|
NotImplemented if other is not a MetadataCollection. |
Examples:
>>> a = MetadataCollection(_items=(1, 2, 3))
>>> b = MetadataCollection(_items=(1, 2, 3))
>>> a == b
True
>>> c = MetadataCollection(_items=(3, 2, 1))
>>> a == c
False
Source code in src/typing_graph/_metadata.py
__hash__ ¶
__hash__() -> int
Returns:
| Type | Description |
|---|---|
int
|
Hash value based on the items tuple. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any item in the collection is unhashable. The error message indicates which items caused the issue. |
Examples:
>>> coll = MetadataCollection(_items=(1, "doc", (2, 3)))
>>> isinstance(hash(coll), int) # Works for hashable items
True
>>> coll_unhashable = MetadataCollection(_items=([1, 2],))
>>> hash(coll_unhashable)
Traceback (most recent call last):
...
TypeError: MetadataCollection contains unhashable items...
Source code in src/typing_graph/_metadata.py
__repr__ ¶
__repr__() -> str
Returns:
| Type | Description |
|---|---|
str
|
A string in the format MetadataCollection([item1, item2, ...]). |
Examples:
>>> MetadataCollection(_items=())
MetadataCollection([])
>>> MetadataCollection(_items=(1, 2))
MetadataCollection([1, 2])
>>> MetadataCollection(_items=(1, 2, 3, 4, 5, 6, 7))
MetadataCollection([1, 2, 3, 4, 5, ...<2 more>])
Source code in src/typing_graph/_metadata.py
__add__ ¶
__add__(other: object) -> MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Another MetadataCollection to concatenate. |
required |
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with concatenated items, or EMPTY if |
MetadataCollection
|
both collections are empty. Returns NotImplemented if other |
MetadataCollection
|
is not a MetadataCollection. |
Examples:
>>> a = MetadataCollection(_items=(1, 2))
>>> b = MetadataCollection(_items=(3, 4))
>>> list(a + b)
[1, 2, 3, 4]
>>> empty = MetadataCollection.EMPTY
>>> (empty + empty) is MetadataCollection.EMPTY
True
Source code in src/typing_graph/_metadata.py
__or__ ¶
__or__(other: object) -> MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Another MetadataCollection to concatenate. |
required |
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with concatenated items. |
Examples:
>>> a = MetadataCollection(_items=(1, 2))
>>> b = MetadataCollection(_items=(3, 4))
>>> list(a | b)
[1, 2, 3, 4]
Source code in src/typing_graph/_metadata.py
find ¶
find(type_: type[T]) -> T | None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type[T]
|
The type to search for (including subclasses). |
required |
Returns:
| Type | Description |
|---|---|
T | None
|
The first matching item, or None if no match is found. |
Examples:
>>> coll = MetadataCollection(_items=("doc", 42, True))
>>> coll.find(int) # Returns 42, not True (first match)
42
>>> coll.find(str)
'doc'
>>> coll.find(float) is None
True
Source code in src/typing_graph/_metadata.py
find_first ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*types
|
type
|
One or more types to search for. |
()
|
Returns:
| Type | Description |
|---|---|
object | None
|
The first item that is an instance of any of the given types, |
object | None
|
or None if no match is found or no types are provided. |
Examples:
>>> coll = MetadataCollection(_items=("doc", 42, True))
>>> coll.find_first(int, bool)
42
>>> coll.find_first(float, complex) is None
True
>>> coll.find_first() is None
True
Source code in src/typing_graph/_metadata.py
has ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*types
|
type
|
One or more types to check for. |
()
|
Returns:
| Type | Description |
|---|---|
bool
|
True if any item matches any of the given types, |
bool
|
False otherwise or if no types are provided. |
Examples:
>>> coll = MetadataCollection(_items=("doc", 42))
>>> coll.has(int)
True
>>> coll.has(float)
False
>>> coll.has(str, int)
True
>>> coll.has()
False
Source code in src/typing_graph/_metadata.py
count ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*types
|
type
|
One or more types to count. |
()
|
Returns:
| Type | Description |
|---|---|
int
|
The number of items matching any of the given types, |
int
|
or 0 if no types are provided. |
Examples:
>>> coll = MetadataCollection(_items=("a", "b", 1, 2, 3))
>>> coll.count(str)
2
>>> coll.count(int)
3
>>> coll.count(str, int)
5
>>> coll.count()
0
Source code in src/typing_graph/_metadata.py
find_all ¶
find_all() -> MetadataCollection
find_all(type_: type[T]) -> MetadataCollection
find_all(type_: type[T], type2_: type, /, *types: type) -> MetadataCollection
find_all(*types: type) -> MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*types
|
type
|
Zero or more types to filter by (including subclasses). |
()
|
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
A new MetadataCollection containing matching items, |
MetadataCollection
|
or all items if no types are specified. |
Examples:
>>> coll = MetadataCollection(_items=("a", 1, "b", 2))
>>> list(coll.find_all())
['a', 1, 'b', 2]
>>> list(coll.find_all(str))
['a', 'b']
>>> list(coll.find_all(int, str))
['a', 1, 'b', 2]
>>> coll.find_all(float) is MetadataCollection.EMPTY
True
Source code in src/typing_graph/_metadata.py
get ¶
get(type_: type[T], default: D | None = None) -> T | D | None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type[T]
|
The type to search for. |
required |
default
|
D | None
|
Value to return if no match is found. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
T | D | None
|
The first matching item, or the default value if not found. |
Examples:
>>> coll = MetadataCollection(_items=("doc", 42))
>>> coll.get(int)
42
>>> coll.get(float) is None
True
>>> coll.get(float, -1)
-1
>>> coll.get(float, "missing")
'missing'
>>> # Falsy values are returned correctly
>>> coll = MetadataCollection(_items=(0, False, ""))
>>> coll.get(int, -1)
0
>>> coll.get(bool, True)
False
Source code in src/typing_graph/_metadata.py
get_required ¶
get_required(type_: type[T]) -> T
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type[T]
|
The type to search for. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The first matching item. |
Raises:
| Type | Description |
|---|---|
MetadataNotFoundError
|
If no item of the given type is found. |
Examples:
>>> coll = MetadataCollection(_items=("doc", 42))
>>> coll.get_required(int)
42
>>> coll.get_required(float)
Traceback (most recent call last):
...
MetadataNotFoundError: No metadata of type 'float' found...
Source code in src/typing_graph/_metadata.py
filter ¶
filter(predicate: Callable[[object], bool]) -> MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[object], bool]
|
Callable taking an item, returning True if it should be included. |
required |
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with matching items, or EMPTY if none match. |
Examples:
>>> coll = MetadataCollection(_items=(1, 2, 3, 4, 5))
>>> evens = coll.filter(lambda x: x % 2 == 0)
>>> list(evens)
[2, 4]
Source code in src/typing_graph/_metadata.py
filter_by_type ¶
filter_by_type(type_: type[T], predicate: Callable[[T], bool]) -> MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type[T]
|
Type to filter by. |
required |
predicate
|
Callable[[T], bool]
|
Callable taking typed item, returning True to include. |
required |
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with matching items, or EMPTY if none match. |
Examples:
>>> coll = MetadataCollection(_items=("short", "medium", "verylongstring"))
>>> long_strings = coll.filter_by_type(str, lambda s: len(s) > 6)
>>> list(long_strings)
['verylongstring']
Source code in src/typing_graph/_metadata.py
first ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[object], bool]
|
Callable taking an item, returning True if it matches. |
required |
Returns:
| Type | Description |
|---|---|
object | None
|
First matching item, or None if no match. |
Examples:
>>> coll = MetadataCollection(_items=(1, 2, 3, 4, 5))
>>> coll.first(lambda x: x > 3)
4
>>> coll.first(lambda x: x > 10) is None
True
Source code in src/typing_graph/_metadata.py
first_of_type ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type[T]
|
Type to search for. |
required |
predicate
|
Callable[[T], bool] | None
|
Optional callable to filter typed items. |
None
|
Returns:
| Type | Description |
|---|---|
T | None
|
First matching item, or None if no match. |
Examples:
>>> coll = MetadataCollection(_items=("a", 10, "bb", 20))
>>> coll.first_of_type(int, lambda x: x > 15)
20
>>> coll.first_of_type(str)
'a'
Source code in src/typing_graph/_metadata.py
any ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[object], bool]
|
Callable taking an item, returning bool. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if any item satisfies predicate, False otherwise. |
Examples:
>>> coll = MetadataCollection(_items=(1, 2, 3, 4, 5))
>>> coll.any(lambda x: x > 3)
True
>>> coll.any(lambda x: x > 10)
False
Source code in src/typing_graph/_metadata.py
find_protocol ¶
find_protocol(protocol: type) -> MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
protocol
|
type
|
A @runtime_checkable Protocol type. |
required |
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with matching items, or EMPTY if none match. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If protocol is not a Protocol. |
ProtocolNotRuntimeCheckableError
|
If protocol lacks @runtime_checkable. |
Examples:
>>> from typing import Protocol, runtime_checkable
>>> @runtime_checkable
... class HasValue(Protocol):
... value: int
>>> class Item:
... value = 42
>>> coll = MetadataCollection(_items=(Item(), "doc", 123))
>>> matches = coll.find_protocol(HasValue)
>>> len(matches)
1
Source code in src/typing_graph/_metadata.py
has_protocol ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
protocol
|
type
|
A @runtime_checkable Protocol type. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if any item satisfies the protocol. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If protocol is not a Protocol. |
ProtocolNotRuntimeCheckableError
|
If protocol lacks @runtime_checkable. |
Examples:
>>> from typing import Protocol, runtime_checkable
>>> @runtime_checkable
... class HasLen(Protocol):
... def __len__(self) -> int: ...
>>> coll = MetadataCollection(_items=([1, 2], "doc", 123))
>>> coll.has_protocol(HasLen)
True
Source code in src/typing_graph/_metadata.py
count_protocol ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
protocol
|
type
|
A @runtime_checkable Protocol type. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of items satisfying the protocol. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If protocol is not a Protocol. |
ProtocolNotRuntimeCheckableError
|
If protocol lacks @runtime_checkable. |
Examples:
>>> from typing import Protocol, runtime_checkable
>>> @runtime_checkable
... class HasLen(Protocol):
... def __len__(self) -> int: ...
>>> coll = MetadataCollection(_items=([1, 2], "doc", 123, (3, 4)))
>>> coll.count_protocol(HasLen)
3
Source code in src/typing_graph/_metadata.py
of
classmethod
¶
of(items: Iterable[object] = (), *, auto_flatten: bool = True) -> MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Iterable[object]
|
Iterable of metadata objects. |
()
|
auto_flatten
|
bool
|
If True (default), expand GroupedMetadata items one level. Set to False to preserve GroupedMetadata as-is. |
True
|
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection containing the items, or EMPTY if no items. |
Examples:
>>> MetadataCollection.of([1, 2, 3])
MetadataCollection([1, 2, 3])
>>> MetadataCollection.of([])
MetadataCollection([])
>>> MetadataCollection.of([]) is MetadataCollection.EMPTY
True
Source code in src/typing_graph/_metadata.py
from_annotated
classmethod
¶
from_annotated(
annotated_type: object, *, unwrap_nested: bool = True
) -> MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotated_type
|
object
|
A type, potentially |
required |
unwrap_nested
|
bool
|
If True (default), recursively unwrap nested Annotated types, collecting all metadata. Outer metadata comes first, then inner metadata. |
True
|
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
MetadataCollection with extracted metadata, or EMPTY if the type |
MetadataCollection
|
is not Annotated or has no metadata. |
Examples:
>>> from typing import Annotated
>>> MetadataCollection.from_annotated(Annotated[int, "doc", 42])
MetadataCollection(['doc', 42])
>>> MetadataCollection.from_annotated(int)
MetadataCollection([])
>>> # Nested Annotated types are unwrapped by default
>>> Inner = Annotated[int, "inner"]
>>> Outer = Annotated[Inner, "outer"]
>>> MetadataCollection.from_annotated(Outer)
MetadataCollection(['inner', 'outer'])
Source code in src/typing_graph/_metadata.py
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 | |
flatten ¶
flatten() -> MetadataCollection
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with GroupedMetadata expanded one level, |
MetadataCollection
|
or self if no GroupedMetadata items exist. |
Examples:
Source code in src/typing_graph/_metadata.py
flatten_deep ¶
flatten_deep() -> MetadataCollection
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with all GroupedMetadata fully expanded, |
MetadataCollection
|
or self if no GroupedMetadata items exist. |
Examples:
>>> coll = MetadataCollection(_items=(1, 2, 3))
>>> coll.flatten_deep()
MetadataCollection([1, 2, 3])
Source code in src/typing_graph/_metadata.py
exclude ¶
exclude(*types: type) -> MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*types
|
type
|
One or more types to exclude. |
()
|
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with non-matching items, or EMPTY if |
MetadataCollection
|
all items match. Returns self if no types are provided. |
Examples:
>>> coll = MetadataCollection(_items=("a", 1, "b", 2))
>>> list(coll.exclude(int))
['a', 'b']
>>> list(coll.exclude(str, int))
[]
>>> coll.exclude() is coll
True
Source code in src/typing_graph/_metadata.py
unique ¶
unique() -> MetadataCollection
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with unique items, or self if already unique. |
Examples:
>>> coll = MetadataCollection(_items=(1, 2, 1, 3, 2))
>>> list(coll.unique())
[1, 2, 3]
>>> # Unhashable items are handled
>>> coll = MetadataCollection(_items=([1], [2], [1]))
>>> list(coll.unique())
[[1], [2]]
Source code in src/typing_graph/_metadata.py
sorted ¶
sorted(
*, key: Callable[[object], SupportsLessThan] | None = None
) -> MetadataCollection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Callable[[object], SupportsLessThan] | None
|
Optional callable that extracts a comparison key from each item. The key must return a value supporting the < operator. |
None
|
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with sorted items, or EMPTY if empty. |
Examples:
>>> coll = MetadataCollection(_items=(3, 1, 2))
>>> list(coll.sorted())
[1, 2, 3]
>>> coll = MetadataCollection(_items=("b", "a", "c"))
>>> list(coll.sorted())
['a', 'b', 'c']
>>> # Custom key function
>>> coll = MetadataCollection(_items=("bb", "a", "ccc"))
>>> list(coll.sorted(key=len))
['a', 'bb', 'ccc']
Source code in src/typing_graph/_metadata.py
reversed ¶
reversed() -> MetadataCollection
Returns:
| Type | Description |
|---|---|
MetadataCollection
|
New MetadataCollection with reversed items, or EMPTY if empty. |
Examples:
>>> coll = MetadataCollection(_items=(1, 2, 3))
>>> list(coll.reversed())
[3, 2, 1]
>>> MetadataCollection.EMPTY.reversed() is MetadataCollection.EMPTY
True
Source code in src/typing_graph/_metadata.py
map ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[object], T]
|
Callable to apply to each item. |
required |
Returns:
| Type | Description |
|---|---|
tuple[T, ...]
|
Tuple containing the results of applying func to each item. |
Examples:
>>> coll = MetadataCollection(_items=(1, 2, 3))
>>> coll.map(lambda x: x * 2)
(2, 4, 6)
>>> coll = MetadataCollection(_items=("a", "bb", "ccc"))
>>> coll.map(len)
(1, 2, 3)
Source code in src/typing_graph/_metadata.py
partition ¶
partition(
predicate: Callable[[object], bool],
) -> tuple[MetadataCollection, MetadataCollection]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[object], bool]
|
Callable taking an item, returning True if it should be in the first partition. |
required |
Returns:
| Type | Description |
|---|---|
tuple[MetadataCollection, MetadataCollection]
|
Tuple of (matching, non_matching) MetadataCollections. |
Examples:
>>> coll = MetadataCollection(_items=(1, 2, 3, 4, 5))
>>> matching, non_matching = coll.partition(lambda x: x % 2 == 0)
>>> list(matching)
[2, 4]
>>> list(non_matching)
[1, 3, 5]
Source code in src/typing_graph/_metadata.py
types ¶
Returns:
| Type | Description |
|---|---|
frozenset[type]
|
Frozenset containing the type of each unique item type. |
Examples:
>>> coll = MetadataCollection(_items=("a", 1, "b", 2.0))
>>> sorted(t.__name__ for t in coll.types())
['float', 'int', 'str']
Source code in src/typing_graph/_metadata.py
by_type ¶
Returns:
| Type | Description |
|---|---|
Mapping[type, tuple[object, ...]]
|
Immutable mapping from type to tuple of items of that type. |
Mapping[type, tuple[object, ...]]
|
Order within each group matches original insertion order. |
Examples:
>>> coll = MetadataCollection(_items=("a", 1, "b", 2))
>>> grouped = coll.by_type()
>>> list(grouped[str])
['a', 'b']
>>> list(grouped[int])
[1, 2]
Source code in src/typing_graph/_metadata.py
typing_graph.SupportsLessThan ¶
Bases: Protocol
Methods:
| Name | Description |
|---|---|
__lt__ |
Compare this object to another for less-than ordering. |
Source code in src/typing_graph/_metadata.py
Edge types¶
Types representing edges between nodes in the type graph.
Classes:
| Name | Description |
|---|---|
TypeEdge |
Metadata describing a graph edge between nodes. |
TypeEdgeKind |
Semantic relationship between parent and child nodes. |
TypeEdgeConnection |
A connection from a node to a child node via an edge. |
typing_graph.TypeEdge
dataclass
¶
Methods:
| Name | Description |
|---|---|
field |
Create a FIELD edge with the given name. |
element |
Create an ELEMENT edge with the given index. |
Source code in src/typing_graph/_node.py
field
classmethod
¶
typing_graph.TypeEdgeKind ¶
Source code in src/typing_graph/_node.py
typing_graph.TypeEdgeConnection
dataclass
¶
Source code in src/typing_graph/_node.py
Exceptions¶
Exception classes for error handling.
Base exception¶
Classes:
| Name | Description |
|---|---|
TypingGraphError |
Base exception for all typing-graph errors. |
typing_graph.TypingGraphError ¶
Metadata exceptions¶
Classes:
| Name | Description |
|---|---|
MetadataNotFoundError |
Raised when requested metadata type is not found in a collection. |
ProtocolNotRuntimeCheckableError |
Raised when a protocol without @runtime_checkable is used for matching. |
typing_graph.MetadataNotFoundError ¶
Bases: LookupError
Attributes:
| Name | Type | Description |
|---|---|---|
requested_type |
type
|
The type that was searched for but not found. |
collection |
MetadataCollection
|
The MetadataCollection that was searched. |
Examples:
>>> coll = MetadataCollection()
>>> try:
... raise MetadataNotFoundError(int, coll)
... except MetadataNotFoundError as e:
... print(e.requested_type)
<class 'int'>
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the exception with the requested type and collection. |
Source code in src/typing_graph/_metadata.py
__init__ ¶
__init__(requested_type: type, collection: MetadataCollection) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requested_type
|
type
|
The type that was not found. |
required |
collection
|
MetadataCollection
|
The collection that was searched. |
required |
Source code in src/typing_graph/_metadata.py
typing_graph.ProtocolNotRuntimeCheckableError ¶
Bases: TypeError
Attributes:
| Name | Type | Description |
|---|---|---|
protocol |
type
|
The protocol type that is not runtime checkable. |
Examples:
>>> from typing import Protocol
>>> class NotRuntime(Protocol):
... value: int
>>> try:
... raise ProtocolNotRuntimeCheckableError(NotRuntime)
... except ProtocolNotRuntimeCheckableError as e:
... print(e.protocol.__name__)
NotRuntime
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the exception with the non-runtime-checkable protocol. |
Source code in src/typing_graph/_metadata.py
__init__ ¶
__init__(protocol: type) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
protocol
|
type
|
The protocol that is not runtime checkable. |
required |
Source code in src/typing_graph/_metadata.py
Traversal exceptions¶
Classes:
| Name | Description |
|---|---|
TraversalError |
Error during graph traversal. |
typing_graph.TraversalError ¶
Bases: TypingGraphError
Source code in src/typing_graph/_exceptions.py
Type guards¶
Node type guards¶
Type guard functions for type narrowing on node types.
Functions:
| Name | Description |
|---|---|
is_type_node |
Return whether the argument is an instance of TypeNode. |
is_concrete_node |
Return whether the argument is a ConcreteNode instance. |
is_annotated_node |
Return whether the argument is an AnnotatedNode instance. |
is_subscripted_generic_node |
Return whether the argument is a SubscriptedGenericNode instance. |
is_generic_alias_node |
Return whether the argument is a GenericAliasNode instance. |
is_generic_node |
Return whether the argument is a GenericType instance. |
is_union_type_node |
Return whether the argument is a UnionNode instance. |
is_intersection_node |
Return whether the argument is an IntersectionNode instance. |
is_type_var_node |
Return whether the argument is a TypeVarNode instance. |
is_param_spec_node |
Return whether the argument is a ParamSpecNode instance. |
is_type_var_tuple_node |
Return whether the argument is a TypeVarTupleNode instance. |
is_type_param_node |
Check if a node is a type parameter (TypeVar, ParamSpec, or TypeVarTuple). |
is_any_node |
Return whether the argument is an AnyNode instance. |
is_never_node |
Return whether the argument is a NeverNode instance. |
is_self_node |
Return whether the argument is a SelfNode instance. |
is_literal_node |
Return whether the argument is a LiteralNode instance. |
is_literal_string_node |
Return whether the argument is a LiteralStringNode instance. |
is_tuple_node |
Return whether the argument is a TupleNode instance. |
is_ellipsis_node |
Return whether the argument is an EllipsisNode instance. |
is_forward_ref_node |
Return whether the argument is a ForwardRefNode instance. |
is_meta_node |
Return whether the argument is a MetaNode instance. |
is_concatenate_node |
Return whether the argument is a ConcatenateNode instance. |
is_unpack_node |
Return whether the argument is an UnpackNode instance. |
is_type_guard_node |
Return whether the argument is a TypeGuardNode instance. |
is_type_is_node |
Return whether the argument is a TypeIsNode instance. |
is_ref_state_resolved |
Return whether the argument is a RefResolved instance. |
is_ref_state_unresolved |
Return whether the argument is a RefUnresolved instance. |
is_ref_state_failed |
Return whether the argument is a RefFailed instance. |
is_structured_node |
Return whether the argument is a StructuredNode instance. |
is_class_node |
Return whether the argument is a ClassNode instance. |
is_dataclass_node |
Return whether the argument is a DataclassNode instance. |
is_typed_dict_node |
Return whether the argument is a TypedDictNode instance. |
is_named_tuple_node |
Return whether the argument is a NamedTupleNode instance. |
is_enum_node |
Return whether the argument is an EnumNode instance. |
is_protocol_node |
Return whether the argument is a ProtocolNode instance. |
is_function_node |
Return whether the argument is a FunctionNode instance. |
is_callable_node |
Return whether the argument is a CallableNode instance. |
is_signature_node |
Return whether the argument is a SignatureNode instance. |
is_method_sig |
Return whether the argument is a MethodSig instance. |
is_type_alias_node |
Return whether the argument is a TypeAliasNode instance. |
is_new_type_node |
Return whether the argument is a NewTypeNode instance. |
typing_graph.is_type_node ¶
typing_graph.is_concrete_node ¶
is_concrete_node(obj: object) -> TypeIs[ConcreteNode]
typing_graph.is_annotated_node ¶
is_annotated_node(obj: object) -> TypeIs[AnnotatedNode]
typing_graph.is_subscripted_generic_node ¶
is_subscripted_generic_node(obj: object) -> TypeIs[SubscriptedGenericNode]
typing_graph.is_generic_alias_node ¶
is_generic_alias_node(obj: object) -> TypeIs[GenericAliasNode]
typing_graph.is_generic_node ¶
is_generic_node(obj: object) -> TypeIs[GenericTypeNode]
typing_graph.is_union_type_node ¶
typing_graph.is_intersection_node ¶
is_intersection_node(obj: object) -> TypeIs[IntersectionNode]
typing_graph.is_type_var_node ¶
is_type_var_node(obj: object) -> TypeIs[TypeVarNode]
typing_graph.is_param_spec_node ¶
is_param_spec_node(obj: object) -> TypeIs[ParamSpecNode]
typing_graph.is_type_var_tuple_node ¶
is_type_var_tuple_node(obj: object) -> TypeIs[TypeVarTupleNode]
typing_graph.is_type_param_node ¶
is_type_param_node(node: TypeNode) -> TypeIs[TypeParamNode]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TypeNode
|
The TypeNode to check. |
required |
Returns:
| Type | Description |
|---|---|
TypeIs[TypeParamNode]
|
True if the node is a TypeVarNode, ParamSpecNode, or TypeVarTupleNode. |
Source code in src/typing_graph/_node.py
typing_graph.is_any_node ¶
typing_graph.is_never_node ¶
typing_graph.is_self_node ¶
typing_graph.is_literal_node ¶
is_literal_node(obj: object) -> TypeIs[LiteralNode]
typing_graph.is_literal_string_node ¶
is_literal_string_node(obj: object) -> TypeIs[LiteralStringNode]
typing_graph.is_tuple_node ¶
typing_graph.is_ellipsis_node ¶
is_ellipsis_node(obj: object) -> TypeIs[EllipsisNode]
typing_graph.is_forward_ref_node ¶
is_forward_ref_node(obj: object) -> TypeIs[ForwardRefNode]
typing_graph.is_meta_node ¶
typing_graph.is_concatenate_node ¶
is_concatenate_node(obj: object) -> TypeIs[ConcatenateNode]
typing_graph.is_unpack_node ¶
is_unpack_node(obj: object) -> TypeIs[UnpackNode]
typing_graph.is_type_guard_node ¶
is_type_guard_node(obj: object) -> TypeIs[TypeGuardNode]
typing_graph.is_type_is_node ¶
is_type_is_node(obj: object) -> TypeIs[TypeIsNode]
typing_graph.is_ref_state_resolved ¶
is_ref_state_resolved(state: object) -> TypeIs[RefResolved]
typing_graph.is_ref_state_unresolved ¶
is_ref_state_unresolved(state: object) -> TypeIs[RefUnresolved]
typing_graph.is_ref_state_failed ¶
typing_graph.is_structured_node ¶
is_structured_node(obj: object) -> TypeIs[StructuredNode]
typing_graph.is_class_node ¶
typing_graph.is_dataclass_node ¶
is_dataclass_node(obj: object) -> TypeIs[DataclassNode]
typing_graph.is_typed_dict_node ¶
is_typed_dict_node(obj: object) -> TypeIs[TypedDictNode]
typing_graph.is_named_tuple_node ¶
is_named_tuple_node(obj: object) -> TypeIs[NamedTupleNode]
typing_graph.is_enum_node ¶
typing_graph.is_protocol_node ¶
is_protocol_node(obj: object) -> TypeIs[ProtocolNode]
typing_graph.is_function_node ¶
is_function_node(obj: object) -> TypeIs[FunctionNode]
typing_graph.is_callable_node ¶
is_callable_node(obj: object) -> TypeIs[CallableNode]
typing_graph.is_signature_node ¶
is_signature_node(obj: object) -> TypeIs[SignatureNode]
typing_graph.is_method_sig ¶
typing_graph.is_type_alias_node ¶
is_type_alias_node(obj: object) -> TypeIs[TypeAliasNode]
Class detection functions¶
Functions for detecting special class types at runtime.
Functions:
| Name | Description |
|---|---|
is_dataclass_class |
Check if cls is a dataclass. |
is_typeddict_class |
Check if cls is a TypedDict. |
is_namedtuple_class |
Check if cls is a NamedTuple. |
is_enum_class |
Check if cls is an Enum subclass with TypeIs narrowing. |
is_protocol_class |
Check if cls is a Protocol. |
typing_graph.is_dataclass_class ¶
Source code in src/typing_graph/_inspect_class.py
typing_graph.is_typeddict_class ¶
Source code in src/typing_graph/_inspect_class.py
typing_graph.is_namedtuple_class ¶
Source code in src/typing_graph/_inspect_class.py
typing_graph.is_enum_class ¶
Source code in src/typing_graph/_inspect_class.py
typing_graph.is_protocol_class ¶
Source code in src/typing_graph/_inspect_class.py
Utility functions¶
Helper functions for working with union types and optional values.
Functions:
| Name | Description |
|---|---|
get_union_members |
Extract union members from either union representation. |
is_union_node |
Check if a node represents any union type. |
is_optional_node |
Check if a node represents an optional type (union containing None). |
unwrap_optional |
Extract non-None types from an optional union. |
to_runtime_type |
Convert a TypeNode back to runtime type hints. |
typing_graph.get_union_members ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TypeNode
|
A TypeNode to extract union members from. |
required |
Returns:
| Type | Description |
|---|---|
tuple[TypeNode, ...] | None
|
A tuple of member TypeNodes if |
tuple[TypeNode, ...] | None
|
or |
Examples:
>>> from typing import Literal
>>> from typing_graph import inspect_type, get_union_members
>>>
>>> # types.UnionType (PEP 604 with concrete types)
>>> node1 = inspect_type(int | str)
>>> members1 = get_union_members(node1)
>>> len(members1)
2
>>>
>>> # typing.Union (from Literal | Literal)
>>> node2 = inspect_type(Literal["a"] | Literal["b"])
>>> members2 = get_union_members(node2)
>>> len(members2)
2
Source code in src/typing_graph/_helpers.py
typing_graph.is_union_node ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TypeNode
|
A TypeNode to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
>>> from typing import Literal
>>> from typing_graph import inspect_type, is_union_node
>>>
>>> is_union_node(inspect_type(int | str))
True
>>> is_union_node(inspect_type(Literal["a"] | Literal["b"]))
True
>>> is_union_node(inspect_type(list[int]))
False
Source code in src/typing_graph/_helpers.py
typing_graph.is_optional_node ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TypeNode
|
A TypeNode to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
>>> from typing import Optional, Union
>>> from typing_graph import inspect_type, is_optional_node
>>>
>>> is_optional_node(inspect_type(int | None))
True
>>> is_optional_node(inspect_type(Optional[str]))
True
>>> is_optional_node(inspect_type(int | str))
False
>>> is_optional_node(inspect_type(int))
False
Source code in src/typing_graph/_helpers.py
typing_graph.unwrap_optional ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TypeNode
|
A TypeNode to unwrap. |
required |
Returns:
| Type | Description |
|---|---|
tuple[TypeNode, ...] | None
|
A tuple of non-None member TypeNodes if |
tuple[TypeNode, ...] | None
|
or |
Examples:
>>> from typing import Optional
>>> from typing_graph import inspect_type, unwrap_optional, is_concrete_node
>>>
>>> node = inspect_type(int | None)
>>> unwrapped = unwrap_optional(node)
>>> len(unwrapped)
1
>>> is_concrete_node(unwrapped[0]) and unwrapped[0].cls is int
True
>>>
>>> # Multiple non-None types
>>> node2 = inspect_type(int | str | None)
>>> unwrapped2 = unwrap_optional(node2)
>>> len(unwrapped2)
2
>>>
>>> # Not an optional
>>> unwrap_optional(inspect_type(int | str)) is None
True
Source code in src/typing_graph/_helpers.py
typing_graph.to_runtime_type ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TypeNode
|
The TypeNode to convert. |
required |
include_extras
|
bool
|
Whether to include metadata as Annotated (default True). |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
A runtime type annotation corresponding to the node. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the node is a TypeVarNode, ParamSpecNode, TypeVarTupleNode, or a CallableNode with ParamSpec parameters. These types cannot be reconstructed because the original TypeVar/ParamSpec objects are not preserved. |
Source code in src/typing_graph/_inspect_type.py
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 | |
Cache management¶
Functions for managing the type inspection cache.
Functions:
| Name | Description |
|---|---|
cache_info |
Return type inspection cache statistics. |
cache_clear |
Clear the global type inspection cache. |
typing_graph.cache_info ¶
Returns:
| Type | Description |
|---|---|
_CacheInfo
|
A CacheInfo named tuple with: |
_CacheInfo
|
|
_CacheInfo
|
|
_CacheInfo
|
|
_CacheInfo
|
|