class SemanticAnalyzer(NodeVisitor[None], SemanticAnalyzerInterface, SemanticAnalyzerPluginInterface): (source)
Semantically analyze parsed mypy files.
The analyzer binds names and does various consistency checks for an AST. Note that type checking is performed as a separate pass.
Method | __init__ |
Construct semantic analyzer. |
Method | accept |
Undocumented |
Method | add_builtin_aliases |
Add builtin type aliases to typing module. |
Method | add_exports |
Undocumented |
Method | add_function_to_symbol_table |
Undocumented |
Method | add_implicit_module_attrs |
Manually add implicit definitions of module '__name__' etc. |
Method | add_imported_symbol |
Add an alias to an existing symbol through import. |
Method | add_local |
Add local variable or function. |
Method | add_module_symbol |
Add symbol that is a reference to a module object. |
Method | add_plugin_dependency |
Add dependency from trigger to a target. |
Method | add_redefinition |
Add a symbol table node that reflects a redefinition as a function or a class. |
Method | add_symbol |
Add symbol to the currently active symbol table. |
Method | add_symbol_skip_local |
Same as above, but skipping the local namespace. |
Method | add_symbol_table_node |
Add symbol table node to the currently active symbol table. |
Method | add_type_alias_deps |
Add full names of type aliases on which the current node depends. |
Method | add_typing_extension_aliases |
Typing extensions module does contain some type aliases. |
Method | add_unknown_imported_symbol |
Add symbol that we don't know what it points to because resolving an import failed. |
Method | adjust_public_exports |
Adjust the module visibility of globals due to __all__. |
Method | already_defined |
Undocumented |
Method | anal_type |
Semantically analyze a type. |
Method | analyze_alias |
Check if 'rvalue' is a valid type allowed for aliasing (e.g. not a type variable). |
Method | analyze_arg_initializers |
Undocumented |
Method | analyze_base_classes |
Analyze base class types. |
Method | analyze_class |
Undocumented |
Method | analyze_class_body_common |
Parts of class body analysis that are common to all kinds of class defs. |
Method | analyze_class_decorator |
Undocumented |
Method | analyze_class_keywords |
Undocumented |
Method | analyze_class_typevar_declaration |
Analyze type variables declared using Generic[...] or Protocol[...]. |
Method | analyze_comp_for |
Analyses the 'comp_for' part of comprehensions (part 1). |
Method | analyze_comp_for_2 |
Analyses the 'comp_for' part of comprehensions (part 2). |
Method | analyze_enum_assign |
Check if s defines an Enum. |
Method | analyze_func_def |
Undocumented |
Method | analyze_function_body |
Undocumented |
Method | analyze_identity_global_assignment |
Special case 'X = X' in global scope. |
Method | analyze_lvalue |
Analyze an lvalue or assignment target. |
Method | analyze_lvalues |
Undocumented |
Method | analyze_member_lvalue |
Analyze lvalue that is a member expression. |
Method | analyze_metaclass |
Undocumented |
Method | analyze_name_lvalue |
Analyze an lvalue that targets a name expression. |
Method | analyze_namedtuple_assign |
Check if s defines a namedtuple. |
Method | analyze_namedtuple_classdef |
Check if this class can define a named tuple. |
Method | analyze_overload_sigs_and_impl |
Find overload signatures, the implementation, and items with missing @overload. |
Method | analyze_overloaded_func_def |
Undocumented |
Method | analyze_property_with_multi_part_definition |
Analyze a property defined using multiple methods (e.g., using @x.setter). |
Method | analyze_simple_literal_type |
Return builtins.int if rvalue is an int literal, etc. |
Method | analyze_try_stmt |
Undocumented |
Method | analyze_tuple_or_list_lvalue |
Analyze an lvalue or assignment target that is a list or tuple. |
Method | analyze_type_application |
Analyze special form -- type application (either direct or via type aliasing). |
Method | analyze_type_application_args |
Analyze type arguments (index) in a type application. |
Method | analyze_type_expr |
Undocumented |
Method | analyze_typeddict_assign |
Check if s defines a typed dict. |
Method | analyze_unbound_tvar |
Undocumented |
Method | analyze_value_types |
Analyze types from values expressions in type variable definition. |
Method | apply_class_plugin_hooks |
Apply a plugin hook that may infer a more precise definition for a class. |
Method | apply_dynamic_class_hook |
Undocumented |
Method | attribute_already_defined |
Undocumented |
Method | basic_new_typeinfo |
Undocumented |
Method | bind_name_expr |
Bind name expression to a symbol table node. |
Method | calculate_class_mro |
Calculate method resolution order for a class. |
Method | can_be_type_alias |
Is this a valid r.h.s. for an alias definition? |
Method | cannot_resolve_name |
Undocumented |
Method | check_and_set_up_type_alias |
Check if assignment creates a type alias and set it up as needed. |
Method | check_classvar |
Check if assignment defines a class variable. |
Method | check_classvar_in_signature |
Undocumented |
Method | check_decorated_function_is_method |
Undocumented |
Method | check_final_implicit_def |
Do basic checks for final declaration on self in __init__. |
Method | check_fixed_args |
Verify that expr has specified number of positional args. |
Method | check_function_signature |
Undocumented |
Method | check_lvalue_validity |
Undocumented |
Method | check_no_global |
Undocumented |
Method | check_typevarlike_name |
Checks that the name of a TypeVar or ParamSpec matches its variable. |
Method | class_type |
Generate type of first argument of class methods from type of self. |
Method | clean_up_bases_and_infer_type_variables |
Remove extra base classes such as Generic and infer type vars. |
Method | configure_base_classes |
Set up base classes. |
Method | configure_tuple_base_class |
Undocumented |
Method | correct_relative_import |
Undocumented |
Method | create_alias |
Undocumented |
Method | create_getattr_var |
Create a dummy variable using module-level __getattr__ return type. |
Method | current_symbol_kind |
Undocumented |
Method | current_symbol_table |
Get currently active symbol table. |
Method | defer |
Defer current analysis target to be analyzed again. |
Method | enter |
Enter a function, generator or comprehension scope. |
Method | enter_class |
Undocumented |
Method | enum_has_final_values |
Undocumented |
Method | expr_to_analyzed_type |
Undocumented |
Method | expr_to_unanalyzed_type |
Undocumented |
Method | fail |
Emit an error message at given location. |
Method | fail_blocker |
Undocumented |
Method | fail_invalid_classvar |
Undocumented |
Method | file_context |
Configure analyzer for analyzing targets within a file/class. |
Method | flatten_lvalues |
Undocumented |
Method | found_incomplete_ref |
Have we encountered an incomplete reference since starting tracking? |
Method | get_all_bases_tvars |
Return all type variable references in bases. |
Method | get_module_symbol |
Look up a symbol from a module. |
Method | get_name_repr_of_expr |
Try finding a short simplified textual representation of a base class expression. |
Method | get_typevarlike_declaration |
Returns the call expression if s is a declaration of typevarlike_type (TypeVar or ParamSpec), or None otherwise. |
Method | handle_missing_overload_decorators |
Generate errors for overload items without @overload. |
Method | handle_missing_overload_implementation |
Generate error about missing overload implementation (only if needed). |
Method | implicit_symbol |
Create symbol for a qualified name reference through Any type. |
Method | in_checked_function |
Should we type-check the current function? |
Method | is_active_symbol_in_class_body |
Can a symbol defined in class body accessed at current statement? |
Method | is_alias_for_final_name |
Undocumented |
Method | is_annotated_protocol_member |
Check whether a protocol member is annotated. |
Method | is_base_class |
Determine if t is a base class of s (but do not use mro). |
Method | is_class_scope |
Undocumented |
Method | is_classvar |
Undocumented |
Method | is_core_builtin_class |
Undocumented |
Method | is_defined_in_current_module |
Undocumented |
Method | is_final_redefinition |
Undocumented |
Method | is_final_type |
Undocumented |
Method | is_func_scope |
Undocumented |
Method | is_future_flag_set |
Is the specific __future__ feature imported |
Method | is_global_or_nonlocal |
Undocumented |
Method | is_incomplete_namespace |
Is a module or class namespace potentially missing some definitions? |
Method | is_initial_mangled_global |
Undocumented |
Method | is_local_name |
Does name look like reference to a definition in the current module? |
Method | is_mangled_global |
Undocumented |
Method | is_missing_module |
Undocumented |
Method | is_module_scope |
Undocumented |
Method | is_nested_within_func_scope |
Are we underneath a function scope, even if we are in a nested class also? |
Method | is_none_alias |
Is this a r.h.s. for a None alias? |
Method | is_overloaded_item |
Check whether the function belongs to the overloaded variants |
Method | is_self_member_ref |
Does memberexpr to refer to an attribute of self? |
Method | is_textually_before_statement |
Check if a node is defined textually before the current statement |
Method | is_type_ref |
Does this expression refer to a type? |
Method | is_valid_del_target |
Undocumented |
Method | leave_class |
Restore analyzer state. |
Method | lookup |
Look up an unqualified (no dots) name in all active namespaces. |
Method | lookup_current_scope |
Undocumented |
Method | lookup_fully_qualified |
Lookup a symbol by its fully qualified name. |
Method | lookup_fully_qualified_or_none |
Lookup a fully qualified name that refers to a module-level definition. |
Method | lookup_qualified |
Lookup a qualified name in all activate namespaces. |
Method | lookup_type_node |
Undocumented |
Method | make_empty_type_info |
Undocumented |
Method | make_name_lvalue_point_to_existing_def |
Update an lvalue to point to existing definition in the same scope. |
Method | make_name_lvalue_var |
Return a Var node for an lvalue that is a name expression. |
Method | mark_incomplete |
Mark a definition as incomplete (and defer current analysis target). |
Method | name_already_defined |
Undocumented |
Method | name_not_defined |
Undocumented |
Method | named_type |
Construct an instance of a builtin type with given type arguments. |
Method | named_type_or_none |
Construct an instance of a type with given type arguments. |
Method | note |
Undocumented |
Method | object_type |
Undocumented |
Method | parse_bool |
Parse True/False literals. |
Method | prepare_builtins_namespace |
Add certain special-cased definitions to the builtins module. |
Method | prepare_class_def |
Prepare for the analysis of a class definition. |
Method | prepare_file |
Prepare a freshly parsed file for semantic analysis. |
Method | prepare_method_signature |
Check basic signature validity and tweak annotation of self/cls argument. |
Method | prepare_typing_namespace |
Remove dummy alias definitions such as List = TypeAlias(object) from typing. |
Method | process__all__ |
Export names if argument is a __all__ assignment. |
Method | process__deletable__ |
Undocumented |
Method | process__slots__ |
Processing __slots__ if defined in type. |
Method | process_final_in_overload |
Detect the @final status of an overloaded function (and perform checks). |
Method | process_import_over_existing_name |
Undocumented |
Method | process_imported_symbol |
Undocumented |
Method | process_module_assignment |
Propagate module references across assignments. |
Method | process_paramspec_declaration |
Checks if s declares a ParamSpec; if yes, store it in symbol table. |
Method | process_placeholder |
Process a reference targeting placeholder node. |
Method | process_static_or_class_method_in_overload |
Undocumented |
Method | process_type_annotation |
Analyze type annotation or infer simple literal type. |
Method | process_typevar_declaration |
Check if s declares a TypeVar; it yes, store it in symbol table. |
Method | process_typevar_parameters |
Undocumented |
Method | qualified_name |
Make qualified name using current module and enclosing class (if any). |
Method | record_incomplete_ref |
Record the encounter of an incomplete reference and defer current analysis target. |
Method | record_special_form_lvalue |
Record minimal necessary information about l.h.s. of a special form. |
Method | refresh_partial |
Refresh a stale target in fine-grained incremental mode. |
Method | refresh_top_level |
Reanalyze a stale module top-level in fine-grained incremental mode. |
Method | report_hang |
Undocumented |
Method | report_missing_module_attribute |
Undocumented |
Method | schedule_patch |
Undocumented |
Method | set_dummy_mro |
Undocumented |
Method | set_future_import_flags |
Undocumented |
Method | set_original_def |
If 'new' conditionally redefine 'previous', set 'previous' as original |
Method | should_wait_rhs |
Can we already classify this r.h.s. of an assignment or should we wait? |
Method | store_declared_types |
Undocumented |
Method | store_final_status |
If this is a locally valid final declaration, set the corresponding flag on Var . |
Method | str_type |
Undocumented |
Method | track_incomplete_refs |
Return tag that can be used for tracking references to incomplete names. |
Method | translate_dict_call |
Translate 'dict(x=y, ...)' to {'x': y, ...} and 'dict()' to {}. |
Method | tvar_scope_frame |
Undocumented |
Method | type_analyzer |
Undocumented |
Method | unbox_literal |
Undocumented |
Method | unwrap_final |
Strip Final[...] if present in an assignment. |
Method | update_function_type_variables |
Make any type variables in the signature of defn explicit. |
Method | update_metaclass |
Lookup for special metaclass declarations, and update defn fields accordingly. |
Method | verify_base_classes |
Undocumented |
Method | visit__promote_expr |
Undocumented |
Method | visit_assert_stmt |
Undocumented |
Method | visit_assignment_expr |
Undocumented |
Method | visit_assignment_stmt |
Undocumented |
Method | visit_await_expr |
Undocumented |
Method | visit_backquote_expr |
Undocumented |
Method | visit_block |
Undocumented |
Method | visit_block_maybe |
Undocumented |
Method | visit_break_stmt |
Undocumented |
Method | visit_call_expr |
Analyze a call expression. |
Method | visit_cast_expr |
Undocumented |
Method | visit_class_def |
Undocumented |
Method | visit_comparison_expr |
Undocumented |
Method | visit_conditional_expr |
Undocumented |
Method | visit_continue_stmt |
Undocumented |
Method | visit_decorator |
Undocumented |
Method | visit_del_stmt |
Undocumented |
Method | visit_dict_expr |
Undocumented |
Method | visit_dictionary_comprehension |
Undocumented |
Method | visit_exec_stmt |
Undocumented |
Method | visit_expression_stmt |
Undocumented |
Method | visit_for_stmt |
Undocumented |
Method | visit_func_def |
Undocumented |
Method | visit_generator_expr |
Undocumented |
Method | visit_global_decl |
Undocumented |
Method | visit_if_stmt |
Undocumented |
Method | visit_import |
Undocumented |
Method | visit_import_all |
Undocumented |
Method | visit_import_from |
Undocumented |
Method | visit_index_expr |
Undocumented |
Method | visit_lambda_expr |
Undocumented |
Method | visit_list_comprehension |
Undocumented |
Method | visit_list_expr |
Undocumented |
Method | visit_member_expr |
Undocumented |
Method | visit_name_expr |
Undocumented |
Method | visit_nonlocal_decl |
Undocumented |
Method | visit_op_expr |
Undocumented |
Method | visit_operator_assignment_stmt |
Undocumented |
Method | visit_overloaded_func_def |
Undocumented |
Method | visit_print_stmt |
Undocumented |
Method | visit_raise_stmt |
Undocumented |
Method | visit_return_stmt |
Undocumented |
Method | visit_reveal_expr |
Undocumented |
Method | visit_set_comprehension |
Undocumented |
Method | visit_set_expr |
Undocumented |
Method | visit_slice_expr |
Undocumented |
Method | visit_star_expr |
Undocumented |
Method | visit_super_expr |
Undocumented |
Method | visit_try_stmt |
Undocumented |
Method | visit_tuple_expr |
Undocumented |
Method | visit_type_application |
Undocumented |
Method | visit_unary_expr |
Undocumented |
Method | visit_while_stmt |
Undocumented |
Method | visit_with_stmt |
Undocumented |
Method | visit_yield_expr |
Undocumented |
Method | visit_yield_from_expr |
Undocumented |
Class Variable | __deletable__ |
Undocumented |
Class Variable | global_decls |
Undocumented |
Class Variable | nonlocal_decls |
Undocumented |
Class Variable | progress |
Undocumented |
Class Variable | wrapped_coro_return_types |
Undocumented |
Instance Variable | all_exports |
Undocumented |
Instance Variable | block_depth |
Undocumented |
Instance Variable | cur_mod_id |
Undocumented |
Instance Variable | cur_mod_node |
Undocumented |
Instance Variable | deferral_debug_context |
Undocumented |
Instance Variable | deferred |
Undocumented |
Instance Variable | enum_call_analyzer |
Undocumented |
Instance Variable | errors |
Undocumented |
Instance Variable | export_map |
Undocumented |
Instance Variable | function_stack |
Undocumented |
Instance Variable | future_import_flags |
Undocumented |
Instance Variable | globals |
Undocumented |
Instance Variable | imports |
Undocumented |
Instance Variable | incomplete |
Undocumented |
Instance Variable | incomplete_namespaces |
Undocumented |
Instance Variable | incomplete_type_stack |
Undocumented |
Instance Variable | is_comprehension_stack |
Undocumented |
Instance Variable | locals |
Undocumented |
Instance Variable | loop_depth |
Undocumented |
Instance Variable | missing_modules |
Undocumented |
Instance Variable | missing_names |
Undocumented |
Instance Variable | modules |
Undocumented |
Instance Variable | msg |
Undocumented |
Instance Variable | named_tuple_analyzer |
Undocumented |
Instance Variable | newtype_analyzer |
Undocumented |
Instance Variable | num_incomplete_refs |
Undocumented |
Instance Variable | options |
Undocumented |
Instance Variable | patches |
Undocumented |
Instance Variable | plugin |
Undocumented |
Instance Variable | recurse_into_functions |
Undocumented |
Instance Variable | saved_locals |
Undocumented |
Instance Variable | scope |
Undocumented |
Instance Variable | statement |
Undocumented |
Instance Variable | tvar_scope |
Undocumented |
Instance Variable | type |
Undocumented |
Instance Variable | type_stack |
Undocumented |
Instance Variable | typed_dict_analyzer |
Undocumented |
Property | final_iteration |
Is this the final iteration of semantic analysis? |
Property | is_stub_file |
Undocumented |
Property | is_typeshed_stub_file |
Undocumented |
Instance Variable | _final_iteration |
Undocumented |
Instance Variable | _is_stub_file |
Undocumented |
Instance Variable | _is_typeshed_stub_file |
Undocumented |
Construct semantic analyzer.
We reuse the same semantic analyzer instance across multiple modules.
modules: Global modules dictionary missing_modules: Modules that could not be imported encountered so far incomplete_namespaces: Namespaces that are being populated during semantic analysis
(can contain modules and classes within the current SCC; mutated by the caller)
errors: Report analysis errors using this instance
Parameters | |
modules:Dict[ | Undocumented |
missing_modules:Set[ | Undocumented |
incomplete_namespaces:Set[ | Undocumented |
errors:Errors | Undocumented |
plugin:Plugin | Undocumented |
Add builtin type aliases to typing module.
For historical reasons, the aliases like List = list
are not defined
in typeshed stubs for typing module. Instead we need to manually add the
corresponding nodes on the fly. We explicitly mark these aliases as normalized,
so that a user can write typing.List[int]
.
Parameters | |
tree:MypyFile | Undocumented |
Parameters | |
file_node:MypyFile | Undocumented |
Parameters | |
name:str | Undocumented |
node:SymbolTableNode | Undocumented |
context:Context | Undocumented |
module_public:bool | Undocumented |
module_hidden:bool | Undocumented |
Parameters | |
node:Union[ | Undocumented |
context:Context | Undocumented |
Parameters | |
id:str | Undocumented |
as_id:str | Undocumented |
context:Context | Undocumented |
module_public:bool | Undocumented |
module_hidden:bool | Undocumented |
Add dependency from trigger to a target.
If the target is not given explicitly, use the current target.
Parameters | |
trigger:str | Undocumented |
target:Optional[ | Undocumented |
Add a symbol table node that reflects a redefinition as a function or a class.
Redefinitions need to be added to the symbol table so that they can be found through AST traversal, but they have dummy names of form 'name-redefinition[N]', where N ranges over 2, 3, ... (omitted for the first redefinition).
Note: we always store redefinitions independently of whether they are valid or not (so they will be semantically analyzed), the caller should give an error for invalid redefinitions (such as e.g. variable redefined as a class).
Parameters | |
names:SymbolTable | Undocumented |
name:str | Undocumented |
symbol:SymbolTableNode | Undocumented |
Add symbol to the currently active symbol table.
Generally additions to symbol table should go through this method or one of the methods below so that kinds, redefinitions, conditional definitions, and skipped names are handled consistently.
Return True if we actually added the symbol, or False if we refused to do so (because something is not ready).
If can_defer is True, defer current target if adding a placeholder.
Parameters | |
name:str | Undocumented |
node:SymbolNode | Undocumented |
context:Context | Undocumented |
module_public:bool | Undocumented |
module_hidden:bool | Undocumented |
can_defer:bool | Undocumented |
escape_comprehensions:bool | Undocumented |
Returns | |
bool | Undocumented |
Same as above, but skipping the local namespace.
This doesn't check for previous definition and is only used for serialization of method-level classes.
Classes defined within methods can be exposed through an attribute type, but method-level symbol tables aren't serialized. This method can be used to add such classes to an enclosing, serialized symbol table.
Parameters | |
name:str | Undocumented |
node:SymbolNode | Undocumented |
Add symbol table node to the currently active symbol table.
Return True if we actually added the symbol, or False if we refused to do so (because something is not ready or it was a no-op).
Generate an error if there is an invalid redefinition.
If context is None, unconditionally add node, since we can't report an error. Note that this is used by plugins to forcibly replace nodes!
TODO: Prevent plugins from replacing nodes, as it could cause problems?
Parameters | |
name:str | Undocumented |
symbol:SymbolTableNode | Undocumented |
context:Optional[ | Undocumented |
can_defer:bool | Undocumented |
escape_comprehensions:bool | Undocumented |
Returns | |
bool | Undocumented |
Add full names of type aliases on which the current node depends.
This is used by fine-grained incremental mode to re-check the corresponding nodes.
If target
is None, then the target node used will be the current scope.
Parameters | |
aliases_used:Iterable[ | Undocumented |
target:Optional[ | Undocumented |
Typing extensions module does contain some type aliases.
We need to analyze them as such, because in typeshed
they are just defined as _Alias()
call.
Which is not supported natively.
Parameters | |
tree:MypyFile | Undocumented |
Add symbol that we don't know what it points to because resolving an import failed.
This can happen if a module is missing, or it is present, but doesn't have
the imported attribute. The target_name
is the name of symbol in the namespace
it is imported from. For example, for 'from mod import x as y' the target_name is
'mod.x'. This is currently used only to track logical dependencies.
Parameters | |
name:str | Undocumented |
context:Context | Undocumented |
target_name:Optional[ | Undocumented |
module_public:bool | Undocumented |
module_hidden:bool | Undocumented |
Undocumented
Parameters | |
name:str | Undocumented |
ctx:Context | Undocumented |
original_ctx:Optional[ | Undocumented |
noun:str | Undocumented |
Semantically analyze a type.
typ: Type to analyze (if already analyzed, this is a no-op) allow_placeholder: If True, may return PlaceholderType if
encountering an incomplete definition
Return None only if some part of the type couldn't be bound and it referred to an incomplete namespace or definition. In this case also defer as needed. During a final iteration this won't return None; instead report an error if the type can't be analyzed and return AnyType.
In case of other errors, report an error message and return AnyType.
Parameters | |
typ:Type | Undocumented |
tvar_scope:Optional[ | Undocumented |
allow_tuple_literal:bool | Undocumented |
allow_unbound_tvars:bool | Undocumented |
allow_placeholder:bool | Undocumented |
allow_required:bool | Undocumented |
report_invalid_types:bool | Undocumented |
third_pass:bool | Undocumented |
Returns | |
Optional[ | Undocumented |
Check if 'rvalue' is a valid type allowed for aliasing (e.g. not a type variable).
If yes, return the corresponding type, a list of qualified type variable names for generic aliases, a set of names the alias depends on, and a list of type variables if the alias is generic. An schematic example for the dependencies:
A = int B = str analyze_alias(Dict[A, B])[2] == {'__main__.A', '__main__.B'}
Parameters | |
rvalue:Expression | Undocumented |
allow_placeholder:bool | Undocumented |
Returns | |
Tuple[ | Undocumented |
Analyze base class types.
Return None if some definition was incomplete. Otherwise, return a tuple with these items:
- List of (analyzed type, original expression) tuples
- Boolean indicating whether one of the bases had a semantic analysis error
Parameters | |
base_type_exprs:List[ | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
defn:ClassDef | Undocumented |
Analyze type variables declared using Generic[...] or Protocol[...].
Return None if the base class does not declare type variables. Otherwise, return the type variables.
Parameters | |
base:Type | Undocumented |
Returns | |
Optional[ | Undocumented |
Analyses the 'comp_for' part of comprehensions (part 1).
That is the part after 'for' in (x for x in l if p). This analyzes variables and conditions which are analyzed in a local scope.
Parameters | |
expr:Union[ | Undocumented |
Analyses the 'comp_for' part of comprehensions (part 2).
That is the part after 'for' in (x for x in l if p). This analyzes the 'l' part which is analyzed in the surrounding scope.
Parameters | |
expr:Union[ | Undocumented |
Special case 'X = X' in global scope.
This allows supporting some important use cases.
Return true if special casing was applied.
Parameters | |
s:AssignmentStmt | Undocumented |
Returns | |
bool | Undocumented |
Analyze an lvalue or assignment target.
lval: The target lvalue nested: If true, the lvalue is within a tuple or list lvalue expression explicit_type: Assignment has type annotation escape_comprehensions: If we are inside a comprehension, set the variable
in the enclosing scope instead. This implements https://www.python.org/dev/peps/pep-0572/#scope-of-the-target
Parameters | |
lval:Lvalue | Undocumented |
nested:bool | Undocumented |
explicit_type:bool | Undocumented |
is_final:bool | Undocumented |
escape_comprehensions:bool | Undocumented |
has_explicit_value:bool | Undocumented |
Analyze lvalue that is a member expression.
Parameters | |
lval:MemberExpr | Undocumented |
explicit_type:bool | Undocumented |
is_final:bool | Undocumented |
Analyze an lvalue that targets a name expression.
Arguments are similar to "analyze_lvalue".
Parameters | |
lvalue:NameExpr | Undocumented |
explicit_type:bool | Undocumented |
is_final:bool | Undocumented |
escape_comprehensions:bool | Undocumented |
has_explicit_value:bool | Undocumented |
Parameters | |
defn:ClassDef | Undocumented |
Returns | |
bool | Undocumented |
Find overload signatures, the implementation, and items with missing @overload.
Assume that the first was already analyzed. As a side effect: analyzes remaining items and updates 'is_overload' flags.
Parameters | |
defn:OverloadedFuncDef | Undocumented |
Returns | |
Tuple[ | Undocumented |
Analyze a property defined using multiple methods (e.g., using @x.setter).
Assume that the first method (@property) has already been analyzed.
Parameters | |
defn:OverloadedFuncDef | Undocumented |
Return builtins.int if rvalue is an int literal, etc.
If this is a 'Final' context, we return "Literal[...]" instead.
Parameters | |
rvalue:Expression | Undocumented |
is_final:bool | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
lval:TupleExpr | Undocumented |
explicit_type:bool | Undocumented |
Parameters | |
expr:IndexExpr | Undocumented |
Parameters | |
items:List[ | Undocumented |
Returns | |
List[ | Undocumented |
Parameters | |
defn:ClassDef | Undocumented |
Undocumented
Parameters | |
name:str | Undocumented |
ctx:Context | Undocumented |
original_ctx:Optional[ | Undocumented |
Parameters | |
expr:NameExpr | Undocumented |
sym:SymbolTableNode | Undocumented |
Calculate method resolution order for a class.
obj_type
may be omitted in the third pass when all classes are already analyzed.
It exists just to fill in empty base class list during second pass in case of
an import cycle.
Parameters | |
defn:ClassDef | Undocumented |
obj_type:Optional[ | Undocumented |
Is this a valid r.h.s. for an alias definition?
Note: this function should be only called for expressions where self.should_wait_rhs() returns False.
Parameters | |
rv:Expression | Undocumented |
allow_none:bool | Undocumented |
Returns | |
bool | Undocumented |
Check if assignment creates a type alias and set it up as needed.
Return True if it is a type alias (even if the target is not ready), or False otherwise.
Note: the resulting types for subscripted (including generic) aliases are also stored in rvalue.analyzed.
Parameters | |
s:AssignmentStmt | Undocumented |
Returns | |
bool | Undocumented |
Do basic checks for final declaration on self in __init__.
Additional re-definition checks are performed by analyze_lvalue
.
Parameters | |
s:AssignmentStmt | Undocumented |
Verify that expr has specified number of positional args.
Return True if the arguments are valid.
Parameters | |
expr:CallExpr | Undocumented |
numargs:int | Undocumented |
name:str | Undocumented |
Returns | |
bool | Undocumented |
Undocumented
Parameters | |
name:str | Undocumented |
ctx:Context | Undocumented |
is_overloaded_func:bool | Undocumented |
Remove extra base classes such as Generic and infer type vars.
For example, consider this class:
class Foo(Bar, Generic[T]): ...
Now we will remove Generic[T] from bases of Foo and infer that the type variable 'T' is a type argument of Foo.
Note that this is performed before semantic analysis.
Returns (remaining base expressions, inferred type variables, is protocol).
Parameters | |
defn:ClassDef | Undocumented |
base_type_exprs:List[ | Undocumented |
context:Context | Undocumented |
Returns | |
Tuple[ | Undocumented |
Set up base classes.
This computes several attributes on the corresponding TypeInfo defn.info related to the base classes: defn.info.bases, defn.info.mro, and miscellaneous others (at least tuple_type, fallback_to_any, and is_enum.)
Parameters | |
defn:ClassDef | Undocumented |
bases:List[ | Undocumented |
Undocumented
Parameters | |
defn:ClassDef | Undocumented |
base:TupleType | Undocumented |
base_expr:Expression | Undocumented |
Returns | |
Instance | Undocumented |
Undocumented
Parameters | |
tree:MypyFile | Undocumented |
target_name:str | Undocumented |
alias:str | Undocumented |
name:str | Undocumented |
Create a dummy variable using module-level __getattr__ return type.
If not possible, return None.
Note that multiple Var nodes can be created for a single name. We can use the from_module_getattr and the fullname attributes to check if two dummy Var nodes refer to the same thing. Reusing Var nodes would require non-local mutable state, which we prefer to avoid.
Parameters | |
getattr_defn:SymbolTableNode | Undocumented |
name:str | Undocumented |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
Get currently active symbol table.
May be module, class, or local namespace.
Parameters | |
escape_comprehensions:bool | Undocumented |
Returns | |
SymbolTable | Undocumented |
Defer current analysis target to be analyzed again.
This must be called if something in the current target is incomplete or has a placeholder node. However, this must not be called during the final analysis iteration! Instead, an error should be generated. Often 'process_placeholder' is a good way to either defer or generate an error.
Parameters | |
debug_context:Optional[ | Undocumented |
Parameters | |
function:Union[ | Undocumented |
Returns | |
Iterator[ | Undocumented |
Undocumented
Parameters | |
expr:Expression | Undocumented |
report_invalid_types:bool | Undocumented |
allow_placeholder:bool | Undocumented |
Returns | |
Optional[ | Undocumented |
Configure analyzer for analyzing targets within a file/class.
Parameters | |
file_node:MypyFile | Undocumented |
options:Options | Undocumented |
active_type:Optional[ | Undocumented |
Returns | |
Iterator[ | Undocumented |
Parameters | |
tag:Tag | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
base_type_exprs:List[ | Undocumented |
removed:List[ | Undocumented |
Returns | |
TypeVarLikeList | Undocumented |
Look up a symbol from a module.
Return None if no matching symbol could be bound.
Parameters | |
node:MypyFile | Undocumented |
name:str | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
expr:Expression | Undocumented |
Returns | |
Optional[ | Undocumented |
s
is a declaration of typevarlike_type
(TypeVar or ParamSpec), or None otherwise.Parameters | |
s:AssignmentStmt | Undocumented |
typevarlike_types:Tuple[ | Undocumented |
Returns | |
Optional[ | Undocumented |
Generate errors for overload items without @overload.
Side effect: remote non-overload items.
Parameters | |
defn:OverloadedFuncDef | Undocumented |
non_overload_indexes:List[ | Undocumented |
some_overload_decorators:bool | Undocumented |
Parameters | |
defn:OverloadedFuncDef | Undocumented |
Parameters | |
sym:SymbolTableNode | Undocumented |
name:str | Undocumented |
parts:List[ | Undocumented |
source_type:AnyType | Undocumented |
Returns | |
SymbolTableNode | Undocumented |
Should we type-check the current function?
Returns | |
bool | Undocumented |
Can a symbol defined in class body accessed at current statement?
Only allow access to class attributes textually after the definition, so that it's possible to fall back to the outer scope. Example:
class X: ...
- class C:
- X = X # Initializer refers to outer scope
Nested classes are an exception, since we want to support arbitrary forward references in type annotations.
Parameters | |
node:Optional[ | Undocumented |
Returns | |
bool | Undocumented |
Check whether a protocol member is annotated.
There are some exceptions that can be left unannotated, like __slots__.
Parameters | |
s:AssignmentStmt | Undocumented |
Returns | |
bool | Undocumented |
Is a module or class namespace potentially missing some definitions?
If a name is missing from an incomplete namespace, we'll need to defer the current analysis target.
Parameters | |
fullname:str | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
name:str | Undocumented |
Returns | |
bool | Undocumented |
Is this a r.h.s. for a None alias?
We special case the assignments like Void = type(None), to allow using Void in type annotations.
Parameters | |
node:Expression | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
node:SymbolNode | Undocumented |
statement:Statement | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
memberexpr:MemberExpr | Undocumented |
Returns | |
bool | Undocumented |
Check if a node is defined textually before the current statement
Note that decorated functions' line number are the same as the top decorator.
Parameters | |
node:SymbolNode | Undocumented |
Returns | |
bool | Undocumented |
Does this expression refer to a type?
If bare is True, this is not a base of an index expression, so some special forms are not valid (like a bare Union).
Note: This method should be only used in context of a type alias definition. This method can only return True for RefExprs, to check if C[int] is a valid target for type alias call this method on expr.base (i.e. on C in C[int]). See also can_be_type_alias().
Parameters | |
rv:Expression | Undocumented |
bare:bool | Undocumented |
Returns | |
bool | Undocumented |
Look up an unqualified (no dots) name in all active namespaces.
Note that the result may contain a PlaceholderNode. The caller may want to defer in that case.
Generate an error if the name is not defined unless suppress_errors is true or the current namespace is incomplete. In the latter case defer.
Parameters | |
name:str | Undocumented |
ctx:Context | Undocumented |
suppress_errors:bool | Undocumented |
Returns | |
Optional[ | Undocumented |
Lookup a symbol by its fully qualified name.
Raise an error if not found.
Parameters | |
fullname:str | Undocumented |
Returns | |
SymbolTableNode | Undocumented |
Lookup a fully qualified name that refers to a module-level definition.
Don't assume that the name is defined. This happens in the global namespace -- the local module namespace is ignored. This does not dereference indirect refs.
Note that this can't be used for names nested in class namespaces.
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
Lookup a qualified name in all activate namespaces.
Note that the result may contain a PlaceholderNode. The caller may want to defer in that case.
Generate an error if the name is not defined unless suppress_errors is true or the current namespace is incomplete. In the latter case defer.
Parameters | |
name:str | Undocumented |
ctx:Context | Undocumented |
suppress_errors:bool | Undocumented |
Returns | |
Optional[ | Undocumented |
Update an lvalue to point to existing definition in the same scope.
Arguments are similar to "analyze_lvalue".
Assume that an existing name exists.
Parameters | |
lval:NameExpr | Undocumented |
explicit_type:bool | Undocumented |
is_final:bool | Undocumented |
Mark a definition as incomplete (and defer current analysis target).
Also potentially mark the current namespace as incomplete.
name: The name that we weren't able to define (or '*' if the name is unknown) node: The node that refers to the name (definition or lvalue) becomes_typeinfo: Pass this to PlaceholderNode (used by special forms like
named tuples that will create TypeInfos).
Parameters | |
name:str | Undocumented |
node:Node | Undocumented |
becomes_typeinfo:bool | Undocumented |
module_public:bool | Undocumented |
module_hidden:bool | Undocumented |
Undocumented
Parameters | |
name:str | Undocumented |
ctx:Context | Undocumented |
original_ctx:Optional[ | Undocumented |
Undocumented
Parameters | |
name:str | Undocumented |
ctx:Context | Undocumented |
namespace:Optional[ | Undocumented |
Construct an instance of a type with given type arguments.
Return None if a type could not be constructed for the qualified type name. This is possible when the qualified name includes a module name and the module has not been imported.
Parameters | |
fullname:str | Undocumented |
args:Optional[ | Undocumented |
Returns | |
Optional[ | Undocumented |
Add certain special-cased definitions to the builtins module.
Some definitions are too special or fundamental to be processed normally from the AST.
Parameters | |
file_node:MypyFile | Undocumented |
Remove dummy alias definitions such as List = TypeAlias(object) from typing.
They will be replaced with real aliases when corresponding targets are ready.
Parameters | |
file_node:MypyFile | Undocumented |
aliases:Dict[ | Undocumented |
Processing __slots__ if defined in type.
See: https://docs.python.org/3/reference/datamodel.html#slots
Parameters | |
s:AssignmentStmt | Undocumented |
Parameters | |
defn:OverloadedFuncDef | Undocumented |
Undocumented
Parameters | |
imported_id:str | Undocumented |
existing_symbol:SymbolTableNode | Undocumented |
module_symbol:SymbolTableNode | Undocumented |
import_node:ImportBase | Undocumented |
Returns | |
bool | Undocumented |
Undocumented
Parameters | |
node:SymbolTableNode | Undocumented |
module_id:str | Undocumented |
id:str | Undocumented |
imported_id:str | Undocumented |
fullname:str | Undocumented |
module_public:bool | Undocumented |
context:ImportBase | Undocumented |
Propagate module references across assignments.
Recursively handles the simple form of iterable unpacking; doesn't handle advanced unpacking with *rest, dictionary unpacking, etc.
In an expression like x = y = z, z is the rval and lvals will be [x, y].
Parameters | |
lvals:List[ | Undocumented |
rval:Expression | Undocumented |
ctx:AssignmentStmt | Undocumented |
Checks if s declares a ParamSpec; if yes, store it in symbol table.
Return True if this looks like a ParamSpec (maybe with errors), otherwise return False.
In the future, ParamSpec may accept bounds and variance arguments, in which case more aggressive sharing of code with process_typevar_declaration should be pursued.
Parameters | |
s:AssignmentStmt | Undocumented |
Returns | |
bool | Undocumented |
Process a reference targeting placeholder node.
If this is not a final iteration, defer current node, otherwise report an error.
The 'kind' argument indicates if this a name or attribute expression (used for better error message).
Parameters | |
name:str | Undocumented |
kind:str | Undocumented |
ctx:Context | Undocumented |
Check if s declares a TypeVar; it yes, store it in symbol table.
Return True if this looks like a type variable declaration (but maybe with errors), otherwise return False.
Parameters | |
s:AssignmentStmt | Undocumented |
Returns | |
bool | Undocumented |
Undocumented
Parameters | |
args:List[ | Undocumented |
names:List[ | Undocumented |
kinds:List[ | Undocumented |
num_values:int | Undocumented |
context:Context | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
name:str | Undocumented |
Returns | |
str | Undocumented |
Record minimal necessary information about l.h.s. of a special form.
This exists mostly for compatibility with the old semantic analyzer.
Parameters | |
s:AssignmentStmt | Undocumented |
Parameters | |
node:Union[ | Undocumented |
patches:List[ | Undocumented |
final_iteration:bool | Undocumented |
file_node:MypyFile | Undocumented |
options:Options | Undocumented |
active_type:Optional[ | Undocumented |
Parameters | |
file_node:MypyFile | Undocumented |
Undocumented
Parameters | |
import_id:str | Undocumented |
source_id:str | Undocumented |
imported_id:str | Undocumented |
module_public:bool | Undocumented |
module_hidden:bool | Undocumented |
context:Node | Undocumented |
If 'new' conditionally redefine 'previous', set 'previous' as original
We reject straight redefinitions of functions, as they are usually a programming error. For example:
def f(): ... def f(): ... # Error: 'f' redefined
Parameters | |
previous:Optional[ | Undocumented |
new:Union[ | Undocumented |
Returns | |
bool | Undocumented |
Can we already classify this r.h.s. of an assignment or should we wait?
This returns True if we don't have enough information to decide whether an assignment is just a normal variable definition or a special form. Always return False if this is a final iteration. This will typically cause the lvalue to be classified as a variable plus emit an error.
Parameters | |
rv:Expression | Undocumented |
Returns | |
bool | Undocumented |
Var
.Parameters | |
s:AssignmentStmt | Undocumented |
Undocumented
Parameters | |
tvar_scope:Optional[ | Undocumented |
allow_tuple_literal:bool | Undocumented |
allow_unbound_tvars:bool | Undocumented |
allow_placeholder:bool | Undocumented |
allow_required:bool | Undocumented |
report_invalid_types:bool | Undocumented |
Returns | |
TypeAnalyser | Undocumented |
Undocumented
Parameters | |
e:Expression | Undocumented |
Returns | |
Optional[ | Undocumented |
Strip Final[...] if present in an assignment.
This is done to invoke type inference during type checking phase for this
assignment. Also, Final[...] doesn't affect type in any way -- it is rather an
access qualifier for given Var
.
Also perform various consistency checks.
Returns True if Final[...] was present.
Parameters | |
s:AssignmentStmt | Undocumented |
Returns | |
bool | Undocumented |
Make any type variables in the signature of defn explicit.
Update the signature of defn to contain type variable definitions if defn is generic.
Parameters | |
fun_type:CallableType | Undocumented |
defn:FuncItem | Undocumented |
Lookup for special metaclass declarations, and update defn fields accordingly.
Parameters | |
defn:ClassDef | Undocumented |
Analyze a call expression.
Some call expressions are recognized as special forms, including cast(...).
Parameters | |
expr:CallExpr | Undocumented |