module documentation

Undocumented

Class MarkedSubstitutions Undocumented
Class Substitutions A domain of instantiated type variables.
Function approximate_overload Undocumented
Function better_signature Get a signature from the function def information wrapped by this type instance. Return None if there is no function wrapped, in the case of a written 'Callable' annotation for instance.
Function bind_callables Dertemine signature based compatibility between two callables. Returns (caller, callee, bound arguments).
Function callable_struct Returns tuple: (args, kwargs, rtype)
Function debug_unify Undocumented
Function merge_unify When two possible values for a given type variable doesn't unify, we use the first common - nominal - supertype, that's not object, or fail.
Function occurs_in Checks whether a types variable occurs in any other types.
Function occurs_in_type Checks whether a type variable occurs in a type expression.
Function poor_signature Get a approximated signature based on a Callable type (created from annotations or from ast.Call before unification).
Function substitute Undocumented
Function unify Unify the two types t1 and t2.
def approximate_overload(b: Type, types: Sequence[Type], subst: Substitutions) -> Type: (source)

Undocumented

def better_signature(callable_type: Type) -> inspect.Signature | None: (source)

Get a signature from the function def information wrapped by this type instance. Return None if there is no function wrapped, in the case of a written 'Callable' annotation for instance.

def bind_callables(a: Type, b: Type) -> tuple[Type, Type, inspect.BoundArguments]: (source)

Dertemine signature based compatibility between two callables. Returns (caller, callee, bound arguments).

def callable_struct(callable_type: Type) -> tuple[Sequence[Type], Mapping[str, Type], Type]: (source)

Returns tuple: (args, kwargs, rtype)

def debug_unify(t1: Type, t2: Type, subst: Substitutions | None = None) -> Type: (source)

Undocumented

def merge_unify(t1: Type, t2: Type, subst: Substitutions) -> Type: (source)

When two possible values for a given type variable doesn't unify, we use the first common - nominal - supertype, that's not object, or fail.

So list[str] and set[str] will be reconcile to Collection[str]. But None and int can't reconcile.

def occurs_in(t: TypeVariable, types: Sequence[Type]) -> bool: (source)

Checks whether a types variable occurs in any other types.

Args:
t: The TypeVariable to be tested for types: The sequence of types in which to search
Returns:
True if t occurs in any of types, otherwise False
def occurs_in_type(v: TypeVariable, type2: Type) -> bool: (source)

Checks whether a type variable occurs in a type expression.

Note: Must be called with v pre-pruned

Args:
v: The TypeVariable to be tested for type2: The type in which to search
Returns:
True if v occurs in type2, otherwise False
def poor_signature(callable_type: Type) -> inspect.Signature: (source)

Get a approximated signature based on a Callable type (created from annotations or from ast.Call before unification).

def substitute(term: Type, subst: Substitutions) -> Type: (source)

Undocumented

def unify(t1: Type, t2: Type, subst: Substitutions | None = None) -> Type: (source)

Unify the two types t1 and t2.

Returns a new type that represent the unification of both type.

Args:
t1: The first type to be made equivalent t2: The second type to be be equivalent subst: type variables substitutions.
Returns:
Tuple: (The unified type, Subst)
Raises:
InferenceError: Raised if the types cannot be unified.
>>> t = Type.overload.add_args(args=
... [Type.Callable.add_args([Type('float', 'builtins'), Type('int', 'builtins'),]),
...  Type.Callable.add_args([Type.Union.add_args([Type('str', 'builtins'),
...                                               Type('bytes', 'builtins'),
...                                               Type('object', 'builtins')]),
...                          Type('str', 'builtins'),])])
>>> expr1 = Type.Callable.add_args([Type('int', 'builtins'), TypeVariable()])
>>> print(t.annotation)
(float) -> int | (str | bytes | object) -> str
>>> print(expr1.annotation)
(int) -> @TypeVar...
>>> print(unify(expr1, t).annotation)
(float) -> int
>>> expr2 = Type.Callable.add_args([Type('float', 'builtins'), TypeVariable()])
>>> print(unify(expr2, t).annotation)
(float) -> int
>>> expr3 = Type.Callable.add_args([Type('bytes', 'builtins'), TypeVariable()])
>>> print(unify(expr3, t).annotation)
(object) -> str
>>> t2 = Type.Callable.add_args([Type('list', 'builtins').add_args(
...     [Type('@TypeVar1')]), Type('list', 'builtins').add_args(
...     [Type('@TypeVar1')]),])
>>> expr4 = Type.Callable.add_args([Type('list', 'builtins').add_args([Type('float', 'builtins')]), Type('@TypeVar2')])
>>> s = Substitutions()
>>> print(unify(expr4, t2, s).annotation)
(list[float]) -> list[float]