Class | CheckerScope |
No class docstring; 0/1 instance variable, 2/8 methods documented |
Class | DisjointDict |
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 | NothingSeeker |
Find any <nothing> types resulting from failed (ambiguous) type inference. |
Class | SetNothingToAny |
Replace all ambiguous <nothing> types with Any (to avoid spurious extra errors). |
Class | TypeChecker |
Mypy type checker. |
Class | TypeTransformVisitor |
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 | DeferredNode |
Undocumented |
Variable | DeferredNodeType |
Undocumented |
Variable | FineGrainedDeferredNode |
Undocumented |
Variable | FineGrainedDeferredNodeType |
Undocumented |
Variable | PartialTypeScope |
Undocumented |
Variable | TKey |
Undocumented |
Variable | TValue |
Undocumented |
Variable | TypeMap |
Undocumented |
Variable | TypeRange |
Undocumented |
Parameters | |
t:CallableType | Undocumented |
s:CallableType | Undocumented |
Returns | |
bool | Undocumented |
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:Type | Undocumented |
Returns | |
Optional[ | Undocumented |
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:Expression | Undocumented |
Returns | |
Expression | Undocumented |
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:Expression | Undocumented |
current_type:Optional[ | Undocumented |
proposed_type_ranges:Optional[ | Undocumented |
Returns | |
Tuple[ | Undocumented |
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:CallableType | Undocumented |
Returns | |
CallableType | Undocumented |
Parameters | |
t:Expression | Undocumented |
Returns | |
List[ | Undocumented |
Parameters | |
base:str | Undocumented |
table:SymbolTable | Undocumented |
Returns | |
str | Undocumented |
Undocumented
Parameters | |
expr:Expression | Undocumented |
type_map:Dict[ | Undocumented |
Returns | |
Optional[ | Undocumented |
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[ | Undocumented |
operand_to_literal_hash:Mapping[ | Undocumented |
operators_to_group:Set[ | Undocumented |
Returns | |
List[ | Undocumented |
Parameters | |
typ:ProperType | Undocumented |
Returns | |
bool | Undocumented |
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:Type | Undocumented |
operator:str | Undocumented |
Returns | |
Tuple[ | Undocumented |
Parameters | |
n:Expression | Undocumented |
Returns | |
bool | Undocumented |
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[ | Undocumented |
n:Expression | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
n:Expression | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
t:FunctionLike | Undocumented |
s:FunctionLike | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
node:Optional[ | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
node_name:str | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
n:Expression | Undocumented |
Returns | |
bool | Undocumented |
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:
Assumes that 'signature' appears earlier in the list of overload alternatives then 'other' and that their argument counts are overlapping.
Parameters | |
signature:CallableType | Undocumented |
other:CallableType | Undocumented |
Returns | |
bool | Undocumented |
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:Type | Undocumented |
Returns | |
bool | Undocumented |
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:CallableType | Undocumented |
other:CallableType | Undocumented |
Returns | |
bool | Undocumented |
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[ | Undocumented |
Returns | |
Tuple[ | Undocumented |