Typing Utiltites¶
- class GenericTypeMeta(name, bases, dct)[source]¶
Bases:
type
Metaclass to support subtyping with parameters for pattern matching, e.g.
Number[int, int]
.
- deep_isinstance(obj, cls)[source]¶
Enhanced version of
isinstance()
that can handle basic structuredtyping
types, including Funsor terms and otherGenericTypeMeta
instances,Union
,Tuple
, andFrozenSet
.Does not support
TypeVar
, arbitraryGeneric
, forward references, or mutable generic collection types likeList
. Will attempt to fall back toisinstance()
when it encounters an unsupported type inobj
orcls
.Usage:
x = (1, ("a", "b")) assert deep_isinstance(x, typing.Tuple[int, tuple]) assert deep_isinstance(x, typing.Tuple[typing.Any, typing.Tuple[str, ...]])
- Parameters
obj – An object that may be an instance of
cls
.cls – A class that may be a parent class of
obj
.
- deep_issubclass(subcls, cls)[source]¶
Enhanced version of
issubclass()
that can handle structured types, including Funsor terms,Tuple
, andFrozenSet
.Does not support more advanced
typing
features such asTypeVar
, arbitraryGeneric
subtypes, forward references, or mutable collection types likeList
. Will attempt to fall back toissubclass()
when it encounters a type insubcls
orcls
that it does not understand.Usage:
class A: pass class B(A): pass assert deep_issubclass(typing.Tuple[int, B], typing.Tuple[int, A]) assert not deep_issubclass(typing.Tuple[int, A], typing.Tuple[int, B]) assert deep_issubclass(typing.Tuple[A, A], typing.Tuple[A, ...]) assert not deep_issubclass(typing.Tuple[B], typing.Tuple[A, ...])
- Parameters
subcls – A class that may be a subclass of
cls
.cls – A class that may be a parent class of
subcls
.
- deep_type(obj)[source]¶
- deep_type(obj: tuple)
- deep_type(obj: frozenset)
An enhanced version of
type()
that reconstructs structuredtyping`
types for a limited set of immutable data structures, notablytuple
andfrozenset
. Mostly intended for internal use in Funsor interpretation pattern-matching.Example:
assert deep_type((1, ("a",))) is typing.Tuple[int, typing.Tuple[str]] assert deep_type(frozenset(["a"])) is typing.FrozenSet[str]
- get_type_hints(obj, globalns=None, localns=None)[source]¶
Return type hints for an object.
This is often the same as obj.__annotations__, but it handles forward references encoded as string literals, and if necessary adds Optional[t] if a default value equal to None is set.
The argument may be a module, class, method, or function. The annotations are returned as a dictionary. For classes, annotations include also inherited members.
TypeError is raised if the argument is not of a type that can contain annotations, and an empty dictionary is returned if no annotations are present.
BEWARE – the behavior of globalns and localns is counterintuitive (unless you are familiar with how eval() and exec() work). The search order is locals first, then globals.
If no dict arguments are passed, an attempt is made to use the globals from obj (or the respective module’s globals for classes), and these are also used as the locals. If the object does not appear to have globals, an empty dictionary is used.
If one dict argument is passed, it is used for both globals and locals.
If two dict arguments are passed, they specify globals and locals, respectively.
- register_subclasscheck(cls)[source]¶
Decorator for registering a custom
__subclasscheck__
method forcls
which is only ever invoked indeep_issubclass()
.This is primarily intended for working with the
typing
library at runtime. Prefer overriding__subclasscheck__
in the usual way with a metaclass where possible.