refs
¶
Utilities for working with typing.ForwardRef.
This module allows the developer to create and evaluate typing.ForwardRef instances
with additional logic to support forwards compatibility.
Typical Usage
>>> from typelib.py import refs
>>> ref = refs.forwardref("str")
>>> cls = refs.evaluate(ref)
>>> cls is str
True
Functions:
-
evaluate–Evaluate the
typing.ForwardRefinstance into a proper type. -
forwardref–Create a
typing.ForwardRefinstance from arefstring.
evaluate
¶
evaluate(ref: ForwardRef, *, globalns: Mapping[str, Any] | None = None, localns: Mapping[str, Any] | None = None, recursive_guard: set | None = None) -> Any
Evaluate the typing.ForwardRef instance into a proper type.
Note
Most of the time you will not need to provide anything but ref.
Parameters:
-
(ref¶ForwardRef) –The
typing.ForwardRefinstance to evaluate. -
(globalns¶Mapping[str, Any] | None, default:None) –A mapping of global variable names to values (optional).
-
(localns¶Mapping[str, Any] | None, default:None) –A mapping of local variable names to values (optional).
-
(recursive_guard¶set | None, default:None) –A set instance to prevent recursion during evaluation (optional). Ignored on Python 3.14+.
Source code in src/typelib/py/refs.py
forwardref
¶
forwardref(ref: str | type, *, is_argument: bool = False, module: Any | None = None, is_class: bool = True) -> ForwardRef
Create a typing.ForwardRef instance from a ref string.
This wrapper function will attempt to determine the module name ahead of instantiation if not provided. This is important when resolving the reference to an actual type.
Parameters:
-
(ref¶str | type) –The type reference string.
-
(is_argument¶bool, default:False) –Whether the reference string was an argument to a function (default False).
-
(module¶Any | None, default:None) –The python module in which the reference string is defined (optional)
-
(is_class¶bool, default:True) –Whether the reference string is a class (default True).