class SemanticAnalyzer(NodeVisitor[
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 |
Add builtin type aliases to typing module. |
Method | add |
Undocumented |
Method | add |
Undocumented |
Method | add |
Manually add implicit definitions of module '__name__' etc. |
Method | add |
Add an alias to an existing symbol through import. |
Method | add |
Add local variable or function. |
Method | add |
Add dependency from trigger to a target. |
Method | add |
Add a symbol table node that reflects a redefinition as a function or a class. |
Method | add |
Add symbol to the currently active symbol table. |
Method | add |
Same as above, but skipping the local namespace. |
Method | add |
Add symbol table node to the currently active symbol table. |
Method | add |
Add full names of type aliases on which the current node depends. |
Method | add |
Typing extensions module does contain some type aliases. |
Method | add |
Add symbol that we don't know what it points to because resolving an import failed. |
Method | adjust |
Adjust the module visibility of globals due to __all__. |
Method | allow |
Undocumented |
Method | already |
Undocumented |
Method | anal |
Semantically analyze a type. |
Method | analyze |
Check if 'rvalue' is a valid type allowed for aliasing (e.g. not a type variable). |
Method | analyze |
Undocumented |
Method | analyze |
Analyze base class types. |
Method | analyze |
Undocumented |
Method | analyze |
Parts of class body analysis that are common to all kinds of class defs. |
Method | analyze |
Undocumented |
Method | analyze |
Undocumented |
Method | analyze |
Analyze type variables declared using Generic[...] or Protocol[...]. |
Method | analyze |
Analyses the 'comp_for' part of comprehensions (part 1). |
Method | analyze |
Analyses the 'comp_for' part of comprehensions (part 2). |
Method | analyze |
Check if s defines an Enum. |
Method | analyze |
Undocumented |
Method | analyze |
Undocumented |
Method | analyze |
Special case 'X = X' in global scope. |
Method | analyze |
Analyze an lvalue or assignment target. |
Method | analyze |
Undocumented |
Method | analyze |
Analyze lvalue that is a member expression. |
Method | analyze |
Analyze an lvalue that targets a name expression. |
Method | analyze |
Check if s defines a namedtuple. |
Method | analyze |
Check if this class can define a named tuple. |
Method | analyze |
Find overload signatures, the implementation, and items with missing @overload. |
Method | analyze |
Undocumented |
Method | analyze |
Analyze a property defined using multiple methods (e.g., using @x.setter). |
Method | analyze |
Return builtins.int if rvalue is an int literal, etc. |
Method | analyze |
Undocumented |
Method | analyze |
Analyze an lvalue or assignment target that is a list or tuple. |
Method | analyze |
Analyze special form -- type application (either direct or via type aliasing). |
Method | analyze |
Analyze type arguments (index) in a type application. |
Method | analyze |
Undocumented |
Method | analyze |
Check if s defines a typed dict. |
Method | analyze |
Undocumented |
Method | analyze |
Undocumented |
Method | analyze |
Analyze types from values expressions in type variable definition. |
Method | apply |
Apply a plugin hook that may infer a more precise definition for a class. |
Method | apply |
Undocumented |
Method | attribute |
Undocumented |
Method | basic |
Undocumented |
Method | bind |
Bind name expression to a symbol table node. |
Method | builtin |
Legacy function -- use named_type() instead. |
Method | calculate |
Calculate method resolution order for a class. |
Method | can |
Is this a valid r.h.s. for an alias definition? |
Method | can |
Like can_be_type_alias(), but simpler and doesn't require fully analyzed rvalue. |
Method | cannot |
Undocumented |
Method | check |
Check if assignment creates a type alias and set it up as needed. |
Method | check |
Check if assignment defines a class variable. |
Method | check |
Undocumented |
Method | check |
Undocumented |
Method | check |
Do basic checks for final declaration on self in __init__. |
Method | check |
Verify that expr has specified number of positional args. |
Method | check |
Undocumented |
Method | check |
Undocumented |
Method | check |
Undocumented |
Method | check |
Checks that the name of a TypeVar or ParamSpec matches its variable. |
Method | check |
Check that assignment expression is not nested within comprehension at class scope. |
Method | class |
Generate type of first argument of class methods from type of self. |
Method | clean |
Remove extra base classes such as Generic and infer type vars. |
Method | configure |
Set up base classes. |
Method | configure |
Undocumented |
Method | correct |
Undocumented |
Method | create |
Undocumented |
Method | create |
Create a dummy variable using module-level __getattr__ return type. |
Method | current |
Undocumented |
Method | current |
Get currently active symbol table. |
Method | defer |
Defer current analysis target to be analyzed again. |
Method | disable |
Prohibit and fix recursive type aliases that are invalid/unsupported. |
Method | enter |
Enter a function, generator or comprehension scope. |
Method | enter |
Undocumented |
Method | expr |
Undocumented |
Method | expr |
Undocumented |
Method | extract |
Undocumented |
Method | fail |
Emit an error message at given location. |
Method | fail |
Undocumented |
Method | file |
Configure analyzer for analyzing targets within a file/class. |
Method | flatten |
Undocumented |
Method | found |
Have we encountered an incomplete reference since starting tracking? |
Method | get |
Return all type variable references in bases. |
Method | get |
Return all type variable references in item type expressions. |
Method | get |
Get declared metaclass from metaclass expression. |
Method | get |
Undocumented |
Method | get |
Look up a symbol from a module. |
Method | get |
Try finding a short simplified textual representation of a base class expression. |
Method | get |
Returns the call expression if `s` is a declaration of `typevarlike_type` (TypeVar or ParamSpec), or None otherwise. |
Method | handle |
Generate errors for overload items without @overload. |
Method | handle |
Generate error about missing overload implementation (only if needed). |
Method | implicit |
Create symbol for a qualified name reference through Any type. |
Method | in |
Should we type-check the current function? |
Method | incomplete |
Undocumented |
Method | infer |
Lookup for special metaclass declarations, and update defn fields accordingly. |
Method | is |
Can a symbol defined in class body accessed at current statement? |
Method | is |
Undocumented |
Method | is |
Check whether a protocol member is annotated. |
Method | is |
Determine if t is a base class of s (but do not use mro). |
Method | is |
Undocumented |
Method | is |
Undocumented |
Method | is |
Undocumented |
Method | is |
Undocumented |
Method | is |
Does this (analyzed or not) type represent the expected Self type for a method? |
Method | is |
Undocumented |
Method | is |
Undocumented |
Method | is |
Undocumented |
Method | is |
Is the specific __future__ feature imported |
Method | is |
Undocumented |
Method | is |
Is a module or class namespace potentially missing some definitions? |
Method | is |
Undocumented |
Method | is |
Does name look like reference to a definition in the current module? |
Method | is |
Undocumented |
Method | is |
Undocumented |
Method | is |
Undocumented |
Method | is |
Are we underneath a function scope, even if we are in a nested class also? |
Method | is |
Is this a r.h.s. for a None alias? |
Method | is |
Check whether the function belongs to the overloaded variants |
Method | is |
Undocumented |
Method | is |
Does memberexpr to refer to an attribute of self? |
Method | is |
Check if a node is defined textually before the current statement |
Method | is |
Does this expression refer to a type? |
Method | is |
Undocumented |
Method | leave |
Restore analyzer state. |
Method | lookup |
Look up an unqualified (no dots) name in all active namespaces. |
Method | lookup |
Undocumented |
Method | lookup |
Lookup a symbol by its fully qualified name. |
Method | lookup |
Lookup a fully qualified name that refers to a module-level definition. |
Method | lookup |
Lookup a qualified name in all activate namespaces. |
Method | lookup |
Undocumented |
Method | make |
Undocumented |
Method | make |
Update an lvalue to point to existing definition in the same scope. |
Method | make |
Return a Var node for an lvalue that is a name expression. |
Method | mark |
Mark a definition as incomplete (and defer current analysis target). |
Method | name |
Undocumented |
Method | name |
Undocumented |
Method | named |
Construct an instance of a builtin type with given type arguments. |
Method | named |
Construct an instance of a type with given type arguments. |
Method | note |
Undocumented |
Method | object |
Undocumented |
Method | parse |
Parse True/False literals. |
Method | parse |
Undocumented |
Method | parse |
Build a DataclassTransformSpec from the arguments passed to the given call to typing.dataclass_transform. |
Method | parse |
Attempt to find the string literal value of the given expression. Returns `None` if no literal value can be found. |
Method | prepare |
Add certain special-cased definitions to the builtins module. |
Method | prepare |
Prepare for the analysis of a class definition. |
Method | prepare |
Prepare a freshly parsed file for semantic analysis. |
Method | prepare |
Check basic signature validity and tweak annotation of self/cls argument. |
Method | prepare |
Remove dummy alias definitions such as List = TypeAlias(object) from typing. |
Method | process |
Export names if argument is a __all__ assignment. |
Method | process |
Undocumented |
Method | process |
Processing ``__slots__`` if defined in type. |
Method | process |
Detect the @final status of an overloaded function (and perform checks). |
Method | process |
Undocumented |
Method | process |
Undocumented |
Method | process |
Propagate module references across assignments. |
Method | process |
Set flags for an overload implementation. |
Method | process |
Checks if s declares a ParamSpec; if yes, store it in symbol table. |
Method | process |
Process a reference targeting placeholder node. |
Method | process |
Undocumented |
Method | process |
Analyze type annotation or infer simple literal type. |
Method | process |
Check if s declares a TypeVar; it yes, store it in symbol table. |
Method | process |
Undocumented |
Method | process |
Checks if s declares a TypeVarTuple; if yes, store it in symbol table. |
Method | qualified |
Make qualified name using current module and enclosing class (if any). |
Method | recalculate |
Undocumented |
Method | record |
Record the encounter of an incomplete reference and defer current analysis target. |
Method | record |
Record minimal necessary information about l.h.s. of a special form. |
Method | refresh |
Refresh a stale target in fine-grained incremental mode. |
Method | refresh |
Reanalyze a stale module top-level in fine-grained incremental mode. |
Method | remove |
Undocumented |
Method | report |
Undocumented |
Method | report |
Undocumented |
Method | schedule |
Undocumented |
Method | set |
Undocumented |
Method | set |
Undocumented |
Method | set |
If 'new' conditionally redefine 'previous', set 'previous' as original |
Method | setup |
Undocumented |
Method | setup |
Setup a (shared) Self type variable for current class. |
Method | setup |
Undocumented |
Method | should |
Can we already classify this r.h.s. of an assignment or should we wait? |
Method | store |
Undocumented |
Method | store |
If this is a locally valid final declaration, set the corresponding flag on `Var`. |
Method | str |
Undocumented |
Method | track |
Return tag that can be used for tracking references to incomplete names. |
Method | translate |
Translate 'dict(x=y, ...)' to {'x': y, ...} and 'dict()' to {}. |
Method | tvar |
Undocumented |
Method | type |
Undocumented |
Method | unwrap |
Strip Final[...] if present in an assignment. |
Method | update |
Make any type variables in the signature of defn explicit. |
Method | verify |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Analyze a call expression. |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Class Variable | __deletable__ |
Undocumented |
Class Variable | global |
Undocumented |
Class Variable | nonlocal |
Undocumented |
Class Variable | wrapped |
Undocumented |
Instance Variable | all |
Undocumented |
Instance Variable | allow |
Undocumented |
Instance Variable | basic |
Undocumented |
Instance Variable | block |
Undocumented |
Instance Variable | cur |
Undocumented |
Instance Variable | cur |
Undocumented |
Instance Variable | deferral |
Undocumented |
Instance Variable | deferred |
Undocumented |
Instance Variable | enum |
Undocumented |
Instance Variable | errors |
Undocumented |
Instance Variable | export |
Undocumented |
Instance Variable | function |
Undocumented |
Instance Variable | globals |
Undocumented |
Instance Variable | imports |
Undocumented |
Instance Variable | incomplete |
Undocumented |
Instance Variable | incomplete |
Undocumented |
Instance Variable | incomplete |
Undocumented |
Instance Variable | is |
Undocumented |
Instance Variable | locals |
Undocumented |
Instance Variable | loop |
Undocumented |
Instance Variable | missing |
Undocumented |
Instance Variable | missing |
Undocumented |
Instance Variable | modules |
Undocumented |
Instance Variable | msg |
Undocumented |
Instance Variable | named |
Undocumented |
Instance Variable | newtype |
Undocumented |
Instance Variable | num |
Undocumented |
Instance Variable | options |
Undocumented |
Instance Variable | patches |
Undocumented |
Instance Variable | plugin |
Undocumented |
Instance Variable | progress |
Undocumented |
Instance Variable | recurse |
Undocumented |
Instance Variable | saved |
Undocumented |
Instance Variable | scope |
Undocumented |
Instance Variable | statement |
Undocumented |
Instance Variable | tvar |
Undocumented |
Instance Variable | type |
Undocumented |
Instance Variable | typed |
Undocumented |
Property | final |
Is this the final iteration of semantic analysis? |
Property | is |
Undocumented |
Property | is |
Undocumented |
Property | type |
Undocumented |
Method | _get |
Undocumented |
Instance Variable | _final |
Undocumented |
Instance Variable | _is |
Undocumented |
Instance Variable | _is |
Undocumented |
Instance Variable | _type |
Undocumented |
Inherited from NodeVisitor
:
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
Method | visit |
Undocumented |
dict[ str, MypyFile]
, missing_modules: set[ str]
, incomplete_namespaces: set[ str]
, errors: Errors
, plugin: Plugin
):
(source)
¶
Construct semantic analyzer. We reuse the same semantic analyzer instance across multiple modules. Args: 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
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]`.
str
, node: SymbolTableNode
, context: ImportBase
, module_public: bool
, module_hidden: bool
):
(source)
¶
Add an alias to an existing symbol through import.
Add dependency from trigger to a target. If the target is not given explicitly, use the current target.
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).
str
, node: SymbolNode
, context: Context
, module_public: bool
= True, module_hidden: bool
= False, can_defer: bool
= True, escape_comprehensions: bool
= False) -> bool
:
(source)
¶
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.
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.
str
, symbol: SymbolTableNode
, context: Context|None
= None, can_defer: bool
= True, escape_comprehensions: bool
= False) -> bool
:
(source)
¶
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? Args: name: short name of symbol symbol: Node to add can_defer: if True, defer current target if adding a placeholder context: error context (see above about None value)
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.
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.
str
, context: Context
, target_name: str|None
, module_public: bool
, module_hidden: bool
):
(source)
¶
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.
str
, ctx: Context
, original_ctx: (SymbolTableNode|SymbolNode)|None
, noun: str
):
(source)
¶
Undocumented
Type
, *, tvar_scope: TypeVarLikeScope|None
= None, allow_tuple_literal: bool
= False, allow_unbound_tvars: bool
= False, allow_placeholder: bool
= False, allow_required: bool
= False, allow_param_spec_literals: bool
= False, report_invalid_types: bool
= True, prohibit_self_type: str|None
= None, allow_type_any: bool
= False, third_pass: bool
= False) -> Type|None
:
(source)
¶
Semantically analyze a type. Args: typ: Type to analyze (if already analyzed, this is a no-op) allow_placeholder: If True, may return PlaceholderType if encountering an incomplete definition third_pass: Unused; only for compatibility with old semantic analyzer 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. NOTE: The caller shouldn't defer even if this returns None or a placeholder type.
str
, rvalue: Expression
, allow_placeholder: bool
= False) -> tuple[ Type|None, list[ TypeVarLikeType], set[ str], list[ str]]
:
(source)
¶
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. A schematic example for the dependencies: A = int B = str analyze_alias(Dict[A, B])[2] == {'__main__.A', '__main__.B'}
list[ Expression]
) -> tuple[ list[ tuple[ ProperType, Expression]], bool]|None
:
(source)
¶
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
Type
) -> tuple[ TypeVarLikeList, bool]|None
:
(source)
¶
Analyze type variables declared using Generic[...] or Protocol[...]. Args: base: Non-analyzed base class Return None if the base class does not declare type variables. Otherwise, return the type variables.
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.
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.
Special case 'X = X' in global scope. This allows supporting some important use cases. Return true if special casing was applied.
Lvalue
, nested: bool
= False, explicit_type: bool
= False, is_final: bool
= False, escape_comprehensions: bool
= False, has_explicit_value: bool
= False):
(source)
¶
Analyze an lvalue or assignment target. Args: 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
Analyze lvalue that is a member expression. Arguments: lval: The target lvalue explicit_type: Assignment has type annotation is_final: Is the target final
NameExpr
, explicit_type: bool
, is_final: bool
, escape_comprehensions: bool
, has_explicit_value: bool
):
(source)
¶
Analyze an lvalue that targets a name expression. Arguments are similar to "analyze_lvalue".
ClassDef
, tvar_defs: list[ TypeVarLikeType]
) -> bool
:
(source)
¶
Check if this class can define a named tuple.
OverloadedFuncDef
) -> tuple[ list[ CallableType], OverloadPart|None, list[ int]]
:
(source)
¶
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.
Analyze a property defined using multiple methods (e.g., using @x.setter). Assume that the first method (@property) has already been analyzed.
Return builtins.int if rvalue is an int literal, etc. If this is a 'Final' context, we return "Literal[...]" instead.
str
, ctx: Context
, original_ctx: (SymbolTableNode|SymbolNode)|None
= None):
(source)
¶
Undocumented
str
, basetype_or_fallback: Instance
, line: int
) -> TypeInfo
:
(source)
¶
Undocumented
ClassDef
, obj_type: Callable[ [], Instance]|None
= None):
(source)
¶
Calculate method resolution order for a class. `obj_type` exists just to fill in empty base class list in case of an error.
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.
Like can_be_type_alias(), but simpler and doesn't require fully analyzed rvalue. Instead, use lvalues/annotations structure to figure out whether this can potentially be a type alias definition, NamedTuple, or TypedDict. Another difference from above function is that we are only interested IndexExpr, CallExpr and OpExpr rvalues, since only those can be potentially recursive (things like `A = A` are never valid).
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.
Do basic checks for final declaration on self in __init__. Additional re-definition checks are performed by `analyze_lvalue`.
Check that assignment expression is not nested within comprehension at class scope. class C: [(j := i) for i in [1, 2, 3]] is a syntax error that is not enforced by Python parser, but at later steps.
ClassDef
, base_type_exprs: list[ Expression]
, context: Context
) -> tuple[ list[ Expression], list[ TypeVarLikeType], bool]
:
(source)
¶
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).
ClassDef
, bases: list[ tuple[ ProperType, Expression]]
):
(source)
¶
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.)
SymbolTableNode
, name: str
, fullname: str
) -> Var|None
:
(source)
¶
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.
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. NOTE: Some methods, such as 'anal_type', 'mark_incomplete' and 'record_incomplete_ref', call this implicitly, or when needed. They are usually preferable to a direct defer() call.
def enter(self, function:
(FuncItem|GeneratorExpr)|DictionaryComprehension
) -> Iterator[ None]
:
(source)
¶
Enter a function, generator or comprehension scope.
Expression
, report_invalid_types: bool
= True, allow_placeholder: bool
= False, allow_type_any: bool
= False) -> Type|None
:
(source)
¶
Undocumented
str
, ctx: Context
, serious: bool
= False, *, code: ErrorCode|None
= None, blocker: bool
= False):
(source)
¶
Emit an error message at given location.
def file_context(self, file_node:
MypyFile
, options: Options
, active_type: TypeInfo|None
= None) -> Iterator[ None]
:
(source)
¶
Configure analyzer for analyzing targets within a file/class. Args: file_node: target file options: options specific to the file active_type: must be the surrounding class to analyze method targets
list[ Expression]
, removed: list[ int]
) -> TypeVarLikeList
:
(source)
¶
Return all type variable references in bases.
Return all type variable references in item type expressions. This is a helper for generic TypedDicts and NamedTuples. Essentially it is a simplified version of the logic we use for ClassDef bases. We duplicate some amount of code, because it is hard to refactor common pieces.
str
, metaclass_expr: Expression|None
) -> tuple[ Instance|None, bool, bool]
:
(source)
¶
Get declared metaclass from metaclass expression. Returns a tuple of three values: * A metaclass instance or None * A boolean indicating whether we should defer * A boolean indicating whether we should set metaclass Any fallback (either for Any metaclass or invalid/dynamic metaclass). The two boolean flags can only be True if instance is None.
AssignmentStmt
, typevarlike_types: tuple[ str, ...]
) -> CallExpr|None
:
(source)
¶
Returns the call expression if `s` is a declaration of `typevarlike_type` (TypeVar or ParamSpec), or None otherwise.
OverloadedFuncDef
, non_overload_indexes: list[ int]
, some_overload_decorators: bool
):
(source)
¶
Generate errors for overload items without @overload. Side effect: remote non-overload items.
SymbolTableNode
, name: str
, parts: list[ str]
, source_type: AnyType
) -> SymbolTableNode
:
(source)
¶
Create symbol for a qualified name reference through Any type.
Should we type-check the current function? - Yes if --check-untyped-defs is set. - Yes outside functions. - Yes in annotated functions. - No otherwise.
Lookup for special metaclass declarations, and update defn fields accordingly. * six.with_metaclass(M, B1, B2, ...) * @six.add_metaclass(M) * future.utils.with_metaclass(M, B1, B2, ...) * past.utils.with_metaclass(M, B1, B2, ...)
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. Also, we allow forward references to type aliases to support recursive types.
Check whether a protocol member is annotated. There are some exceptions that can be left unannotated, like ``__slots__``.
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.
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.
Check if a node is defined textually before the current statement Note that decorated functions' line number are the same as the top decorator.
Does this expression refer to a type? This includes: * Special forms, like Any or Union * Classes (except subscripted enums) * Other type aliases * PlaceholderNodes with becomes_typeinfo=True (these can be not ready class definitions, and not ready aliases). 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().
str
, ctx: Context
, suppress_errors: bool
= False) -> SymbolTableNode|None
:
(source)
¶
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.
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.
str
, ctx: Context
, suppress_errors: bool
= False) -> SymbolTableNode|None
:
(source)
¶
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.
NameExpr
, explicit_type: bool
, is_final: bool
):
(source)
¶
Update an lvalue to point to existing definition in the same scope. Arguments are similar to "analyze_lvalue". Assume that an existing name exists.
NameExpr
, kind: int
, inferred: bool
, has_explicit_value: bool
) -> Var
:
(source)
¶
Return a Var node for an lvalue that is a name expression.
str
, node: Node
, becomes_typeinfo: bool
= False, module_public: bool
= True, module_hidden: bool
= False):
(source)
¶
Mark a definition as incomplete (and defer current analysis target). Also potentially mark the current namespace as incomplete. Args: 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).
str
, ctx: Context
, original_ctx: (SymbolTableNode|SymbolNode)|None
= None):
(source)
¶
Undocumented
str
, args: list[ Type]|None
= None) -> Instance|None
:
(source)
¶
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.
Expression
) -> tuple[ str, ...]
:
(source)
¶
Undocumented
Build a DataclassTransformSpec from the arguments passed to the given call to typing.dataclass_transform.
Attempt to find the string literal value of the given expression. Returns `None` if no literal value can be found.
Add certain special-cased definitions to the builtins module. Some definitions are too special or fundamental to be processed normally from the AST.
ClassDef
, info: TypeInfo|None
= None, custom_names: bool
= False):
(source)
¶
Prepare for the analysis of a class definition. Create an empty TypeInfo and store it in a symbol table, or if the 'info' argument is provided, store it instead (used for magic type definitions).
Remove dummy alias definitions such as List = TypeAlias(object) from typing. They will be replaced with real aliases when corresponding targets are ready.
Processing ``__slots__`` if defined in type. See: https://docs.python.org/3/reference/datamodel.html#slots
str
, existing_symbol: SymbolTableNode
, module_symbol: SymbolTableNode
, import_node: ImportBase
) -> bool
:
(source)
¶
Undocumented
SymbolTableNode
, module_id: str
, id: str
, imported_id: str
, fullname: str
, module_public: bool
, context: ImportBase
):
(source)
¶
Undocumented
list[ Lvalue]
, rval: Expression
, ctx: AssignmentStmt
):
(source)
¶
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].
Set flags for an overload implementation. Currently, this checks for a trivial body in protocols classes, where it makes the method implicitly abstract.
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.
str|None
, kind: str
, ctx: Context
, force_progress: bool
= False):
(source)
¶
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).
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.
list[ Expression]
, names: list[ str|None]
, kinds: list[ ArgKind]
, num_values: int
, context: Context
) -> tuple[ int, Type]|None
:
(source)
¶
Undocumented
Checks if s declares a TypeVarTuple; if yes, store it in symbol table. Return True if this looks like a TypeVarTuple (maybe with errors), otherwise return False.
Record minimal necessary information about l.h.s. of a special form. This exists mostly for compatibility with the old semantic analyzer.
(MypyFile|FuncDef)|OverloadedFuncDef
, patches: list[ tuple[ int, Callable[ [], None]]]
, final_iteration: bool
, file_node: MypyFile
, options: Options
, active_type: TypeInfo|None
= None):
(source)
¶
Refresh a stale target in fine-grained incremental mode.
str
, source_id: str
, imported_id: str
, module_public: bool
, module_hidden: bool
, context: Node
, add_unknown_imported_symbol: bool
= True):
(source)
¶
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
Setup a (shared) Self type variable for current class. We intentionally don't add it to the class symbol table, so it can be accessed only by mypy and will not cause clashes with user defined names.
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.
Translate 'dict(x=y, ...)' to {'x': y, ...} and 'dict()' to {}. For other variants of dict(...), return None.
TypeVarLikeScope|None
= None, allow_tuple_literal: bool
= False, allow_unbound_tvars: bool
= False, allow_placeholder: bool
= False, allow_required: bool
= False, allow_param_spec_literals: bool
= False, report_invalid_types: bool
= True, prohibit_self_type: str|None
= None, allow_type_any: bool
= False) -> TypeAnalyser
:
(source)
¶
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.
CallableType
, defn: FuncItem
) -> bool
:
(source)
¶
Make any type variables in the signature of defn explicit. Update the signature of defn to contain type variable definitions if defn is generic. Return True, if the signature contains typing.Self type, or False otherwise.
mypy.visitor.NodeVisitor.visit_call_expr
Analyze a call expression. Some call expressions are recognized as special forms, including cast(...).
str
, symbol_node: SymbolNode|None
, context: Context
) -> SymbolNode|None
:
(source)
¶
Undocumented