Typing Utiltites


An enhanced version of type() that reconstructs structured typing` types for a limited set of immutable data structures, notably tuple and frozenset. Mostly intended for internal use in Funsor interpretation pattern-matching.


assert deep_type((1, ("a",))) is typing.Tuple[int, typing.Tuple[str]]
assert deep_type(frozenset(["a"])) is typing.FrozenSet[str]

Decorator for registering a custom __subclasscheck__ method for cls which is only ever invoked in deep_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.


Enhanced version of issubclass() that can handle structured types, including Funsor terms, Tuple, and FrozenSet.

Does not support more advanced typing features such as TypeVar, arbitrary Generic subtypes, forward references, or mutable collection types like List. Will attempt to fall back to issubclass() when it encounters a type in subcls or cls that it does not understand.


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, ...])
  • subcls – A class that may be a subclass of cls.
  • cls – A class that may be a parent class of subcls.
deep_isinstance(obj, cls)[source]

Enhanced version of isinstance() that can handle basic structured typing types, including Funsor terms and other GenericTypeMeta instances, Union, Tuple, and FrozenSet.

Does not support TypeVar, arbitrary Generic, forward references, or mutable generic collection types like List. Will attempt to fall back to isinstance() when it encounters an unsupported type in obj or cls.


x = (1, ("a", "b"))
assert deep_isinstance(x, typing.Tuple[int, tuple])
assert deep_isinstance(x, typing.Tuple[typing.Any, typing.Tuple[str, ...]])
  • obj – An object that may be an instance of cls.
  • cls – A class that may be a parent class of obj.
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.
class GenericTypeMeta(name, bases, dct)[source]

Bases: type

Metaclass to support subtyping with parameters for pattern matching, e.g. Number[int, int].

class typing_wrap[source]

Bases: object

Utility callable for overriding the runtime behavior of typing objects.

class Variadic[source]

Bases: object

A typing-compatible drop-in replacement for Variadic.