class ExpressionChecker(ExpressionVisitor[Type]): (source)
Expression type checker.
This class works closely together with checker.TypeChecker.
Method | __init__ |
Construct an expression type checker. |
Method | accept |
Type check a node in the given type context. If allow_none_return is True and this expression is a call, allow it to return None. This applies only to this expression and not any subexpressions. |
Method | alias_type_in_runtime_context |
Get type of a type alias (could be generic) in a runtime expression. |
Method | always_returns_none |
Check if node refers to something explicitly annotated as only returning None. |
Method | analyze_cond_branch |
Undocumented |
Method | analyze_external_member_access |
Analyse member access that is external, i.e. it cannot refer to private definitions. Return the result type. |
Method | analyze_ordinary_member_access |
Analyse member expression or member lvalue. |
Method | analyze_ref_expr |
Undocumented |
Method | analyze_type_type_callee |
Analyze the callee X in X(...) where X is Type[item]. |
Method | analyze_var_ref |
Undocumented |
Method | apply_function_plugin |
Use special case logic to infer the return type of a specific named function/method. |
Method | apply_function_signature_hook |
Apply a plugin hook that may infer a more precise signature for a function. |
Method | apply_generic_arguments |
Simple wrapper around mypy.applytype.apply_generic_arguments. |
Method | apply_inferred_arguments |
Apply inferred values of type arguments to a generic function. |
Method | apply_method_signature_hook |
Apply a plugin hook that may infer a more precise signature for a method. |
Method | apply_signature_hook |
Helper to apply a signature hook for either a function or method |
Method | apply_type_arguments_to_callable |
Apply type arguments to a generic callable type coming from a type object. |
Method | argument_infer_context |
Undocumented |
Method | bool_type |
Return instance type 'bool'. |
Method | check_any_type_call |
Undocumented |
Method | check_arg |
Check the type of a single argument in a call. |
Method | check_argument_count |
Check that there is a value for all required arguments to a function. |
Method | check_argument_types |
Check argument types against a callable type. |
Method | check_awaitable_expr |
Check the argument to await and extract the type of value. |
Method | check_boolean_op |
Type check a boolean operation ('and' or 'or'). |
Method | check_call |
Type check a call. |
Method | check_call_expr_with_callee_type |
Type check call expression. |
Method | check_callable_call |
Type check a call that targets a callable value. |
Method | check_for_comp |
Check the for_comp part of comprehensions. That is the part from 'for': ... for x in y if z |
Method | check_for_extra_actual_arguments |
Check for extra actual arguments. |
Method | check_generator_or_comprehension |
Type check a generator expression or a list comprehension. |
Method | check_list_multiply |
Type check an expression of form '[...] * e'. |
Method | check_lst_expr |
Undocumented |
Method | check_method_call |
Type check a call to a method with the given name and type on an object. |
Method | check_method_call_by_name |
Type check a call to a named method on an object. |
Method | check_op |
Type check a binary operation which maps to a method call. |
Method | check_op_reversible |
Undocumented |
Method | check_overload_call |
Checks a call to an overloaded function. |
Method | check_protocol_issubclass |
Undocumented |
Method | check_runtime_protocol_test |
Undocumented |
Method | check_str_format_call |
More precise type checking for str.format() calls on literals. |
Method | check_typeddict_call |
Undocumented |
Method | check_typeddict_call_with_dict |
Undocumented |
Method | check_typeddict_call_with_kwargs |
Undocumented |
Method | check_union_call |
Undocumented |
Method | check_union_call_expr |
"Type check calling a member expression where the base type is a union. |
Method | check_union_method_call_by_name |
Type check a call to a named method on an object with union type. |
Method | combine_function_signatures |
Accepts a list of function signatures and attempts to combine them together into a new CallableType consisting of the union of all of the given arguments and return types. |
Method | concat_tuples |
Concatenate two fixed length tuples. |
Method | dangerous_comparison |
Check for dangerous non-overlapping comparisons like 42 == 'no'. |
Method | defn_returns_none |
Check if defn can _only_ return None. |
Method | erased_signature_similarity |
Determine whether arguments could match the signature at runtime, after erasing types. |
Method | fast_container_type |
Fast path to determine the type of a list or set literal, based on the list of entries. This mostly impacts large module-level constant definitions. |
Method | fast_dict_type |
Fast path to determine the type of a dict literal, based on the list of entries. This mostly impacts large module-level constant definitions. |
Method | find_partial_type_ref_fast_path |
If expression has a partial generic type, return it without additional checks. |
Method | find_typeddict_context |
Undocumented |
Method | get_arg_infer_passes |
Return pass numbers for args for two-pass argument type inference. |
Method | get_operator_method |
Undocumented |
Method | get_partial_self_var |
Get variable node for a partial self attribute. |
Method | get_partial_var |
Undocumented |
Method | get_reverse_op_method |
Undocumented |
Method | has_member |
Does type have member with the given name? |
Method | infer_arg_types_in_context |
Infer argument expression types using a callable type as context. |
Method | infer_arg_types_in_empty_context |
Infer argument expression types in an empty context. |
Method | infer_function_type_arguments |
Infer the type arguments for a generic callee type. |
Method | infer_function_type_arguments_pass2 |
Perform second pass of generic function type argument inference. |
Method | infer_function_type_arguments_using_context |
Unify callable return type to type context to infer type vars. |
Method | infer_lambda_type_using_context |
Try to infer lambda expression type using context. |
Method | infer_literal_expr_type |
Analyzes the given literal expression and determines if we should be inferring an Instance type, a Literal[...] type, or an Instance that remembers the original literal. We... |
Method | infer_overload_return_type |
Attempts to find the first matching callable from the given list. |
Method | is_literal_context |
Undocumented |
Method | is_valid_keyword_var_arg |
Is a type valid as a **kwargs argument? |
Method | is_valid_var_arg |
Is a type valid as a *args argument? |
Method | match_typeddict_call_with_dict |
Undocumented |
Method | method_fullname |
Convert a method name to a fully qualified name, based on the type of the object that it is invoked on. Return None if the name of object_type cannot be determined. |
Method | named_type |
Return an instance type with type given by the name and no type arguments. Alias for TypeChecker.named_type. |
Method | narrow_type_from_binder |
Narrow down a known type of expression using information in conditional type binder. |
Method | nonliteral_tuple_index_helper |
Undocumented |
Method | not_ready_callback |
Called when we can't infer the type of a variable because it's not ready yet. |
Method | object_type |
Return instance type 'object'. |
Method | overload_erased_call_targets |
Returns a list of all targets that match the caller after erasing types. |
Method | plausible_overload_call_targets |
Returns all overload call targets that having matching argument counts. |
Method | real_union |
Undocumented |
Method | transform_callee_type |
Attempt to determine a more accurate signature for a method call. |
Method | try_getting_int_literals |
If the given expression or type corresponds to an int literal or a union of int literals, returns a list of the underlying ints. Otherwise, returns None. |
Method | try_infer_partial_type |
Try to make partial type precise from a call. |
Method | try_infer_partial_value_type_from_call |
Try to make partial type precise from a call such as 'x.append(y)'. |
Method | type_overrides_set |
Set _temporary_ type overrides for given expressions. |
Method | union_overload_result |
Accepts a list of overload signatures and attempts to match calls by destructuring the first union. |
Method | validate_typeddict_kwargs |
Undocumented |
Method | visit__promote_expr |
Undocumented |
Method | visit_assignment_expr |
Undocumented |
Method | visit_await_expr |
Undocumented |
Method | visit_backquote_expr |
Undocumented |
Method | visit_bytes_expr |
Type check a bytes literal (trivial). |
Method | visit_call_expr |
Type check a call expression. |
Method | visit_call_expr_inner |
Undocumented |
Method | visit_cast_expr |
Type check a cast expression. |
Method | visit_comparison_expr |
Type check a comparison expression. |
Method | visit_complex_expr |
Type check a complex literal. |
Method | visit_conditional_expr |
Undocumented |
Method | visit_dict_expr |
Type check a dict expression. |
Method | visit_dictionary_comprehension |
Type check a dictionary comprehension. |
Method | visit_ellipsis |
Type check '...'. |
Method | visit_enum_call_expr |
Undocumented |
Method | visit_enum_index_expr |
Undocumented |
Method | visit_float_expr |
Type check a float literal (trivial). |
Method | visit_generator_expr |
Undocumented |
Method | visit_index_expr |
Type check an index expression (base[index]). |
Method | visit_index_expr_helper |
Undocumented |
Method | visit_index_with_type |
Analyze type of an index expression for a given type of base expression. |
Method | visit_int_expr |
Type check an integer literal (trivial). |
Method | visit_lambda_expr |
Type check lambda expression. |
Method | visit_list_comprehension |
Undocumented |
Method | visit_list_expr |
Type check a list expression [...]. |
Method | visit_member_expr |
Visit member expression (of form e.id). |
Method | visit_name_expr |
Type check a name expression. |
Method | visit_namedtuple_expr |
Undocumented |
Method | visit_newtype_expr |
Undocumented |
Method | visit_op_expr |
Type check a binary operator expression. |
Method | visit_paramspec_expr |
Undocumented |
Method | visit_reveal_expr |
Type check a reveal_type expression. |
Method | visit_set_comprehension |
Undocumented |
Method | visit_set_expr |
Undocumented |
Method | visit_slice_expr |
Undocumented |
Method | visit_star_expr |
Undocumented |
Method | visit_str_expr |
Type check a string literal (trivial). |
Method | visit_super_expr |
Type check a super expression (non-lvalue). |
Method | visit_temp_node |
Undocumented |
Method | visit_tuple_expr |
Type check a tuple expression. |
Method | visit_tuple_slice_helper |
Undocumented |
Method | visit_type_alias_expr |
Right hand side of a type alias definition. |
Method | visit_type_application |
Type check a type application (expr[type, ...]). |
Method | visit_type_var_expr |
Undocumented |
Method | visit_typeddict_expr |
Undocumented |
Method | visit_typeddict_index_expr |
Undocumented |
Method | visit_unary_expr |
Type check an unary operation ('not', '-', '+' or '~'). |
Method | visit_unicode_expr |
Type check a unicode literal (trivial). |
Method | visit_yield_expr |
Undocumented |
Method | visit_yield_from_expr |
Undocumented |
Class Variable | container_args |
Undocumented |
Class Variable | item_args |
Undocumented |
Instance Variable | chk |
Undocumented |
Instance Variable | msg |
Undocumented |
Instance Variable | plugin |
Undocumented |
Instance Variable | strfrm_checker |
Undocumented |
Instance Variable | type_context |
Undocumented |
Instance Variable | type_overrides |
Undocumented |
Method | _super_arg_types |
Computes the types of the type and instance expressions in super(T, instance), or the implicit ones for zero-argument super() expressions. Returns a single type for the whole super expression when possible (for errors, anys), otherwise the pair of computed types. |
Parameters | |
chk:mypy.checker.TypeChecker | Undocumented |
msg:MessageBuilder | Undocumented |
plugin:Plugin | Undocumented |
Parameters | |
node:Expression | Undocumented |
type_context:Optional[ | Undocumented |
allow_none_return:bool | Undocumented |
always_allow_any:bool | Undocumented |
Returns | |
Type | Undocumented |
Get type of a type alias (could be generic) in a runtime expression.
Note that this function can be called only if the alias appears _not_ as a target of type application, which is treated separately in the visit_type_application method. Some examples where this method is called are casts and instantiation:
class LongName(Generic[T]): ... A = LongName[int]
x = A() y = cast(A, ...)
Parameters | |
alias:TypeAlias | Undocumented |
no_args:bool | Undocumented |
ctx:Context | Undocumented |
alias_definition:bool | Undocumented |
Returns | |
Type | Undocumented |
node
refers to something explicitly annotated as only returning None.Parameters | |
node:Expression | Undocumented |
Returns | |
bool | Undocumented |
Undocumented
Parameters | |
map:Optional[ | Undocumented |
node:Expression | Undocumented |
context:Optional[ | Undocumented |
allow_none_return:bool | Undocumented |
Returns | |
Type | Undocumented |
Parameters | |
e:MemberExpr | Undocumented |
is_lvalue:bool | Undocumented |
Returns | |
Type | Undocumented |
Analyze the callee X in X(...) where X is Type[item].
Return a Y that we can pass to check_call(Y, ...).
Parameters | |
item:ProperType | Undocumented |
context:Context | Undocumented |
Returns | |
Type | Undocumented |
Use special case logic to infer the return type of a specific named function/method.
Caller must ensure that a plugin hook exists. There are two different cases:
Return the inferred return type.
Parameters | |
callee:CallableType | Undocumented |
arg_kinds:List[ | Undocumented |
arg_types:List[ | Undocumented |
arg_names:Optional[ | Undocumented |
formal_to_actual:List[ | Undocumented |
args:List[ | Undocumented |
fullname:str | Undocumented |
object_type:Optional[ | Undocumented |
context:Context | Undocumented |
Returns | |
Type | Undocumented |
Parameters | |
callee:FunctionLike | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
context:Context | Undocumented |
arg_names:Optional[ | Undocumented |
signature_hook:Callable[ | Undocumented |
Returns | |
FunctionLike | Undocumented |
Parameters | |
callable:CallableType | Undocumented |
types:Sequence[ | Undocumented |
context:Context | Undocumented |
skip_unsatisfied:bool | Undocumented |
Returns | |
CallableType | Undocumented |
Apply inferred values of type arguments to a generic function.
Inferred_args contains the values of function type arguments.
Parameters | |
callee_type:CallableType | Undocumented |
inferred_args:Sequence[ | Undocumented |
context:Context | Undocumented |
Returns | |
CallableType | Undocumented |
Parameters | |
callee:FunctionLike | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
context:Context | Undocumented |
arg_names:Optional[ | Undocumented |
object_type:Type | Undocumented |
signature_hook:Callable[ | Undocumented |
Returns | |
FunctionLike | Undocumented |
Parameters | |
callee:FunctionLike | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
arg_names:Optional[ | Undocumented |
hook:Callable[ | Undocumented |
Returns | |
FunctionLike | Undocumented |
Apply type arguments to a generic callable type coming from a type object.
This will first perform type arguments count checks, report the error as needed, and return the correct kind of Any. As a special case this returns Any for non-callable types, because if type object type is not callable, then an error should be already reported.
Parameters | |
tp:Type | Undocumented |
args:Sequence[ | Undocumented |
ctx:Context | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
args:List[ | Undocumented |
callee:Type | Undocumented |
Returns | |
Tuple[ | Undocumented |
Parameters | |
caller_type:Type | Undocumented |
original_caller_type:Type | Undocumented |
caller_kind:ArgKind | Undocumented |
callee_type:Type | Undocumented |
n:int | Undocumented |
m:int | Undocumented |
callee:CallableType | Undocumented |
object_type:Optional[ | Undocumented |
context:Context | Undocumented |
outer_context:Context | Undocumented |
messages:MessageBuilder | Undocumented |
Check that there is a value for all required arguments to a function.
Also check that there are no duplicate values for arguments. Report found errors using 'messages' if it's not None. If 'messages' is given, 'context' must also be given.
Return False if there were any errors. Otherwise return True
Parameters | |
callee:CallableType | Undocumented |
actual_types:List[ | Undocumented |
actual_kinds:List[ | Undocumented |
actual_names:Optional[ | Undocumented |
formal_to_actual:List[ | Undocumented |
context:Optional[ | Undocumented |
messages:Optional[ | Undocumented |
Returns | |
bool | Undocumented |
Check argument types against a callable type.
Report errors if the argument types are not compatible.
The check_call docstring describes some of the arguments.
Parameters | |
arg_types:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
args:List[ | Undocumented |
callee:CallableType | Undocumented |
formal_to_actual:List[ | Undocumented |
context:Context | Undocumented |
messages:Optional[ | Undocumented |
check_arg:Optional[ | Undocumented |
object_type:Optional[ | Undocumented |
Check the argument to await
and extract the type of value.
Also used by async for
and async with
.
Parameters | |
t:Type | Undocumented |
ctx:Context | Undocumented |
msg:Union[ | Undocumented |
Returns | |
Type | Undocumented |
Type check a call.
Also infer type arguments if the callee is a generic function.
Return (result type, inferred callee type).
callee: type of the called value args: actual argument expressions arg_kinds: contains nodes.ARG_* constant for each argument in args
describing whether the argument is positional, *arg, etc.
context: current expression context, used for inference. arg_names: names of arguments (optional) callable_node: associate the inferred callable type to this node,
if specified
Parameters | |
callee:Type | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
context:Context | Undocumented |
arg_names:Optional[ | Undocumented |
callable_node:Optional[ | Undocumented |
arg_messages:Optional[ | Undocumented |
callable_name:Optional[ | Undocumented |
object_type:Optional[ | Undocumented |
Returns | |
Tuple[ | Undocumented |
Type check call expression.
The callee_type should be used as the type of callee expression. In particular, in case of a union type this can be a particular item of the union, so that we can apply plugin hooks to each item.
The 'member', 'callable_name' and 'object_type' are only used to call plugin hooks. If 'callable_name' is None but 'member' is not None (member call), try constructing 'callable_name' using 'object_type' (the base type on which the method is called), for example 'typing.Mapping.get'.
Parameters | |
callee_type:Type | Undocumented |
e:CallExpr | Undocumented |
callable_name:Optional[ | Undocumented |
object_type:Optional[ | Undocumented |
member:Optional[ | Undocumented |
Returns | |
Type | Undocumented |
Type check a call that targets a callable value.
See the docstring of check_call for more information.
Parameters | |
callee:CallableType | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
context:Context | Undocumented |
arg_names:Optional[ | Undocumented |
callable_node:Optional[ | Undocumented |
arg_messages:MessageBuilder | Undocumented |
callable_name:Optional[ | Undocumented |
object_type:Optional[ | Undocumented |
Returns | |
Tuple[ | Undocumented |
Check the for_comp part of comprehensions. That is the part from 'for': ... for x in y if z
Note: This adds the type information derived from the condlists to the current binder.
Parameters | |
e:Union[ | Undocumented |
Check for extra actual arguments.
Parameters | |
callee:CallableType | Undocumented |
actual_types:List[ | Undocumented |
actual_kinds:List[ | Undocumented |
actual_names:Optional[ | Undocumented |
all_actuals:List[ | Undocumented |
context:Context | Undocumented |
messages:Optional[ | Undocumented |
Returns | |
Tuple[ | Undocumented |
Parameters | |
gen:GeneratorExpr | Undocumented |
type_name:str | Undocumented |
id_for_messages:str | Undocumented |
additional_args:Optional[ | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
items:List[ | Undocumented |
fullname:str | Undocumented |
tag:str | Undocumented |
context:Context | Undocumented |
Returns | |
Type | Undocumented |
Type check a call to a method with the given name and type on an object.
Return tuple (result type, inferred method type).
Parameters | |
method_name:str | Undocumented |
base_type:Type | Undocumented |
method_type:Type | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
context:Context | Undocumented |
local_errors:Optional[ | Undocumented |
Returns | |
Tuple[ | Undocumented |
Type check a call to a named method on an object.
Return tuple (result type, inferred method type). The 'original_type' is used for error messages.
Parameters | |
method:str | Undocumented |
base_type:Type | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
context:Context | Undocumented |
local_errors:Optional[ | Undocumented |
original_type:Optional[ | Undocumented |
Returns | |
Tuple[ | Undocumented |
Type check a binary operation which maps to a method call.
Return tuple (result type, inferred operator method type).
Parameters | |
method:str | Undocumented |
base_type:Type | Undocumented |
arg:Expression | Undocumented |
context:Context | Undocumented |
allow_reverse:bool | Undocumented |
Returns | |
Tuple[ | Undocumented |
Undocumented
Parameters | |
op_name:str | Undocumented |
left_type:Type | Undocumented |
left_expr:Expression | Undocumented |
right_type:Type | Undocumented |
right_expr:Expression | Undocumented |
context:Context | Undocumented |
msg:MessageBuilder | Undocumented |
Returns | |
Tuple[ | Undocumented |
Parameters | |
callee:Overloaded | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
arg_names:Optional[ | Undocumented |
callable_name:Optional[ | Undocumented |
object_type:Optional[ | Undocumented |
context:Context | Undocumented |
arg_messages:MessageBuilder | Undocumented |
Returns | |
Tuple[ | Undocumented |
Undocumented
Parameters | |
callee:TypedDictType | Undocumented |
arg_kinds:List[ | Undocumented |
arg_names:Sequence[ | Undocumented |
args:List[ | Undocumented |
context:Context | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
callee:TypedDictType | Undocumented |
kwargs:DictExpr | Undocumented |
context:Context | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
callee:TypedDictType | Undocumented |
kwargs:OrderedDict[ | Undocumented |
context:Context | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
callee:UnionType | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
arg_names:Optional[ | Undocumented |
context:Context | Undocumented |
arg_messages:MessageBuilder | Undocumented |
Returns | |
Tuple[ | Undocumented |
Type check a call to a named method on an object with union type.
This essentially checks the call using check_method_call_by_name() for each union item and unions the result. We do this to allow plugins to act on individual union items.
Parameters | |
method:str | Undocumented |
base_type:UnionType | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
context:Context | Undocumented |
local_errors:MessageBuilder | Undocumented |
original_type:Optional[ | Undocumented |
Returns | |
Tuple[ | Undocumented |
Accepts a list of function signatures and attempts to combine them together into a new CallableType consisting of the union of all of the given arguments and return types.
If there is at least one non-callable type, return Any (this can happen if there is an ambiguity because of Any in arguments).
Parameters | |
types:Sequence[ | Undocumented |
Returns | |
Union[ | Undocumented |
Check for dangerous non-overlapping comparisons like 42 == 'no'.
The original_container is the original container type for 'in' checks (and None for equality checks).
'assert x is not None' for x defined as 'x = None # type: str' in class body (otherwise mypy itself would have couple dozen errors because of this). * Optional[X] and Optional[Y] are non-overlapping if X and Y are non-overlapping, although technically None is overlap, it is most likely an error. * Any overlaps with everything, i.e. always safe. * Special case: b'abc' in b'cde' is safe.
Parameters | |
left:Type | Undocumented |
right:Type | Undocumented |
original_container:Optional[ | Undocumented |
Returns | |
bool | Undocumented |
defn
can _only_ return None.Parameters | |
defn:Optional[ | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
arg_types:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
arg_names:Optional[ | Undocumented |
args:List[ | Undocumented |
callee:CallableType | Undocumented |
context:Context | Undocumented |
Returns | |
bool | Undocumented |
Fast path to determine the type of a list or set literal, based on the list of entries. This mostly impacts large module-level constant definitions.
Parameters | |
items:List[ | Undocumented |
container_fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
Fast path to determine the type of a dict literal, based on the list of entries. This mostly impacts large module-level constant definitions.
Parameters | |
e:DictExpr | Undocumented |
Returns | |
Optional[ | Undocumented |
If expression has a partial generic type, return it without additional checks.
In particular, this does not generate an error about a missing annotation.
Otherwise, return None.
Parameters | |
expr:Expression | Undocumented |
Returns | |
Optional[ | Undocumented |
Undocumented
Parameters | |
context:Optional[ | Undocumented |
dict_expr:DictExpr | Undocumented |
Returns | |
Optional[ | Undocumented |
Return pass numbers for args for two-pass argument type inference.
For each actual, the pass number is either 1 (first pass) or 2 (second pass).
Two-pass argument type inference primarily lets us infer types of lambdas more effectively.
Parameters | |
arg_types:List[ | Undocumented |
formal_to_actual:List[ | Undocumented |
num_actuals:int | Undocumented |
Returns | |
List[ | Undocumented |
Get variable node for a partial self attribute.
If the expression is not a self attribute, or attribute is not variable, or variable is not partial, return None.
Parameters | |
expr:MemberExpr | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
typ:Type | Undocumented |
member:str | Undocumented |
Returns | |
bool | Undocumented |
Infer argument expression types using a callable type as context.
For example, if callee argument 2 has type List[int], infer the argument expression with List[int] type context.
Returns the inferred types of actual arguments.
Parameters | |
callee:CallableType | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
formal_to_actual:List[ | Undocumented |
Returns | |
List[ | Undocumented |
Infer argument expression types in an empty context.
In short, we basically recurse on each argument without considering in what context the argument was called.
Parameters | |
args:List[ | Undocumented |
Returns | |
List[ | Undocumented |
Infer the type arguments for a generic callee type.
Infer based on the types of arguments.
Return a derived callable type that has the arguments applied.
Parameters | |
callee_type:CallableType | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
formal_to_actual:List[ | Undocumented |
context:Context | Undocumented |
Returns | |
CallableType | Undocumented |
Perform second pass of generic function type argument inference.
The second pass is needed for arguments with types such as Callable[[T], S], where both T and S are type variables, when the actual argument is a lambda with inferred types. The idea is to infer the type variable T in the first pass (based on the types of other arguments). This lets us infer the argument and return type of the lambda expression and thus also the type variable S in this second pass.
Return (the callee with type vars applied, inferred actual arg types).
Parameters | |
callee_type:CallableType | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
formal_to_actual:List[ | Undocumented |
old_inferred_args:Sequence[ | Undocumented |
context:Context | Undocumented |
Returns | |
Tuple[ | Undocumented |
Unify callable return type to type context to infer type vars.
For example, if the return type is set[t] where 't' is a type variable of callable, and if the context is set[int], return callable modified by substituting 't' with 'int'.
Parameters | |
callable:CallableType | Undocumented |
error_context:Context | Undocumented |
Returns | |
CallableType | Undocumented |
Try to infer lambda expression type using context.
Return None if could not infer type. The second item in the return type is the type_override parameter for check_func_item.
Parameters | |
e:LambdaExpr | Undocumented |
Returns | |
Tuple[ | Undocumented |
Analyzes the given literal expression and determines if we should be inferring an Instance type, a Literal[...] type, or an Instance that remembers the original literal. We...
Parameters | |
value:LiteralValue | Undocumented |
fallback_name:str | Undocumented |
Returns | |
Type | Undocumented |
Attempts to find the first matching callable from the given list.
If a match is found, returns a tuple containing the result type and the inferred callee type. (This tuple is meant to be eventually returned by check_call.) If multiple targets match due to ambiguous Any parameters, returns (AnyType, AnyType). If no targets match, returns None.
Assumes all of the given targets have argument counts compatible with the caller.
Parameters | |
plausible_targets:List[ | Undocumented |
args:List[ | Undocumented |
arg_types:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
arg_names:Optional[ | Undocumented |
callable_name:Optional[ | Undocumented |
object_type:Optional[ | Undocumented |
context:Context | Undocumented |
arg_messages:Optional[ | Undocumented |
Returns | |
Optional[ | Undocumented |
Undocumented
Parameters | |
callee:TypedDictType | Undocumented |
kwargs:DictExpr | Undocumented |
context:Context | Undocumented |
Returns | |
bool | Undocumented |
None
if the name of object_type
cannot be determined.Parameters | |
object_type:Type | Undocumented |
method_name:str | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
name:str | Undocumented |
Returns | |
Instance | Undocumented |
Narrow down a known type of expression using information in conditional type binder.
If 'skip_non_overlapping' is True, return None if the type and restriction are non-overlapping.
Parameters | |
expr:Expression | Undocumented |
known_type:Type | Undocumented |
skip_non_overlapping:bool | Undocumented |
Returns | |
Optional[ | Undocumented |
Undocumented
Parameters | |
left_type:TupleType | Undocumented |
index:Expression | Undocumented |
Returns | |
Type | Undocumented |
Called when we can't infer the type of a variable because it's not ready yet.
Either defer type checking of the enclosing function to the next pass or report an error.
Parameters | |
name:str | Undocumented |
context:Context | Undocumented |
Returns a list of all targets that match the caller after erasing types.
Assumes all of the given targets have argument counts compatible with the caller.
Parameters | |
plausible_targets:List[ | Undocumented |
arg_types:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
arg_names:Optional[ | Undocumented |
args:List[ | Undocumented |
context:Context | Undocumented |
Returns | |
List[ | Undocumented |
Returns all overload call targets that having matching argument counts.
If the given args contains a star-arg (*arg or **kwarg argument), this method will ensure all star-arg overloads appear at the start of the list, instead of their usual location.
The only exception is if the starred argument is something like a Tuple or a NamedTuple, which has a definitive "shape". If so, we don't move the corresponding alternative to the front since we can infer a more precise match using the original order.
Parameters | |
arg_types:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
arg_names:Optional[ | Undocumented |
overload:Overloaded | Undocumented |
Returns | |
List[ | Undocumented |
Attempt to determine a more accurate signature for a method call.
This is done by looking up and applying a method signature hook (if one exists for the given method name).
If no matching method signature hook is found, callee is returned unmodified. The same happens if the arguments refer to a non-method callable (this is allowed so that the code calling transform_callee_type needs to perform fewer boilerplate checks).
Note: this method is not called automatically as part of check_call, because in some cases check_call is called multiple times while checking a single call (for example when dealing with overloads). Instead, this method needs to be called explicitly (if appropriate) before the signature is passed to check_call.
Parameters | |
callable_name:Optional[ | Undocumented |
callee:Type | Undocumented |
args:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
context:Context | Undocumented |
arg_names:Optional[ | Undocumented |
object_type:Optional[ | Undocumented |
Returns | |
Type | Undocumented |
If the given expression or type corresponds to an int literal or a union of int literals, returns a list of the underlying ints. Otherwise, returns None.
Specifically, this function is guaranteed to return a list with one or more ints if one one the following is true:
Parameters | |
index:Expression | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
exprs:Sequence[ | Undocumented |
overrides:Sequence[ | Undocumented |
Returns | |
Iterator[ | Undocumented |
Accepts a list of overload signatures and attempts to match calls by destructuring the first union.
Return a list of (<return type>, <inferred variant type>) if call succeeds for every item of the desctructured union. Returns None if there is no match.
Parameters | |
plausible_targets:List[ | Undocumented |
args:List[ | Undocumented |
arg_types:List[ | Undocumented |
arg_kinds:List[ | Undocumented |
arg_names:Optional[ | Undocumented |
callable_name:Optional[ | Undocumented |
object_type:Optional[ | Undocumented |
context:Context | Undocumented |
arg_messages:Optional[ | Undocumented |
level:int | Undocumented |
Returns | |
Optional[ | Undocumented |
Undocumented
Parameters | |
kwargs:DictExpr | Undocumented |
Returns | |
Optional[ | Undocumented |
Type check a comparison expression.
Comparison expressions are type checked consecutive-pair-wise That is, 'a < b > c == d' is check as 'a < b and b > c and c == d'
Parameters | |
e:ComparisonExpr | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
e:ConditionalExpr | Undocumented |
allow_none_return:bool | Undocumented |
Returns | |
Type | Undocumented |
Parameters | |
e:DictionaryComprehension | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
enum_type:TypeInfo | Undocumented |
index:Expression | Undocumented |
context:Context | Undocumented |
Returns | |
Type | Undocumented |
Analyze type of an index expression for a given type of base expression.
The 'original_type' is used for error messages (currently used for union types).
Parameters | |
left_type:Type | Undocumented |
e:IndexExpr | Undocumented |
original_type:Optional[ | Undocumented |
Returns | |
Type | Undocumented |
Parameters | |
e:MemberExpr | Undocumented |
is_lvalue:bool | Undocumented |
Returns | |
Type | Undocumented |
Right hand side of a type alias definition.
It has the same type as if the alias itself was used in a runtime context. For example, here:
A = reveal_type(List[T]) reveal_type(A)
both reveal_type
instances will reveal the same type def (...) -> builtins.list[Any]
.
Note that type variables are implicitly substituted with Any
.
Parameters | |
alias:TypeAliasExpr | Undocumented |
Returns | |
Type | Undocumented |
Type check a type application (expr[type, ...]).
There are two different options here, depending on whether expr refers to a type alias or directly to a generic class. In the first case we need to use a dedicated function typeanal.expand_type_aliases. This is due to the fact that currently type aliases machinery uses unbound type variables, while normal generics use bound ones; see TypeAlias docstring for more details.
Parameters | |
tapp:TypeApplication | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
td_type:TypedDictType | Undocumented |
index:Expression | Undocumented |
Returns | |
Type | Undocumented |
Undocumented
Parameters | |
e:YieldFromExpr | Undocumented |
allow_none_return:bool | Undocumented |
Returns | |
Type | Undocumented |
Parameters | |
e:SuperExpr | Undocumented |
Returns | |
Union[ | Undocumented |