module documentation
(source)

Mypy type checker.
Class ​Checker​Scope No class docstring; 0/1 instance variable, 2/8 methods documented
Class ​Disjoint​Dict An variation of the union-find algorithm/data structure where instead of keeping track of just disjoint sets, we keep track of disjoint dicts -- keep track of multiple Set[Key] -> Set[Value] mappings, where each mapping's keys are guaranteed to be disjoint.
Class ​Nothing​Seeker Find any <nothing> types resulting from failed (ambiguous) type inference.
Class ​Set​Nothing​To​Any Replace all ambiguous <nothing> types with Any (to avoid spurious extra errors).
Class ​Type​Checker Mypy type checker.
Class ​Type​Transform​Visitor Undocumented
Function and​_conditional​_maps Calculate what information we can learn from the truth of (e1 and e2) in terms of the information that we can learn from the truth of e1 and the truth of e2.
Function are​_argument​_counts​_overlapping Can a single call match both t and s, based just on positional argument counts?
Function builtin​_item​_type Get the item type of a builtin container.
Function collapse​_walrus If an expression is an AssignmentExpr, pull out the assignment target.
Function conditional​_type​_map Takes in an expression, the current type of the expression, and a proposed type of that expression.
Function convert​_to​_typetype Undocumented
Function detach​_callable Ensures that the callable's type variables are 'detached' and independent of the context.
Function expand​_func Undocumented
Function flatten Flatten a nested sequence of tuples/lists into one list of nodes.
Function flatten​_types Flatten a nested sequence of tuples into one list of nodes.
Function gen​_unique​_name Generate a name that does not appear in table by appending numbers to base.
Function get​_isinstance​_type Undocumented
Function group​_comparison​_operands Group a series of comparison operands together chained by any operand in the 'operators_to_group' set. All other pairwise operands are kept in groups of size 2.
Function has​_bool​_item Return True if type is 'bool' or a union with a 'bool' item.
Function infer​_operator​_assignment​_method Determine if operator assignment on given value type is in-place, and the method name.
Function is​_false​_literal Returns true if this expression is the 'False' literal/keyword.
Function is​_literal​_enum Returns true if this expression (with the given type context) is an Enum literal.
Function is​_literal​_none Returns true if this expression is the 'None' literal/keyword.
Function is​_literal​_not​_implemented Undocumented
Function is​_more​_general​_arg​_prefix Does t have wider arguments than s?
Function is​_node​_static Find out if a node describes a static function method.
Function is​_overlapping​_types​_no​_promote Undocumented
Function is​_private Check if node is private to class definition.
Function is​_same​_arg​_prefix Undocumented
Function is​_static Undocumented
Function is​_subtype​_no​_promote Undocumented
Function is​_true​_literal Returns true if this expression is the 'True' literal/keyword.
Function is​_typed​_callable Undocumented
Function is​_unsafe​_overlapping​_overload​_signatures Check if two overloaded signatures are unsafely overlapping or partially overlapping.
Function is​_untyped​_decorator Undocumented
Function is​_valid​_inferred​_type Is an inferred type valid?
Function or​_conditional​_maps Calculate what information we can learn from the truth of (e1 or e2) in terms of the information that we can learn from the truth of e1 and the truth of e2.
Function overload​_can​_never​_match Check if the 'other' method can never be matched due to 'signature'.
Function reduce​_conditional​_maps Reduces a list containing pairs of if/else TypeMaps into a single pair.
Constant DEFAULT​_LAST​_PASS Undocumented
Constant T Undocumented
Variable ​Deferred​Node Undocumented
Variable ​Deferred​Node​Type Undocumented
Variable ​Fine​Grained​Deferred​Node Undocumented
Variable ​Fine​Grained​Deferred​Node​Type Undocumented
Variable ​Partial​Type​Scope Undocumented
Variable ​TKey Undocumented
Variable ​TValue Undocumented
Variable ​Type​Map Undocumented
Variable ​Type​Range Undocumented
def and_conditional_maps(m1, m2): (source)
Calculate what information we can learn from the truth of (e1 and e2) in terms of the information that we can learn from the truth of e1 and the truth of e2.
Parameters
m1:TypeMapUndocumented
m2:TypeMapUndocumented
Returns
TypeMapUndocumented
def are_argument_counts_overlapping(t, s): (source)
Can a single call match both t and s, based just on positional argument counts?
Parameters
t:CallableTypeUndocumented
s:CallableTypeUndocumented
Returns
boolUndocumented
def builtin_item_type(tp): (source)

Get the item type of a builtin container.

If 'tp' is not one of the built containers (these includes NamedTuple and TypedDict) or if the container is not parameterized (like List or List[Any]) return None. This function is used to narrow optional types in situations like this:

x: Optional[int] if x in (1, 2, 3):

x + 42 # OK

Note: this is only OK for built-in containers, where we know the behavior of __contains__.

Parameters
tp:TypeUndocumented
Returns
Optional[Type]Undocumented
def collapse_walrus(e): (source)

If an expression is an AssignmentExpr, pull out the assignment target.

We don't make any attempt to pull out all the targets in code like x := (y := z). We could support narrowing those if that sort of code turns out to be common.

Parameters
e:ExpressionUndocumented
Returns
ExpressionUndocumented
def conditional_type_map(expr, current_type, proposed_type_ranges): (source)

Takes in an expression, the current type of the expression, and a proposed type of that expression.

Returns a 2-tuple: The first element is a map from the expression to the proposed type, if the expression can be the proposed type. The second element is a map from the expression to the type it would hold if it was not the proposed type, if any. None means bot, {} means top

Parameters
expr:ExpressionUndocumented
current​_type:Optional[Type]Undocumented
proposed​_type​_ranges:Optional[List[TypeRange]]Undocumented
Returns
Tuple[TypeMap, TypeMap]Undocumented
def convert_to_typetype(type_map): (source)

Undocumented

Parameters
type​_map:TypeMapUndocumented
Returns
TypeMapUndocumented
def detach_callable(typ): (source)

Ensures that the callable's type variables are 'detached' and independent of the context.

A callable normally keeps track of the type variables it uses within its 'variables' field. However, if the callable is from a method and that method is using a class type variable, the callable will not keep track of that type variable since it belongs to the class.

This function will traverse the callable and find all used type vars and add them to the variables field if it isn't already present.

The caller can then unify on all type variables whether or not the callable is originally from a class or not.

Parameters
typ:CallableTypeUndocumented
Returns
CallableTypeUndocumented
def expand_func(defn, map): (source)

Undocumented

Parameters
defn:FuncItemUndocumented
map:Dict[TypeVarId, Type]Undocumented
Returns
FuncItemUndocumented
def flatten(t): (source)
Flatten a nested sequence of tuples/lists into one list of nodes.
Parameters
t:ExpressionUndocumented
Returns
List[Expression]Undocumented
def flatten_types(t): (source)
Flatten a nested sequence of tuples into one list of nodes.
Parameters
t:TypeUndocumented
Returns
List[Type]Undocumented
def gen_unique_name(base, table): (source)
Generate a name that does not appear in table by appending numbers to base.
Parameters
base:strUndocumented
table:SymbolTableUndocumented
Returns
strUndocumented
def get_isinstance_type(expr, type_map): (source)

Undocumented

Parameters
expr:ExpressionUndocumented
type​_map:Dict[Expression, Type]Undocumented
Returns
Optional[List[TypeRange]]Undocumented
def group_comparison_operands(pairwise_comparisons, operand_to_literal_hash, operators_to_group): (source)

Group a series of comparison operands together chained by any operand in the 'operators_to_group' set. All other pairwise operands are kept in groups of size 2.

For example, suppose we have the input comparison expression:

x0 == x1 == x2 < x3 < x4 is x5 is x6 is not x7 is not x8

If we get these expressions in a pairwise way (e.g. by calling ComparisionExpr's 'pairwise()' method), we get the following as input:

[('==', x0, x1), ('==', x1, x2), ('<', x2, x3), ('<', x3, x4),
('is', x4, x5), ('is', x5, x6), ('is not', x6, x7), ('is not', x7, x8)]

If operators_to_group is the set {'==', 'is'}, this function will produce the following "simplified operator list":

[("==", [0, 1, 2]), ("<", [2, 3]), ("<", [3, 4]),
("is", [4, 5, 6]), ("is not", [6, 7]), ("is not", [7, 8])]

Note that (a) we yield indices to the operands rather then the operand expressions themselves and that (b) operands used in a consecutive chain of '==' or 'is' are grouped together.

If two of these chains happen to contain operands with the same underlying literal hash (e.g. are assignable and correspond to the same expression), we combine those chains together. For example, if we had:

same == x < y == same

...and if 'operand_to_literal_hash' contained the same values for the indices 0 and 3, we'd produce the following output:

[("==", [0, 1, 2, 3]), ("<", [1, 2])]

But if the 'operand_to_literal_hash' did not contain an entry, we'd instead default to returning:

[("==", [0, 1]), ("<", [1, 2]), ("==", [2, 3])]

This function is currently only used to assist with type-narrowing refinements and is extracted out to a helper function so we can unit test it.

Parameters
pairwise​_comparisons:Iterable[Tuple[str, Expression, Expression]]Undocumented
operand​_to​_literal​_hash:Mapping[int, Key]Undocumented
operators​_to​_group:Set[str]Undocumented
Returns
List[Tuple[str, List[int]]]Undocumented
def has_bool_item(typ): (source)
Return True if type is 'bool' or a union with a 'bool' item.
Parameters
typ:ProperTypeUndocumented
Returns
boolUndocumented
def infer_operator_assignment_method(typ, operator): (source)

Determine if operator assignment on given value type is in-place, and the method name.

For example, if operator is '+', return (True, '__iadd__') or (False, '__add__') depending on which method is supported by the type.

Parameters
typ:TypeUndocumented
operator:strUndocumented
Returns
Tuple[bool, str]Undocumented
def is_false_literal(n): (source)
Returns true if this expression is the 'False' literal/keyword.
Parameters
n:ExpressionUndocumented
Returns
boolUndocumented
def is_literal_enum(type_map, n): (source)

Returns true if this expression (with the given type context) is an Enum literal.

For example, if we had an enum:

class Foo(Enum):
A = 1 B = 2

...and if the expression 'Foo' referred to that enum within the current type context, then the expression 'Foo.A' would be a literal enum. However, if we did 'a = Foo.A', then the variable 'a' would not be a literal enum.

We occasionally special-case expressions like 'Foo.A' and treat them as a single primitive unit for the same reasons we sometimes treat 'True', 'False', or 'None' as a single primitive unit.

Parameters
type​_map:Mapping[Expression, Type]Undocumented
n:ExpressionUndocumented
Returns
boolUndocumented
def is_literal_none(n): (source)
Returns true if this expression is the 'None' literal/keyword.
Parameters
n:ExpressionUndocumented
Returns
boolUndocumented
def is_literal_not_implemented(n): (source)

Undocumented

Parameters
n:ExpressionUndocumented
Returns
boolUndocumented
def is_more_general_arg_prefix(t, s): (source)
Does t have wider arguments than s?
Parameters
t:FunctionLikeUndocumented
s:FunctionLikeUndocumented
Returns
boolUndocumented
def is_node_static(node): (source)
Find out if a node describes a static function method.
Parameters
node:Optional[Node]Undocumented
Returns
Optional[bool]Undocumented
def is_overlapping_types_no_promote(left, right): (source)

Undocumented

Parameters
left:TypeUndocumented
right:TypeUndocumented
Returns
boolUndocumented
def is_private(node_name): (source)
Check if node is private to class definition.
Parameters
node​_name:strUndocumented
Returns
boolUndocumented
def is_same_arg_prefix(t, s): (source)

Undocumented

Parameters
t:CallableTypeUndocumented
s:CallableTypeUndocumented
Returns
boolUndocumented
def is_static(func): (source)

Undocumented

Parameters
func:Union[FuncBase, Decorator]Undocumented
Returns
boolUndocumented
def is_subtype_no_promote(left, right): (source)

Undocumented

Parameters
left:TypeUndocumented
right:TypeUndocumented
Returns
boolUndocumented
def is_true_literal(n): (source)
Returns true if this expression is the 'True' literal/keyword.
Parameters
n:ExpressionUndocumented
Returns
boolUndocumented
def is_typed_callable(c): (source)

Undocumented

Parameters
c:Optional[Type]Undocumented
Returns
boolUndocumented
def is_unsafe_overlapping_overload_signatures(signature, other): (source)

Check if two overloaded signatures are unsafely overlapping or partially overlapping.

We consider two functions 's' and 't' to be unsafely overlapping if both of the following are true:

  1. s's parameters are all more precise or partially overlapping with t's
  2. s's return type is NOT a subtype of t's.

Assumes that 'signature' appears earlier in the list of overload alternatives then 'other' and that their argument counts are overlapping.

Parameters
signature:CallableTypeUndocumented
other:CallableTypeUndocumented
Returns
boolUndocumented
def is_untyped_decorator(typ): (source)

Undocumented

Parameters
typ:Optional[Type]Undocumented
Returns
boolUndocumented
def is_valid_inferred_type(typ): (source)

Is an inferred type valid?

Examples of invalid types include the None type or List[<uninhabited>].

When not doing strict Optional checking, all types containing None are invalid. When doing strict Optional checking, only None and types that are incompletely defined (i.e. contain UninhabitedType) are invalid.

Parameters
typ:TypeUndocumented
Returns
boolUndocumented
def or_conditional_maps(m1, m2): (source)
Calculate what information we can learn from the truth of (e1 or e2) in terms of the information that we can learn from the truth of e1 and the truth of e2.
Parameters
m1:TypeMapUndocumented
m2:TypeMapUndocumented
Returns
TypeMapUndocumented
def overload_can_never_match(signature, other): (source)

Check if the 'other' method can never be matched due to 'signature'.

This can happen if signature's parameters are all strictly broader then other's parameters.

Assumes that both signatures have overlapping argument counts.

Parameters
signature:CallableTypeUndocumented
other:CallableTypeUndocumented
Returns
boolUndocumented
def reduce_conditional_maps(type_maps): (source)

Reduces a list containing pairs of if/else TypeMaps into a single pair.

We "and" together all of the if TypeMaps and "or" together the else TypeMaps. So for example, if we had the input:

[
({x: TypeIfX, shared: TypeIfShared1}, {x: TypeElseX, shared: TypeElseShared1}), ({y: TypeIfY, shared: TypeIfShared2}, {y: TypeElseY, shared: TypeElseShared2}),

]

...we'd return the output:

(
{x: TypeIfX, y: TypeIfY, shared: PseudoIntersection[TypeIfShared1, TypeIfShared2]}, {shared: Union[TypeElseShared1, TypeElseShared2]},

)

...where "PseudoIntersection[X, Y] == Y" because mypy actually doesn't understand intersections yet, so we settle for just arbitrarily picking the right expr's type.

We only retain the shared expression in the 'else' case because we don't actually know whether x was refined or y was refined -- only just that one of the two was refined.

Parameters
type​_maps:List[Tuple[TypeMap, TypeMap]]Undocumented
Returns
Tuple[TypeMap, TypeMap]Undocumented
DEFAULT_LAST_PASS: int = (source)

Undocumented

Value
1

Undocumented

Value
TypeVar('T')
DeferredNode = (source)

Undocumented

DeferredNodeType: _TypeAlias = (source)

Undocumented

FineGrainedDeferredNode = (source)

Undocumented

FineGrainedDeferredNodeType: _TypeAlias = (source)

Undocumented

PartialTypeScope = (source)

Undocumented

TKey = (source)

Undocumented

TValue = (source)

Undocumented

TypeMap: _TypeAlias = (source)

Undocumented

TypeRange = (source)

Undocumented