module documentation

Type inference constraints.

Class CompleteTypeVisitor Undocumented
Class Constraint A representation of a type constraint.
Class ConstraintBuilderVisitor Visitor class for inferring type constraints.
Function any_constraints Deduce what we can from a collection of constraint lists.
Function build_constraints_for_unpack Undocumented
Function filter_satisfiable Keep only constraints that can possibly be satisfied.
Function find_and_build_constraints_for_unpack Undocumented
Function find_matching_overload_item Disambiguate overload item against a template.
Function find_matching_overload_items Like find_matching_overload_item, but return all matches, not just the first.
Function handle_recursive_union Undocumented
Function infer_constraints Infer type constraints.
Function infer_constraints_for_callable Infer type variable constraints for a callable and actual arguments.
Function infer_constraints_if_possible Like infer_constraints, but return None if the input relation is known to be unsatisfiable, for example if template=List[T] and actual=int. (In this case infer_constraints would return [], just like it would for an automatically satisfied relation like template=List[T] and actual=object...
Function is_complete_type Is a type complete?
Function is_same_constraint Undocumented
Function is_same_constraints Undocumented
Function is_similar_constraints Check that two lists of constraints have similar structure.
Function merge_with_any Transform a constraint target into a union with given Any type.
Function neg_op Map SubtypeOf to SupertypeOf and vice versa.
Function select_trivial Select only those lists where each item is a constraint against Any.
Function simplify_away_incomplete_types Undocumented
Constant SUBTYPE_OF Undocumented
Constant SUPERTYPE_OF Undocumented
Function _infer_constraints Undocumented
Function _is_similar_constraints Check that every constraint in the first list has a similar one in the second.
def any_constraints(options: list[list[Constraint]|None], eager: bool) -> list[Constraint]: (source)

Deduce what we can from a collection of constraint lists. It's a given that at least one of the lists must be satisfied. A None element in the list of options represents an unsatisfiable constraint and is ignored. Ignore empty constraint lists if eager is true -- they are always trivially satisfiable.

def build_constraints_for_unpack(mapped: tuple[Type, ...], mapped_prefix_len: int|None, mapped_suffix_len: int|None, template: tuple[Type, ...], template_prefix_len: int, template_suffix_len: int, direction: int) -> tuple[list[Constraint], tuple[Type, ...], tuple[Type, ...]]: (source)

Undocumented

def filter_satisfiable(option: list[Constraint]|None) -> list[Constraint]|None: (source)

Keep only constraints that can possibly be satisfied. Currently, we filter out constraints where target is not a subtype of the upper bound. Since those can be never satisfied. We may add more cases in future if it improves type inference.

def find_and_build_constraints_for_unpack(mapped: tuple[Type, ...], template: tuple[Type, ...], direction: int) -> tuple[list[Constraint], tuple[Type, ...], tuple[Type, ...]]: (source)

Undocumented

def find_matching_overload_item(overloaded: Overloaded, template: CallableType) -> CallableType: (source)

Disambiguate overload item against a template.

def find_matching_overload_items(overloaded: Overloaded, template: CallableType) -> list[CallableType]: (source)

Like find_matching_overload_item, but return all matches, not just the first.

def handle_recursive_union(template: UnionType, actual: Type, direction: int) -> list[Constraint]: (source)

Undocumented

def infer_constraints(template: Type, actual: Type, direction: int) -> list[Constraint]: (source)

Infer type constraints. Match a template type, which may contain type variable references, recursively against a type which does not contain (the same) type variable references. The result is a list of type constrains of form 'T is a supertype/subtype of x', where T is a type variable present in the template and x is a type without reference to type variables present in the template. Assume T and S are type variables. Now the following results can be calculated (read as '(template, actual) --> result'): (T, X) --> T :> X (X[T], X[Y]) --> T <: Y and T :> Y ((T, T), (X, Y)) --> T :> X and T :> Y ((T, S), (X, Y)) --> T :> X and S :> Y (X[T], Any) --> T <: Any and T :> Any The constraints are represented as Constraint objects.

def infer_constraints_for_callable(callee: CallableType, arg_types: Sequence[Type|None], arg_kinds: list[ArgKind], formal_to_actual: list[list[int]], context: ArgumentInferContext) -> list[Constraint]: (source)

Infer type variable constraints for a callable and actual arguments. Return a list of constraints.

def infer_constraints_if_possible(template: Type, actual: Type, direction: int) -> list[Constraint]|None: (source)

Like infer_constraints, but return None if the input relation is known to be unsatisfiable, for example if template=List[T] and actual=int. (In this case infer_constraints would return [], just like it would for an automatically satisfied relation like template=List[T] and actual=object.)

def is_complete_type(typ: Type) -> bool: (source)

Is a type complete? A complete doesn't have uninhabited type components or (when not in strict optional mode) None components.

def is_same_constraint(c1: Constraint, c2: Constraint) -> bool: (source)

Undocumented

def is_same_constraints(x: list[Constraint], y: list[Constraint]) -> bool: (source)

Undocumented

def is_similar_constraints(x: list[Constraint], y: list[Constraint]) -> bool: (source)

Check that two lists of constraints have similar structure. This means that each list has same type variable plus direction pairs (i.e we ignore the target). Except for constraints where target is Any type, there we ignore direction as well.

def merge_with_any(constraint: Constraint) -> Constraint: (source)

Transform a constraint target into a union with given Any type.

def neg_op(op: int) -> int: (source)

Map SubtypeOf to SupertypeOf and vice versa.

def select_trivial(options: Sequence[list[Constraint]|None]) -> list[list[Constraint]]: (source)

Select only those lists where each item is a constraint against Any.

def simplify_away_incomplete_types(types: Iterable[Type]) -> list[Type]: (source)

Undocumented

SUBTYPE_OF: int = (source)

Undocumented

Value
0
SUPERTYPE_OF: int = (source)

Undocumented

Value
1
def _infer_constraints(template: Type, actual: Type, direction: int) -> list[Constraint]: (source)

Undocumented

def _is_similar_constraints(x: list[Constraint], y: list[Constraint]) -> bool: (source)

Check that every constraint in the first list has a similar one in the second. See docstring above for definition of similarity.