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 |
|
Undocumented |
Class |
|
Semantically analyze parsed mypy files. |
Function | apply |
Call patch callbacks in the right order. |
Function | dummy |
Undocumented |
Function | find |
If the list has duplicates, return one of the duplicates. |
Function | is |
Undocumented |
Function | is |
Do n1 and n2 refer to the same Var derived from module-level __getattr__? |
Function | is |
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 |
Can symbol table node replace an existing one? |
Function | make |
Replace all Any types within in with Any that has attribute 'explicit' set to False |
Function | names |
Return all unqualified (short) names assigned to in an assignment statement. |
Function | names |
Return all NameExpr assignment targets in an Lvalue. |
Function | refers |
Does semantically analyzed node refer to a class? |
Function | refers |
Is node a name or member expression with the given full name? |
Function | remove |
Remove all imported names from the symbol table of a module. |
Function | replace |
Undocumented |
Constant | CORE |
Undocumented |
Constant | FUTURE |
Undocumented |
Type Variable | T |
Undocumented |
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.
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
Undocumented
Value |
|