module documentation

The semantic analyzer. Bind names to definitions and do various other simple consistency checks. Populate symbol tables. The semantic analyzer also detects special forms which reuse generic syntax such as NamedTuple and cast(). Multiple analysis iterations may be needed to analyze forward references and import cycles. Each iteration "fills in" additional bindings and references until everything has been bound. For example, consider this program: x = 1 y = x Here semantic analysis would detect that the assignment 'x = 1' defines a new variable, the type of which is to be inferred (in a later pass; type inference or type checking is not part of semantic analysis). Also, it would bind both references to 'x' to the same module-level variable (Var) node. The second assignment would also be analyzed, and the type of 'y' marked as being inferred. Semantic analysis of types is implemented in typeanal.py. See semanal_main.py for the top-level logic. Some important properties: * After semantic analysis is complete, no PlaceholderNode and PlaceholderType instances should remain. During semantic analysis, if we encounter one of these, the current target should be deferred. * A TypeInfo is only created once we know certain basic information about a type, such as the MRO, existence of a Tuple base class (e.g., for named tuples), and whether we have a TypedDict. We use a temporary PlaceholderNode node in the symbol table if some such information is missing. * For assignments, we only add a non-placeholder symbol table entry once we know the sort of thing being defined (variable, NamedTuple, type alias, etc.). * Every part of the analysis step must support multiple iterations over the same AST nodes, and each iteration must be able to fill in arbitrary things that were missing or incomplete in previous iterations. * Changes performed by the analysis need to be reversible, since mypy daemon strips and reuses existing ASTs (to improve performance and/or reduce memory use).

Class MakeAnyNonExplicit Undocumented
Class SemanticAnalyzer Semantically analyze parsed mypy files.
Function apply_semantic_analyzer_patches Call patch callbacks in the right order.
Function dummy_context Undocumented
Function find_duplicate If the list has duplicates, return one of the duplicates.
Function is_same_symbol Undocumented
Function is_same_var_from_getattr Do n1 and n2 refer to the same Var derived from module-level __getattr__?
Function is_trivial_body Returns 'true' if the given body is "trivial" -- if it contains just a "pass", "..." (ellipsis), or "raise NotImplementedError()". A trivial body may also start with a statement containing just a string (e...
Function is_valid_replacement Can symbol table node replace an existing one?
Function make_any_non_explicit Replace all Any types within in with Any that has attribute 'explicit' set to False
Function names_modified_by_assignment Return all unqualified (short) names assigned to in an assignment statement.
Function names_modified_in_lvalue Return all NameExpr assignment targets in an Lvalue.
Function refers_to_class_or_function Does semantically analyzed node refer to a class?
Function refers_to_fullname Is node a name or member expression with the given full name?
Function remove_imported_names_from_symtable Remove all imported names from the symbol table of a module.
Function replace_implicit_first_type Undocumented
Constant CORE_BUILTIN_CLASSES Undocumented
Constant FUTURE_IMPORTS Undocumented
Type Variable T Undocumented
def apply_semantic_analyzer_patches(patches: list[tuple[int, Callable[[], None]]]): (source)

Call patch callbacks in the right order. This should happen after semantic analyzer pass 3.

def dummy_context() -> Context: (source)

Undocumented

def find_duplicate(list: list[T]) -> T|None: (source)

If the list has duplicates, return one of the duplicates. Otherwise, return None.

def is_same_symbol(a: SymbolNode|None, b: SymbolNode|None) -> bool: (source)

Undocumented

def is_same_var_from_getattr(n1: SymbolNode|None, n2: SymbolNode|None) -> bool: (source)

Do n1 and n2 refer to the same Var derived from module-level __getattr__?

def is_trivial_body(block: Block) -> bool: (source)

Returns 'true' if the given body is "trivial" -- if it contains just a "pass", "..." (ellipsis), or "raise NotImplementedError()". A trivial body may also start with a statement containing just a string (e.g. a docstring). Note: functions that raise other kinds of exceptions do not count as "trivial". We use this function to help us determine when it's ok to relax certain checks on body, but functions that raise arbitrary exceptions are more likely to do non-trivial work. For example: def halt(self, reason: str = ...) -> NoReturn: raise MyCustomError("Fatal error: " + reason, self.line, self.context) A function that raises just NotImplementedError is much less likely to be this complex.

def is_valid_replacement(old: SymbolTableNode, new: SymbolTableNode) -> bool: (source)

Can symbol table node replace an existing one? These are the only valid cases: 1. Placeholder gets replaced with a non-placeholder 2. Placeholder that isn't known to become type replaced with a placeholder that can become a type

def make_any_non_explicit(t: Type) -> Type: (source)

Replace all Any types within in with Any that has attribute 'explicit' set to False

def names_modified_by_assignment(s: AssignmentStmt) -> list[NameExpr]: (source)

Return all unqualified (short) names assigned to in an assignment statement.

def names_modified_in_lvalue(lvalue: Lvalue) -> list[NameExpr]: (source)

Return all NameExpr assignment targets in an Lvalue.

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

Does semantically analyzed node refer to a class?

def refers_to_fullname(node: Expression, fullnames: str|tuple[str, ...]) -> bool: (source)

Is node a name or member expression with the given full name?

def remove_imported_names_from_symtable(names: SymbolTable, module: str): (source)

Remove all imported names from the symbol table of a module.

def replace_implicit_first_type(sig: FunctionLike, new: Type) -> FunctionLike: (source)

Undocumented

CORE_BUILTIN_CLASSES: list[str] = (source)

Undocumented

Value
['object', 'bool', 'function']
FUTURE_IMPORTS: dict[str, str] = (source)

Undocumented

Value
{'__future__.nested_scopes': 'nested_scopes',
 '__future__.generators': 'generators',
 '__future__.division': 'division',
 '__future__.absolute_import': 'absolute_import',
 '__future__.with_statement': 'with_statement',
 '__future__.print_function': 'print_function',
 '__future__.unicode_literals': 'unicode_literals',
...

Undocumented

Value
TypeVar('T')