module documentation

Expression type checker. This file is conceptually part of TypeChecker.

Class ArgInferSecondPassQuery Query whether an argument type should be inferred in the second pass.
Class ExpressionChecker Expression type checker.
Class HasAnyType Undocumented
Class HasErasedComponentsQuery Visitor for querying whether a type has an erased component.
Class HasTypeVarQuery Visitor for querying whether a type has a type variable component.
Class HasUninhabitedComponentsQuery Visitor for querying whether a type has an UninhabitedType component.
Exception Finished Raised if we can terminate overload argument check early (no match).
Exception TooManyUnions Indicates that we need to stop splitting unions in an attempt to match an overload in order to save performance.
Function all_same_types Undocumented
Function allow_fast_container_literal Undocumented
Function any_causes_overload_ambiguity May an argument containing 'Any' cause ambiguous result type on call to overloaded function?
Function arg_approximate_similarity Return if caller argument (actual) is roughly compatible with signature arg (formal).
Function extract_refexpr_names Recursively extracts all module references from a reference expression.
Function get_partial_instance_type Undocumented
Function has_any_type Whether t contains an Any type
Function has_bytes_component Is this one of builtin byte types, or a union that contains it?
Function has_coroutine_decorator Whether t came from a function decorated with `@coroutine`.
Function has_erased_component Undocumented
Function has_uninhabited_component Undocumented
Function is_async_def Whether t came from a function defined using `async def`.
Function is_duplicate_mapping Undocumented
Function is_expr_literal_type Returns 'true' if the given node is a Literal
Function is_non_empty_tuple Undocumented
Function is_operator_method Undocumented
Function merge_typevars_in_callables_by_name Takes all the typevars present in the callables and 'combines' the ones with the same name.
Function replace_callable_return_type Return a copy of a callable type with a different return type.
Function try_getting_literal If possible, get a more precise literal type for a given type.
Function type_info_from_type Gets the TypeInfo for a type, indirecting through things like type variables and tuples.
Constant MAX_UNIONS Undocumented
Constant OVERLAPPING_BYTES_ALLOWLIST Undocumented
Constant OVERLAPPING_TYPES_ALLOWLIST Undocumented
Type Alias ArgChecker Undocumented
def all_same_types(types: list[Type]) -> bool: (source)

Undocumented

def allow_fast_container_literal(t: Type) -> bool: (source)

Undocumented

def any_causes_overload_ambiguity(items: list[CallableType], return_types: list[Type], arg_types: list[Type], arg_kinds: list[ArgKind], arg_names: Sequence[str|None]|None) -> bool: (source)

May an argument containing 'Any' cause ambiguous result type on call to overloaded function? Note that this sometimes returns True even if there is no ambiguity, since a correct implementation would be complex (and the call would be imprecisely typed due to Any types anyway). Args: items: Overload items matching the actual arguments arg_types: Actual argument types arg_kinds: Actual argument kinds arg_names: Actual argument names

def arg_approximate_similarity(actual: Type, formal: Type) -> bool: (source)

Return if caller argument (actual) is roughly compatible with signature arg (formal). This function is deliberately loose and will report two types are similar as long as their "shapes" are plausibly the same. This is useful when we're doing error reporting: for example, if we're trying to select an overload alternative and there's no exact match, we can use this function to help us identify which alternative the user might have *meant* to match.

def extract_refexpr_names(expr: RefExpr) -> set[str]: (source)

Recursively extracts all module references from a reference expression. Note that currently, the only two subclasses of RefExpr are NameExpr and MemberExpr.

def get_partial_instance_type(t: Type|None) -> PartialType|None: (source)

Undocumented

def has_any_type(t: Type, ignore_in_type_obj: bool = False) -> bool: (source)

Whether t contains an Any type

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

Is this one of builtin byte types, or a union that contains it?

def has_coroutine_decorator(t: Type) -> bool: (source)

Whether t came from a function decorated with `@coroutine`.

def has_erased_component(t: Type|None) -> bool: (source)

Undocumented

def has_uninhabited_component(t: Type|None) -> bool: (source)

Undocumented

def is_async_def(t: Type) -> bool: (source)

Whether t came from a function defined using `async def`.

def is_duplicate_mapping(mapping: list[int], actual_types: list[Type], actual_kinds: list[ArgKind]) -> bool: (source)

Undocumented

def is_expr_literal_type(node: Expression) -> bool: (source)

Returns 'true' if the given node is a Literal

def is_non_empty_tuple(t: Type) -> bool: (source)

Undocumented

def is_operator_method(fullname: str|None) -> bool: (source)

Undocumented

def merge_typevars_in_callables_by_name(callables: Sequence[CallableType]) -> tuple[list[CallableType], list[TypeVarType]]: (source)

Takes all the typevars present in the callables and 'combines' the ones with the same name. For example, suppose we have two callables with signatures "f(x: T, y: S) -> T" and "f(x: List[Tuple[T, S]]) -> Tuple[T, S]". Both callables use typevars named "T" and "S", but we treat them as distinct, unrelated typevars. (E.g. they could both have distinct ids.) If we pass in both callables into this function, it returns a list containing two new callables that are identical in signature, but use the same underlying TypeVarType for T and S. This is useful if we want to take the output lists and "merge" them into one callable in some way -- for example, when unioning together overloads. Returns both the new list of callables and a list of all distinct TypeVarType objects used.

def replace_callable_return_type(c: CallableType, new_ret_type: Type) -> CallableType: (source)

Return a copy of a callable type with a different return type.

def try_getting_literal(typ: Type) -> ProperType: (source)

If possible, get a more precise literal type for a given type.

def type_info_from_type(typ: Type) -> TypeInfo|None: (source)

Gets the TypeInfo for a type, indirecting through things like type variables and tuples.

MAX_UNIONS: int = (source)

Undocumented

Value
5
OVERLAPPING_BYTES_ALLOWLIST: set[str] = (source)

Undocumented

Value
set(['builtins.bytes', 'builtins.bytearray', 'builtins.memoryview'])
OVERLAPPING_TYPES_ALLOWLIST: list[str] = (source)

Undocumented

Value
['builtins.set',
 'builtins.frozenset',
 'typing.KeysView',
 'typing.ItemsView',
 'builtins._dict_keys',
 'builtins._dict_items',
 '_collections_abc.dict_keys',
...
ArgChecker: _TypeAlias = (source)

Undocumented

Value
Callable[[Type,
          Type,
          ArgKind,
          Type,
          int,
          int,
          CallableType,
...