1"""The semantic analyzer.
2
3Bind names to definitions and do various other simple consistency
4checks.  Populate symbol tables.  The semantic analyzer also detects
5special forms which reuse generic syntax such as NamedTuple and
6cast().  Multiple analysis iterations may be needed to analyze forward
7references and import cycles. Each iteration "fills in" additional
8bindings and references until everything has been bound.
9
10For example, consider this program:
11
12  x = 1
13  y = x
14
15Here semantic analysis would detect that the assignment 'x = 1'
16defines a new variable, the type of which is to be inferred (in a
17later pass; type inference or type checking is not part of semantic
18analysis).  Also, it would bind both references to 'x' to the same
19module-level variable (Var) node.  The second assignment would also
20be analyzed, and the type of 'y' marked as being inferred.
21
22Semantic analysis of types is implemented in typeanal.py.
23
24See semanal_main.py for the top-level logic.
25
26Some important properties:
27
28* After semantic analysis is complete, no PlaceholderNode and
29  PlaceholderType instances should remain. During semantic analysis,
30  if we encounter one of these, the current target should be deferred.
31
32* A TypeInfo is only created once we know certain basic information about
33  a type, such as the MRO, existence of a Tuple base class (e.g., for named
34  tuples), and whether we have a TypedDict. We use a temporary
35  PlaceholderNode node in the symbol table if some such information is
36  missing.
37
38* For assignments, we only add a non-placeholder symbol table entry once
39  we know the sort of thing being defined (variable, NamedTuple, type alias,
40  etc.).
41
42* Every part of the analysis step must support multiple iterations over
43  the same AST nodes, and each iteration must be able to fill in arbitrary
44  things that were missing or incomplete in previous iterations.
45
46* Changes performed by the analysis need to be reversible, since mypy
47  daemon strips and reuses existing ASTs (to improve performance and/or
48  reduce memory use).
49"""
50
51from contextlib import contextmanager
52
53from typing import (
54    List, Dict, Set, Tuple, cast, TypeVar, Union, Optional, Callable, Iterator, Iterable
55)
56from typing_extensions import Final
57
58from mypy.nodes import (
59    MypyFile, TypeInfo, Node, AssignmentStmt, FuncDef, OverloadedFuncDef,
60    ClassDef, Var, GDEF, FuncItem, Import, Expression, Lvalue,
61    ImportFrom, ImportAll, Block, LDEF, NameExpr, MemberExpr,
62    IndexExpr, TupleExpr, ListExpr, ExpressionStmt, ReturnStmt,
63    RaiseStmt, AssertStmt, OperatorAssignmentStmt, WhileStmt,
64    ForStmt, BreakStmt, ContinueStmt, IfStmt, TryStmt, WithStmt, DelStmt,
65    GlobalDecl, SuperExpr, DictExpr, CallExpr, RefExpr, OpExpr, UnaryExpr,
66    SliceExpr, CastExpr, RevealExpr, TypeApplication, Context, SymbolTable,
67    SymbolTableNode, ListComprehension, GeneratorExpr,
68    LambdaExpr, MDEF, Decorator, SetExpr, TypeVarExpr,
69    StrExpr, BytesExpr, PrintStmt, ConditionalExpr, PromoteExpr,
70    ComparisonExpr, StarExpr, ARG_POS, ARG_NAMED, type_aliases,
71    YieldFromExpr, NamedTupleExpr, NonlocalDecl, SymbolNode,
72    SetComprehension, DictionaryComprehension, TypeAlias, TypeAliasExpr,
73    YieldExpr, ExecStmt, BackquoteExpr, ImportBase, AwaitExpr,
74    IntExpr, FloatExpr, UnicodeExpr, TempNode, OverloadPart,
75    PlaceholderNode, COVARIANT, CONTRAVARIANT, INVARIANT,
76    get_nongen_builtins, get_member_expr_fullname, REVEAL_TYPE,
77    REVEAL_LOCALS, is_final_node, TypedDictExpr, type_aliases_source_versions,
78    EnumCallExpr, RUNTIME_PROTOCOL_DECOS, FakeExpression, Statement, AssignmentExpr,
79    ParamSpecExpr
80)
81from mypy.tvar_scope import TypeVarLikeScope
82from mypy.typevars import fill_typevars
83from mypy.visitor import NodeVisitor
84from mypy.errors import Errors, report_internal_error
85from mypy.messages import (
86    best_matches, MessageBuilder, pretty_seq, SUGGESTED_TEST_FIXTURES, TYPES_FOR_UNIMPORTED_HINTS
87)
88from mypy.errorcodes import ErrorCode
89from mypy import message_registry, errorcodes as codes
90from mypy.types import (
91    FunctionLike, UnboundType, TypeVarDef, TupleType, UnionType, StarType,
92    CallableType, Overloaded, Instance, Type, AnyType, LiteralType, LiteralValue,
93    TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
94    get_proper_type, get_proper_types, TypeAliasType)
95from mypy.typeops import function_type
96from mypy.type_visitor import TypeQuery
97from mypy.nodes import implicit_module_attrs
98from mypy.typeanal import (
99    TypeAnalyser, analyze_type_alias, no_subscript_builtin_alias,
100    TypeVarLikeQuery, TypeVarLikeList, remove_dups, has_any_from_unimported_type,
101    check_for_explicit_any, type_constructors, fix_instance_types
102)
103from mypy.exprtotype import expr_to_unanalyzed_type, TypeTranslationError
104from mypy.options import Options
105from mypy.plugin import (
106    Plugin, ClassDefContext, SemanticAnalyzerPluginInterface,
107    DynamicClassDefContext
108)
109from mypy.util import correct_relative_import, unmangle, module_prefix, is_typeshed_file
110from mypy.scope import Scope
111from mypy.semanal_shared import (
112    SemanticAnalyzerInterface, set_callable_name, calculate_tuple_fallback, PRIORITY_FALLBACKS
113)
114from mypy.semanal_namedtuple import NamedTupleAnalyzer
115from mypy.semanal_typeddict import TypedDictAnalyzer
116from mypy.semanal_enum import EnumCallAnalyzer
117from mypy.semanal_newtype import NewTypeAnalyzer
118from mypy.reachability import (
119    infer_reachability_of_if_statement, infer_condition_value, ALWAYS_FALSE, ALWAYS_TRUE,
120    MYPY_TRUE, MYPY_FALSE
121)
122from mypy.mro import calculate_mro, MroError
123
124T = TypeVar('T')
125
126
127FUTURE_IMPORTS = {
128    '__future__.nested_scopes': 'nested_scopes',
129    '__future__.generators': 'generators',
130    '__future__.division': 'division',
131    '__future__.absolute_import': 'absolute_import',
132    '__future__.with_statement': 'with_statement',
133    '__future__.print_function': 'print_function',
134    '__future__.unicode_literals': 'unicode_literals',
135    '__future__.barry_as_FLUFL': 'barry_as_FLUFL',
136    '__future__.generator_stop': 'generator_stop',
137    '__future__.annotations': 'annotations',
138}  # type: Final
139
140
141# Special cased built-in classes that are needed for basic functionality and need to be
142# available very early on.
143CORE_BUILTIN_CLASSES = ['object', 'bool', 'function']  # type: Final
144
145
146# Used for tracking incomplete references
147Tag = int
148
149
150class SemanticAnalyzer(NodeVisitor[None],
151                       SemanticAnalyzerInterface,
152                       SemanticAnalyzerPluginInterface):
153    """Semantically analyze parsed mypy files.
154
155    The analyzer binds names and does various consistency checks for an
156    AST. Note that type checking is performed as a separate pass.
157    """
158
159    # Module name space
160    modules = None  # type: Dict[str, MypyFile]
161    # Global name space for current module
162    globals = None  # type: SymbolTable
163    # Names declared using "global" (separate set for each scope)
164    global_decls = None  # type: List[Set[str]]
165    # Names declated using "nonlocal" (separate set for each scope)
166    nonlocal_decls = None  # type: List[Set[str]]
167    # Local names of function scopes; None for non-function scopes.
168    locals = None  # type: List[Optional[SymbolTable]]
169    # Whether each scope is a comprehension scope.
170    is_comprehension_stack = None  # type: List[bool]
171    # Nested block depths of scopes
172    block_depth = None  # type: List[int]
173    # TypeInfo of directly enclosing class (or None)
174    type = None  # type: Optional[TypeInfo]
175    # Stack of outer classes (the second tuple item contains tvars).
176    type_stack = None  # type: List[Optional[TypeInfo]]
177    # Type variables bound by the current scope, be it class or function
178    tvar_scope = None  # type: TypeVarLikeScope
179    # Per-module options
180    options = None  # type: Options
181
182    # Stack of functions being analyzed
183    function_stack = None  # type: List[FuncItem]
184
185    # Set to True if semantic analysis defines a name, or replaces a
186    # placeholder definition. If some iteration makes no progress,
187    # there can be at most one additional final iteration (see below).
188    progress = False
189    deferred = False  # Set to true if another analysis pass is needed
190    incomplete = False  # Set to true if current module namespace is missing things
191    # Is this the final iteration of semantic analysis (where we report
192    # unbound names due to cyclic definitions and should not defer)?
193    _final_iteration = False
194    # These names couldn't be added to the symbol table due to incomplete deps.
195    # Note that missing names are per module, _not_ per namespace. This means that e.g.
196    # a missing name at global scope will block adding same name at a class scope.
197    # This should not affect correctness and is purely a performance issue,
198    # since it can cause unnecessary deferrals. These are represented as
199    # PlaceholderNodes in the symbol table. We use this to ensure that the first
200    # definition takes precedence even if it's incomplete.
201    #
202    # Note that a star import adds a special name '*' to the set, this blocks
203    # adding _any_ names in the current file.
204    missing_names = None  # type: List[Set[str]]
205    # Callbacks that will be called after semantic analysis to tweak things.
206    patches = None  # type: List[Tuple[int, Callable[[], None]]]
207    loop_depth = 0         # Depth of breakable loops
208    cur_mod_id = ''        # Current module id (or None) (phase 2)
209    _is_stub_file = False   # Are we analyzing a stub file?
210    _is_typeshed_stub_file = False  # Are we analyzing a typeshed stub file?
211    imports = None  # type: Set[str]  # Imported modules (during phase 2 analysis)
212    # Note: some imports (and therefore dependencies) might
213    # not be found in phase 1, for example due to * imports.
214    errors = None  # type: Errors     # Keeps track of generated errors
215    plugin = None  # type: Plugin     # Mypy plugin for special casing of library features
216    statement = None  # type: Optional[Statement]  # Statement/definition being analyzed
217    future_import_flags = None  # type: Set[str]
218
219    # Mapping from 'async def' function definitions to their return type wrapped as a
220    # 'Coroutine[Any, Any, T]'. Used to keep track of whether a function definition's
221    # return type has already been wrapped, by checking if the function definition's
222    # type is stored in this mapping and that it still matches.
223    wrapped_coro_return_types = {}  # type: Dict[FuncDef, Type]
224
225    def __init__(self,
226                 modules: Dict[str, MypyFile],
227                 missing_modules: Set[str],
228                 incomplete_namespaces: Set[str],
229                 errors: Errors,
230                 plugin: Plugin) -> None:
231        """Construct semantic analyzer.
232
233        We reuse the same semantic analyzer instance across multiple modules.
234
235        Args:
236            modules: Global modules dictionary
237            missing_modules: Modules that could not be imported encountered so far
238            incomplete_namespaces: Namespaces that are being populated during semantic analysis
239                (can contain modules and classes within the current SCC; mutated by the caller)
240            errors: Report analysis errors using this instance
241        """
242        self.locals = [None]
243        self.is_comprehension_stack = [False]
244        # Saved namespaces from previous iteration. Every top-level function/method body is
245        # analyzed in several iterations until all names are resolved. We need to save
246        # the local namespaces for the top level function and all nested functions between
247        # these iterations. See also semanal_main.process_top_level_function().
248        self.saved_locals = {} \
249            # type: Dict[Union[FuncItem, GeneratorExpr, DictionaryComprehension], SymbolTable]
250        self.imports = set()
251        self.type = None
252        self.type_stack = []
253        # Are the namespaces of classes being processed complete?
254        self.incomplete_type_stack = []  # type: List[bool]
255        self.tvar_scope = TypeVarLikeScope()
256        self.function_stack = []
257        self.block_depth = [0]
258        self.loop_depth = 0
259        self.errors = errors
260        self.modules = modules
261        self.msg = MessageBuilder(errors, modules)
262        self.missing_modules = missing_modules
263        self.missing_names = [set()]
264        # These namespaces are still in process of being populated. If we encounter a
265        # missing name in these namespaces, we need to defer the current analysis target,
266        # since it's possible that the name will be there once the namespace is complete.
267        self.incomplete_namespaces = incomplete_namespaces
268        self.all_exports = []  # type: List[str]
269        # Map from module id to list of explicitly exported names (i.e. names in __all__).
270        self.export_map = {}  # type: Dict[str, List[str]]
271        self.plugin = plugin
272        # If True, process function definitions. If False, don't. This is used
273        # for processing module top levels in fine-grained incremental mode.
274        self.recurse_into_functions = True
275        self.scope = Scope()
276
277        # Trace line numbers for every file where deferral happened during analysis of
278        # current SCC or top-level function.
279        self.deferral_debug_context = []  # type: List[Tuple[str, int]]
280
281        self.future_import_flags = set()  # type: Set[str]
282
283    # mypyc doesn't properly handle implementing an abstractproperty
284    # with a regular attribute so we make them properties
285    @property
286    def is_stub_file(self) -> bool:
287        return self._is_stub_file
288
289    @property
290    def is_typeshed_stub_file(self) -> bool:
291        return self._is_typeshed_stub_file
292
293    @property
294    def final_iteration(self) -> bool:
295        return self._final_iteration
296
297    #
298    # Preparing module (performed before semantic analysis)
299    #
300
301    def prepare_file(self, file_node: MypyFile) -> None:
302        """Prepare a freshly parsed file for semantic analysis."""
303        if 'builtins' in self.modules:
304            file_node.names['__builtins__'] = SymbolTableNode(GDEF,
305                                                              self.modules['builtins'])
306        if file_node.fullname == 'builtins':
307            self.prepare_builtins_namespace(file_node)
308        if file_node.fullname == 'typing':
309            self.prepare_typing_namespace(file_node)
310
311    def prepare_typing_namespace(self, file_node: MypyFile) -> None:
312        """Remove dummy alias definitions such as List = TypeAlias(object) from typing.
313
314        They will be replaced with real aliases when corresponding targets are ready.
315        """
316        # This is all pretty unfortunate. typeshed now has a
317        # sys.version_info check for OrderedDict, and we shouldn't
318        # take it out, because it is correct and a typechecker should
319        # use that as a source of truth. But instead we rummage
320        # through IfStmts to remove the info first.  (I tried to
321        # remove this whole machinery and ran into issues with the
322        # builtins/typing import cycle.)
323        def helper(defs: List[Statement]) -> None:
324            for stmt in defs.copy():
325                if isinstance(stmt, IfStmt):
326                    for body in stmt.body:
327                        helper(body.body)
328                    if stmt.else_body:
329                        helper(stmt.else_body.body)
330                if (isinstance(stmt, AssignmentStmt) and len(stmt.lvalues) == 1 and
331                        isinstance(stmt.lvalues[0], NameExpr)):
332                    # Assignment to a simple name, remove it if it is a dummy alias.
333                    if 'typing.' + stmt.lvalues[0].name in type_aliases:
334                        defs.remove(stmt)
335
336        helper(file_node.defs)
337
338    def prepare_builtins_namespace(self, file_node: MypyFile) -> None:
339        """Add certain special-cased definitions to the builtins module.
340
341        Some definitions are too special or fundamental to be processed
342        normally from the AST.
343        """
344        names = file_node.names
345
346        # Add empty definition for core built-in classes, since they are required for basic
347        # operation. These will be completed later on.
348        for name in CORE_BUILTIN_CLASSES:
349            cdef = ClassDef(name, Block([]))  # Dummy ClassDef, will be replaced later
350            info = TypeInfo(SymbolTable(), cdef, 'builtins')
351            info._fullname = 'builtins.%s' % name
352            names[name] = SymbolTableNode(GDEF, info)
353
354        bool_info = names['bool'].node
355        assert isinstance(bool_info, TypeInfo)
356        bool_type = Instance(bool_info, [])
357
358        special_var_types = [
359            ('None', NoneType()),
360            # reveal_type is a mypy-only function that gives an error with
361            # the type of its arg.
362            ('reveal_type', AnyType(TypeOfAny.special_form)),
363            # reveal_locals is a mypy-only function that gives an error with the types of
364            # locals
365            ('reveal_locals', AnyType(TypeOfAny.special_form)),
366            ('True', bool_type),
367            ('False', bool_type),
368            ('__debug__', bool_type),
369        ]  # type: List[Tuple[str, Type]]
370
371        for name, typ in special_var_types:
372            v = Var(name, typ)
373            v._fullname = 'builtins.%s' % name
374            file_node.names[name] = SymbolTableNode(GDEF, v)
375
376    #
377    # Analyzing a target
378    #
379
380    def refresh_partial(self,
381                        node: Union[MypyFile, FuncDef, OverloadedFuncDef],
382                        patches: List[Tuple[int, Callable[[], None]]],
383                        final_iteration: bool,
384                        file_node: MypyFile,
385                        options: Options,
386                        active_type: Optional[TypeInfo] = None) -> None:
387        """Refresh a stale target in fine-grained incremental mode."""
388        self.patches = patches
389        self.deferred = False
390        self.incomplete = False
391        self._final_iteration = final_iteration
392        self.missing_names[-1] = set()
393
394        with self.file_context(file_node, options, active_type):
395            if isinstance(node, MypyFile):
396                self.refresh_top_level(node)
397            else:
398                self.recurse_into_functions = True
399                self.accept(node)
400        del self.patches
401
402    def refresh_top_level(self, file_node: MypyFile) -> None:
403        """Reanalyze a stale module top-level in fine-grained incremental mode."""
404        self.recurse_into_functions = False
405        self.add_implicit_module_attrs(file_node)
406        for d in file_node.defs:
407            self.accept(d)
408        if file_node.fullname == 'typing':
409            self.add_builtin_aliases(file_node)
410        self.adjust_public_exports()
411        self.export_map[self.cur_mod_id] = self.all_exports
412        self.all_exports = []
413
414    def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
415        """Manually add implicit definitions of module '__name__' etc."""
416        for name, t in implicit_module_attrs.items():
417            # unicode docstrings should be accepted in Python 2
418            if name == '__doc__':
419                if self.options.python_version >= (3, 0):
420                    typ = UnboundType('__builtins__.str')  # type: Type
421                else:
422                    typ = UnionType([UnboundType('__builtins__.str'),
423                                     UnboundType('__builtins__.unicode')])
424            else:
425                assert t is not None, 'type should be specified for {}'.format(name)
426                typ = UnboundType(t)
427
428            existing = file_node.names.get(name)
429            if existing is not None and not isinstance(existing.node, PlaceholderNode):
430                # Already exists.
431                continue
432
433            an_type = self.anal_type(typ)
434            if an_type:
435                var = Var(name, an_type)
436                var._fullname = self.qualified_name(name)
437                var.is_ready = True
438                self.add_symbol(name, var, dummy_context())
439            else:
440                self.add_symbol(name,
441                                PlaceholderNode(self.qualified_name(name), file_node, -1),
442                                dummy_context())
443
444    def add_builtin_aliases(self, tree: MypyFile) -> None:
445        """Add builtin type aliases to typing module.
446
447        For historical reasons, the aliases like `List = list` are not defined
448        in typeshed stubs for typing module. Instead we need to manually add the
449        corresponding nodes on the fly. We explicitly mark these aliases as normalized,
450        so that a user can write `typing.List[int]`.
451        """
452        assert tree.fullname == 'typing'
453        for alias, target_name in type_aliases.items():
454            if type_aliases_source_versions[alias] > self.options.python_version:
455                # This alias is not available on this Python version.
456                continue
457            name = alias.split('.')[-1]
458            if name in tree.names and not isinstance(tree.names[name].node, PlaceholderNode):
459                continue
460            tag = self.track_incomplete_refs()
461            n = self.lookup_fully_qualified_or_none(target_name)
462            if n:
463                if isinstance(n.node, PlaceholderNode):
464                    self.mark_incomplete(name, tree)
465                else:
466                    # Found built-in class target. Create alias.
467                    target = self.named_type_or_none(target_name, [])
468                    assert target is not None
469                    # Transform List to List[Any], etc.
470                    fix_instance_types(target, self.fail, self.note, self.options.python_version)
471                    alias_node = TypeAlias(target, alias,
472                                           line=-1, column=-1,  # there is no context
473                                           no_args=True, normalized=True)
474                    self.add_symbol(name, alias_node, tree)
475            elif self.found_incomplete_ref(tag):
476                # Built-in class target may not ready yet -- defer.
477                self.mark_incomplete(name, tree)
478            else:
479                # Test fixtures may be missing some builtin classes, which is okay.
480                # Kill the placeholder if there is one.
481                if name in tree.names:
482                    assert isinstance(tree.names[name].node, PlaceholderNode)
483                    del tree.names[name]
484
485    def adjust_public_exports(self) -> None:
486        """Adjust the module visibility of globals due to __all__."""
487        if '__all__' in self.globals:
488            for name, g in self.globals.items():
489                # Being included in __all__ explicitly exports and makes public.
490                if name in self.all_exports:
491                    g.module_public = True
492                    g.module_hidden = False
493                # But when __all__ is defined, and a symbol is not included in it,
494                # it cannot be public.
495                else:
496                    g.module_public = False
497
498    @contextmanager
499    def file_context(self,
500                     file_node: MypyFile,
501                     options: Options,
502                     active_type: Optional[TypeInfo] = None) -> Iterator[None]:
503        """Configure analyzer for analyzing targets within a file/class.
504
505        Args:
506            file_node: target file
507            options: options specific to the file
508            active_type: must be the surrounding class to analyze method targets
509        """
510        scope = self.scope
511        self.options = options
512        self.errors.set_file(file_node.path, file_node.fullname, scope=scope)
513        self.cur_mod_node = file_node
514        self.cur_mod_id = file_node.fullname
515        scope.enter_file(self.cur_mod_id)
516        self._is_stub_file = file_node.path.lower().endswith('.pyi')
517        self._is_typeshed_stub_file = is_typeshed_file(file_node.path)
518        self.globals = file_node.names
519        self.tvar_scope = TypeVarLikeScope()
520
521        self.named_tuple_analyzer = NamedTupleAnalyzer(options, self)
522        self.typed_dict_analyzer = TypedDictAnalyzer(options, self, self.msg)
523        self.enum_call_analyzer = EnumCallAnalyzer(options, self)
524        self.newtype_analyzer = NewTypeAnalyzer(options, self, self.msg)
525
526        # Counter that keeps track of references to undefined things potentially caused by
527        # incomplete namespaces.
528        self.num_incomplete_refs = 0
529
530        if active_type:
531            self.incomplete_type_stack.append(False)
532            scope.enter_class(active_type)
533            self.enter_class(active_type.defn.info)
534            for tvar in active_type.defn.type_vars:
535                self.tvar_scope.bind_existing(tvar)
536
537        yield
538
539        if active_type:
540            scope.leave()
541            self.leave_class()
542            self.type = None
543            self.incomplete_type_stack.pop()
544        scope.leave()
545        del self.options
546
547    #
548    # Functions
549    #
550
551    def visit_func_def(self, defn: FuncDef) -> None:
552        self.statement = defn
553
554        # Visit default values because they may contain assignment expressions.
555        for arg in defn.arguments:
556            if arg.initializer:
557                arg.initializer.accept(self)
558
559        defn.is_conditional = self.block_depth[-1] > 0
560
561        # Set full names even for those definitions that aren't added
562        # to a symbol table. For example, for overload items.
563        defn._fullname = self.qualified_name(defn.name)
564
565        # We don't add module top-level functions to symbol tables
566        # when we analyze their bodies in the second phase on analysis,
567        # since they were added in the first phase. Nested functions
568        # get always added, since they aren't separate targets.
569        if not self.recurse_into_functions or len(self.function_stack) > 0:
570            if not defn.is_decorated and not defn.is_overload:
571                self.add_function_to_symbol_table(defn)
572
573        if not self.recurse_into_functions:
574            return
575
576        with self.scope.function_scope(defn):
577            self.analyze_func_def(defn)
578
579    def analyze_func_def(self, defn: FuncDef) -> None:
580        self.function_stack.append(defn)
581
582        if defn.type:
583            assert isinstance(defn.type, CallableType)
584            self.update_function_type_variables(defn.type, defn)
585        self.function_stack.pop()
586
587        if self.is_class_scope():
588            # Method definition
589            assert self.type is not None
590            defn.info = self.type
591            if defn.type is not None and defn.name in ('__init__', '__init_subclass__'):
592                assert isinstance(defn.type, CallableType)
593                if isinstance(get_proper_type(defn.type.ret_type), AnyType):
594                    defn.type = defn.type.copy_modified(ret_type=NoneType())
595            self.prepare_method_signature(defn, self.type)
596
597        # Analyze function signature
598        with self.tvar_scope_frame(self.tvar_scope.method_frame()):
599            if defn.type:
600                self.check_classvar_in_signature(defn.type)
601                assert isinstance(defn.type, CallableType)
602                # Signature must be analyzed in the surrounding scope so that
603                # class-level imported names and type variables are in scope.
604                analyzer = self.type_analyzer()
605                tag = self.track_incomplete_refs()
606                result = analyzer.visit_callable_type(defn.type, nested=False)
607                # Don't store not ready types (including placeholders).
608                if self.found_incomplete_ref(tag) or has_placeholder(result):
609                    self.defer(defn)
610                    return
611                assert isinstance(result, ProperType)
612                defn.type = result
613                self.add_type_alias_deps(analyzer.aliases_used)
614                self.check_function_signature(defn)
615                if isinstance(defn, FuncDef):
616                    assert isinstance(defn.type, CallableType)
617                    defn.type = set_callable_name(defn.type, defn)
618
619        self.analyze_arg_initializers(defn)
620        self.analyze_function_body(defn)
621        if (defn.is_coroutine and
622                isinstance(defn.type, CallableType) and
623                self.wrapped_coro_return_types.get(defn) != defn.type):
624            if defn.is_async_generator:
625                # Async generator types are handled elsewhere
626                pass
627            else:
628                # A coroutine defined as `async def foo(...) -> T: ...`
629                # has external return type `Coroutine[Any, Any, T]`.
630                any_type = AnyType(TypeOfAny.special_form)
631                ret_type = self.named_type_or_none('typing.Coroutine',
632                                                   [any_type, any_type, defn.type.ret_type])
633                assert ret_type is not None, "Internal error: typing.Coroutine not found"
634                defn.type = defn.type.copy_modified(ret_type=ret_type)
635                self.wrapped_coro_return_types[defn] = defn.type
636
637    def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None:
638        """Check basic signature validity and tweak annotation of self/cls argument."""
639        # Only non-static methods are special.
640        functype = func.type
641        if not func.is_static:
642            if func.name in ['__init_subclass__', '__class_getitem__']:
643                func.is_class = True
644            if not func.arguments:
645                self.fail('Method must have at least one argument', func)
646            elif isinstance(functype, CallableType):
647                self_type = get_proper_type(functype.arg_types[0])
648                if isinstance(self_type, AnyType):
649                    leading_type = fill_typevars(info)  # type: Type
650                    if func.is_class or func.name == '__new__':
651                        leading_type = self.class_type(leading_type)
652                    func.type = replace_implicit_first_type(functype, leading_type)
653
654    def set_original_def(self, previous: Optional[Node], new: Union[FuncDef, Decorator]) -> bool:
655        """If 'new' conditionally redefine 'previous', set 'previous' as original
656
657        We reject straight redefinitions of functions, as they are usually
658        a programming error. For example:
659
660          def f(): ...
661          def f(): ...  # Error: 'f' redefined
662        """
663        if isinstance(new, Decorator):
664            new = new.func
665        if isinstance(previous, (FuncDef, Var, Decorator)) and new.is_conditional:
666            new.original_def = previous
667            return True
668        else:
669            return False
670
671    def update_function_type_variables(self, fun_type: CallableType, defn: FuncItem) -> None:
672        """Make any type variables in the signature of defn explicit.
673
674        Update the signature of defn to contain type variable definitions
675        if defn is generic.
676        """
677        with self.tvar_scope_frame(self.tvar_scope.method_frame()):
678            a = self.type_analyzer()
679            fun_type.variables = a.bind_function_type_variables(fun_type, defn)
680
681    def visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
682        self.statement = defn
683        self.add_function_to_symbol_table(defn)
684
685        if not self.recurse_into_functions:
686            return
687
688        # NB: Since _visit_overloaded_func_def will call accept on the
689        # underlying FuncDefs, the function might get entered twice.
690        # This is fine, though, because only the outermost function is
691        # used to compute targets.
692        with self.scope.function_scope(defn):
693            self.analyze_overloaded_func_def(defn)
694
695    def analyze_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
696        # OverloadedFuncDef refers to any legitimate situation where you have
697        # more than one declaration for the same function in a row.  This occurs
698        # with a @property with a setter or a deleter, and for a classic
699        # @overload.
700
701        defn._fullname = self.qualified_name(defn.name)
702        # TODO: avoid modifying items.
703        defn.items = defn.unanalyzed_items.copy()
704
705        first_item = defn.items[0]
706        first_item.is_overload = True
707        first_item.accept(self)
708
709        if isinstance(first_item, Decorator) and first_item.func.is_property:
710            # This is a property.
711            first_item.func.is_overload = True
712            self.analyze_property_with_multi_part_definition(defn)
713            typ = function_type(first_item.func, self.builtin_type('builtins.function'))
714            assert isinstance(typ, CallableType)
715            types = [typ]
716        else:
717            # This is an a normal overload. Find the item signatures, the
718            # implementation (if outside a stub), and any missing @overload
719            # decorators.
720            types, impl, non_overload_indexes = self.analyze_overload_sigs_and_impl(defn)
721            defn.impl = impl
722            if non_overload_indexes:
723                self.handle_missing_overload_decorators(defn, non_overload_indexes,
724                                                        some_overload_decorators=len(types) > 0)
725            # If we found an implementation, remove it from the overload item list,
726            # as it's special.
727            if impl is not None:
728                assert impl is defn.items[-1]
729                defn.items = defn.items[:-1]
730            elif not non_overload_indexes:
731                self.handle_missing_overload_implementation(defn)
732
733        if types:
734            defn.type = Overloaded(types)
735            defn.type.line = defn.line
736
737        if not defn.items:
738            # It was not a real overload after all, but function redefinition. We've
739            # visited the redefinition(s) already.
740            if not defn.impl:
741                # For really broken overloads with no items and no implementation we need to keep
742                # at least one item to hold basic information like function name.
743                defn.impl = defn.unanalyzed_items[-1]
744            return
745
746        # We know this is an overload def. Infer properties and perform some checks.
747        self.process_final_in_overload(defn)
748        self.process_static_or_class_method_in_overload(defn)
749
750    def analyze_overload_sigs_and_impl(
751            self,
752            defn: OverloadedFuncDef) -> Tuple[List[CallableType],
753                                              Optional[OverloadPart],
754                                              List[int]]:
755        """Find overload signatures, the implementation, and items with missing @overload.
756
757        Assume that the first was already analyzed. As a side effect:
758        analyzes remaining items and updates 'is_overload' flags.
759        """
760        types = []
761        non_overload_indexes = []
762        impl = None  # type: Optional[OverloadPart]
763        for i, item in enumerate(defn.items):
764            if i != 0:
765                # Assume that the first item was already visited
766                item.is_overload = True
767                item.accept(self)
768            # TODO: support decorated overloaded functions properly
769            if isinstance(item, Decorator):
770                callable = function_type(item.func, self.builtin_type('builtins.function'))
771                assert isinstance(callable, CallableType)
772                if not any(refers_to_fullname(dec, 'typing.overload')
773                           for dec in item.decorators):
774                    if i == len(defn.items) - 1 and not self.is_stub_file:
775                        # Last item outside a stub is impl
776                        impl = item
777                    else:
778                        # Oops it wasn't an overload after all. A clear error
779                        # will vary based on where in the list it is, record
780                        # that.
781                        non_overload_indexes.append(i)
782                else:
783                    item.func.is_overload = True
784                    types.append(callable)
785            elif isinstance(item, FuncDef):
786                if i == len(defn.items) - 1 and not self.is_stub_file:
787                    impl = item
788                else:
789                    non_overload_indexes.append(i)
790        return types, impl, non_overload_indexes
791
792    def handle_missing_overload_decorators(self,
793                                           defn: OverloadedFuncDef,
794                                           non_overload_indexes: List[int],
795                                           some_overload_decorators: bool) -> None:
796        """Generate errors for overload items without @overload.
797
798        Side effect: remote non-overload items.
799        """
800        if some_overload_decorators:
801            # Some of them were overloads, but not all.
802            for idx in non_overload_indexes:
803                if self.is_stub_file:
804                    self.fail("An implementation for an overloaded function "
805                              "is not allowed in a stub file", defn.items[idx])
806                else:
807                    self.fail("The implementation for an overloaded function "
808                              "must come last", defn.items[idx])
809        else:
810            for idx in non_overload_indexes[1:]:
811                self.name_already_defined(defn.name, defn.items[idx], defn.items[0])
812            if defn.impl:
813                self.name_already_defined(defn.name, defn.impl, defn.items[0])
814        # Remove the non-overloads
815        for idx in reversed(non_overload_indexes):
816            del defn.items[idx]
817
818    def handle_missing_overload_implementation(self, defn: OverloadedFuncDef) -> None:
819        """Generate error about missing overload implementation (only if needed)."""
820        if not self.is_stub_file:
821            if self.type and self.type.is_protocol and not self.is_func_scope():
822                # An overloded protocol method doesn't need an implementation.
823                for item in defn.items:
824                    if isinstance(item, Decorator):
825                        item.func.is_abstract = True
826                    else:
827                        item.is_abstract = True
828            else:
829                self.fail(
830                    "An overloaded function outside a stub file must have an implementation",
831                    defn)
832
833    def process_final_in_overload(self, defn: OverloadedFuncDef) -> None:
834        """Detect the @final status of an overloaded function (and perform checks)."""
835        # If the implementation is marked as @final (or the first overload in
836        # stubs), then the whole overloaded definition if @final.
837        if any(item.is_final for item in defn.items):
838            # We anyway mark it as final because it was probably the intention.
839            defn.is_final = True
840            # Only show the error once per overload
841            bad_final = next(ov for ov in defn.items if ov.is_final)
842            if not self.is_stub_file:
843                self.fail("@final should be applied only to overload implementation",
844                          bad_final)
845            elif any(item.is_final for item in defn.items[1:]):
846                bad_final = next(ov for ov in defn.items[1:] if ov.is_final)
847                self.fail("In a stub file @final must be applied only to the first overload",
848                          bad_final)
849        if defn.impl is not None and defn.impl.is_final:
850            defn.is_final = True
851
852    def process_static_or_class_method_in_overload(self, defn: OverloadedFuncDef) -> None:
853        class_status = []
854        static_status = []
855        for item in defn.items:
856            if isinstance(item, Decorator):
857                inner = item.func
858            elif isinstance(item, FuncDef):
859                inner = item
860            else:
861                assert False, "The 'item' variable is an unexpected type: {}".format(type(item))
862            class_status.append(inner.is_class)
863            static_status.append(inner.is_static)
864
865        if defn.impl is not None:
866            if isinstance(defn.impl, Decorator):
867                inner = defn.impl.func
868            elif isinstance(defn.impl, FuncDef):
869                inner = defn.impl
870            else:
871                assert False, "Unexpected impl type: {}".format(type(defn.impl))
872            class_status.append(inner.is_class)
873            static_status.append(inner.is_static)
874
875        if len(set(class_status)) != 1:
876            self.msg.overload_inconsistently_applies_decorator('classmethod', defn)
877        elif len(set(static_status)) != 1:
878            self.msg.overload_inconsistently_applies_decorator('staticmethod', defn)
879        else:
880            defn.is_class = class_status[0]
881            defn.is_static = static_status[0]
882
883    def analyze_property_with_multi_part_definition(self, defn: OverloadedFuncDef) -> None:
884        """Analyze a property defined using multiple methods (e.g., using @x.setter).
885
886        Assume that the first method (@property) has already been analyzed.
887        """
888        defn.is_property = True
889        items = defn.items
890        first_item = cast(Decorator, defn.items[0])
891        deleted_items = []
892        for i, item in enumerate(items[1:]):
893            if isinstance(item, Decorator):
894                if len(item.decorators) == 1:
895                    node = item.decorators[0]
896                    if isinstance(node, MemberExpr):
897                        if node.name == 'setter':
898                            # The first item represents the entire property.
899                            first_item.var.is_settable_property = True
900                            # Get abstractness from the original definition.
901                            item.func.is_abstract = first_item.func.is_abstract
902                else:
903                    self.fail("Decorated property not supported", item)
904                item.func.accept(self)
905            else:
906                self.fail('Unexpected definition for property "{}"'.format(first_item.func.name),
907                          item)
908                deleted_items.append(i + 1)
909        for i in reversed(deleted_items):
910            del items[i]
911
912    def add_function_to_symbol_table(self, func: Union[FuncDef, OverloadedFuncDef]) -> None:
913        if self.is_class_scope():
914            assert self.type is not None
915            func.info = self.type
916        func._fullname = self.qualified_name(func.name)
917        self.add_symbol(func.name, func, func)
918
919    def analyze_arg_initializers(self, defn: FuncItem) -> None:
920        with self.tvar_scope_frame(self.tvar_scope.method_frame()):
921            # Analyze default arguments
922            for arg in defn.arguments:
923                if arg.initializer:
924                    arg.initializer.accept(self)
925
926    def analyze_function_body(self, defn: FuncItem) -> None:
927        is_method = self.is_class_scope()
928        with self.tvar_scope_frame(self.tvar_scope.method_frame()):
929            # Bind the type variables again to visit the body.
930            if defn.type:
931                a = self.type_analyzer()
932                a.bind_function_type_variables(cast(CallableType, defn.type), defn)
933            self.function_stack.append(defn)
934            self.enter(defn)
935            for arg in defn.arguments:
936                self.add_local(arg.variable, defn)
937
938            # The first argument of a non-static, non-class method is like 'self'
939            # (though the name could be different), having the enclosing class's
940            # instance type.
941            if is_method and not defn.is_static and not defn.is_class and defn.arguments:
942                defn.arguments[0].variable.is_self = True
943
944            defn.body.accept(self)
945            self.leave()
946            self.function_stack.pop()
947
948    def check_classvar_in_signature(self, typ: ProperType) -> None:
949        if isinstance(typ, Overloaded):
950            for t in typ.items():  # type: ProperType
951                self.check_classvar_in_signature(t)
952            return
953        if not isinstance(typ, CallableType):
954            return
955        for t in get_proper_types(typ.arg_types) + [get_proper_type(typ.ret_type)]:
956            if self.is_classvar(t):
957                self.fail_invalid_classvar(t)
958                # Show only one error per signature
959                break
960
961    def check_function_signature(self, fdef: FuncItem) -> None:
962        sig = fdef.type
963        assert isinstance(sig, CallableType)
964        if len(sig.arg_types) < len(fdef.arguments):
965            self.fail('Type signature has too few arguments', fdef)
966            # Add dummy Any arguments to prevent crashes later.
967            num_extra_anys = len(fdef.arguments) - len(sig.arg_types)
968            extra_anys = [AnyType(TypeOfAny.from_error)] * num_extra_anys
969            sig.arg_types.extend(extra_anys)
970        elif len(sig.arg_types) > len(fdef.arguments):
971            self.fail('Type signature has too many arguments', fdef, blocker=True)
972
973    def visit_decorator(self, dec: Decorator) -> None:
974        self.statement = dec
975        # TODO: better don't modify them at all.
976        dec.decorators = dec.original_decorators.copy()
977        dec.func.is_conditional = self.block_depth[-1] > 0
978        if not dec.is_overload:
979            self.add_symbol(dec.name, dec, dec)
980        dec.func._fullname = self.qualified_name(dec.name)
981        for d in dec.decorators:
982            d.accept(self)
983        removed = []  # type: List[int]
984        no_type_check = False
985        for i, d in enumerate(dec.decorators):
986            # A bunch of decorators are special cased here.
987            if refers_to_fullname(d, 'abc.abstractmethod'):
988                removed.append(i)
989                dec.func.is_abstract = True
990                self.check_decorated_function_is_method('abstractmethod', dec)
991            elif (refers_to_fullname(d, 'asyncio.coroutines.coroutine') or
992                  refers_to_fullname(d, 'types.coroutine')):
993                removed.append(i)
994                dec.func.is_awaitable_coroutine = True
995            elif refers_to_fullname(d, 'builtins.staticmethod'):
996                removed.append(i)
997                dec.func.is_static = True
998                dec.var.is_staticmethod = True
999                self.check_decorated_function_is_method('staticmethod', dec)
1000            elif refers_to_fullname(d, 'builtins.classmethod'):
1001                removed.append(i)
1002                dec.func.is_class = True
1003                dec.var.is_classmethod = True
1004                self.check_decorated_function_is_method('classmethod', dec)
1005            elif (refers_to_fullname(d, 'builtins.property') or
1006                  refers_to_fullname(d, 'abc.abstractproperty') or
1007                  refers_to_fullname(d, 'functools.cached_property')):
1008                removed.append(i)
1009                dec.func.is_property = True
1010                dec.var.is_property = True
1011                if refers_to_fullname(d, 'abc.abstractproperty'):
1012                    dec.func.is_abstract = True
1013                elif refers_to_fullname(d, 'functools.cached_property'):
1014                    dec.var.is_settable_property = True
1015                self.check_decorated_function_is_method('property', dec)
1016                if len(dec.func.arguments) > 1:
1017                    self.fail('Too many arguments', dec.func)
1018            elif refers_to_fullname(d, 'typing.no_type_check'):
1019                dec.var.type = AnyType(TypeOfAny.special_form)
1020                no_type_check = True
1021            elif (refers_to_fullname(d, 'typing.final') or
1022                  refers_to_fullname(d, 'typing_extensions.final')):
1023                if self.is_class_scope():
1024                    assert self.type is not None, "No type set at class scope"
1025                    if self.type.is_protocol:
1026                        self.msg.protocol_members_cant_be_final(d)
1027                    else:
1028                        dec.func.is_final = True
1029                        dec.var.is_final = True
1030                    removed.append(i)
1031                else:
1032                    self.fail("@final cannot be used with non-method functions", d)
1033        for i in reversed(removed):
1034            del dec.decorators[i]
1035        if (not dec.is_overload or dec.var.is_property) and self.type:
1036            dec.var.info = self.type
1037            dec.var.is_initialized_in_class = True
1038        if not no_type_check and self.recurse_into_functions:
1039            dec.func.accept(self)
1040        if dec.decorators and dec.var.is_property:
1041            self.fail('Decorated property not supported', dec)
1042
1043    def check_decorated_function_is_method(self, decorator: str,
1044                                           context: Context) -> None:
1045        if not self.type or self.is_func_scope():
1046            self.fail('"%s" used with a non-method' % decorator, context)
1047
1048    #
1049    # Classes
1050    #
1051
1052    def visit_class_def(self, defn: ClassDef) -> None:
1053        self.statement = defn
1054        self.incomplete_type_stack.append(not defn.info)
1055        with self.tvar_scope_frame(self.tvar_scope.class_frame()):
1056            self.analyze_class(defn)
1057        self.incomplete_type_stack.pop()
1058
1059    def analyze_class(self, defn: ClassDef) -> None:
1060        fullname = self.qualified_name(defn.name)
1061        if not defn.info and not self.is_core_builtin_class(defn):
1062            # Add placeholder so that self-references in base classes can be
1063            # resolved.  We don't want this to cause a deferral, since if there
1064            # are no incomplete references, we'll replace this with a TypeInfo
1065            # before returning.
1066            placeholder = PlaceholderNode(fullname, defn, defn.line, becomes_typeinfo=True)
1067            self.add_symbol(defn.name, placeholder, defn, can_defer=False)
1068
1069        tag = self.track_incomplete_refs()
1070
1071        # Restore base classes after previous iteration (things like Generic[T] might be removed).
1072        defn.base_type_exprs.extend(defn.removed_base_type_exprs)
1073        defn.removed_base_type_exprs.clear()
1074
1075        self.update_metaclass(defn)
1076
1077        bases = defn.base_type_exprs
1078        bases, tvar_defs, is_protocol = self.clean_up_bases_and_infer_type_variables(defn, bases,
1079                                                                                     context=defn)
1080
1081        for tvd in tvar_defs:
1082            if any(has_placeholder(t) for t in [tvd.upper_bound] + tvd.values):
1083                # Some type variable bounds or values are not ready, we need
1084                # to re-analyze this class.
1085                self.defer()
1086
1087        self.analyze_class_keywords(defn)
1088        result = self.analyze_base_classes(bases)
1089
1090        if result is None or self.found_incomplete_ref(tag):
1091            # Something was incomplete. Defer current target.
1092            self.mark_incomplete(defn.name, defn)
1093            return
1094
1095        base_types, base_error = result
1096        if any(isinstance(base, PlaceholderType) for base, _ in base_types):
1097            # We need to know the TypeInfo of each base to construct the MRO. Placeholder types
1098            # are okay in nested positions, since they can't affect the MRO.
1099            self.mark_incomplete(defn.name, defn)
1100            return
1101
1102        is_typeddict, info = self.typed_dict_analyzer.analyze_typeddict_classdef(defn)
1103        if is_typeddict:
1104            for decorator in defn.decorators:
1105                decorator.accept(self)
1106                if isinstance(decorator, RefExpr):
1107                    if decorator.fullname in ('typing.final',
1108                                              'typing_extensions.final'):
1109                        self.fail("@final cannot be used with TypedDict", decorator)
1110            if info is None:
1111                self.mark_incomplete(defn.name, defn)
1112            else:
1113                self.prepare_class_def(defn, info)
1114            return
1115
1116        if self.analyze_namedtuple_classdef(defn):
1117            return
1118
1119        # Create TypeInfo for class now that base classes and the MRO can be calculated.
1120        self.prepare_class_def(defn)
1121
1122        defn.type_vars = tvar_defs
1123        defn.info.type_vars = [tvar.name for tvar in tvar_defs]
1124        if base_error:
1125            defn.info.fallback_to_any = True
1126
1127        with self.scope.class_scope(defn.info):
1128            self.configure_base_classes(defn, base_types)
1129            defn.info.is_protocol = is_protocol
1130            self.analyze_metaclass(defn)
1131            defn.info.runtime_protocol = False
1132            for decorator in defn.decorators:
1133                self.analyze_class_decorator(defn, decorator)
1134            self.analyze_class_body_common(defn)
1135
1136    def is_core_builtin_class(self, defn: ClassDef) -> bool:
1137        return self.cur_mod_id == 'builtins' and defn.name in CORE_BUILTIN_CLASSES
1138
1139    def analyze_class_body_common(self, defn: ClassDef) -> None:
1140        """Parts of class body analysis that are common to all kinds of class defs."""
1141        self.enter_class(defn.info)
1142        defn.defs.accept(self)
1143        self.apply_class_plugin_hooks(defn)
1144        self.leave_class()
1145
1146    def analyze_namedtuple_classdef(self, defn: ClassDef) -> bool:
1147        """Check if this class can define a named tuple."""
1148        if defn.info and defn.info.is_named_tuple:
1149            # Don't reprocess everything. We just need to process methods defined
1150            # in the named tuple class body.
1151            is_named_tuple, info = True, defn.info  # type: bool, Optional[TypeInfo]
1152        else:
1153            is_named_tuple, info = self.named_tuple_analyzer.analyze_namedtuple_classdef(
1154                defn, self.is_stub_file)
1155        if is_named_tuple:
1156            if info is None:
1157                self.mark_incomplete(defn.name, defn)
1158            else:
1159                self.prepare_class_def(defn, info)
1160                with self.scope.class_scope(defn.info):
1161                    with self.named_tuple_analyzer.save_namedtuple_body(info):
1162                        self.analyze_class_body_common(defn)
1163            return True
1164        return False
1165
1166    def apply_class_plugin_hooks(self, defn: ClassDef) -> None:
1167        """Apply a plugin hook that may infer a more precise definition for a class."""
1168        def get_fullname(expr: Expression) -> Optional[str]:
1169            if isinstance(expr, CallExpr):
1170                return get_fullname(expr.callee)
1171            elif isinstance(expr, IndexExpr):
1172                return get_fullname(expr.base)
1173            elif isinstance(expr, RefExpr):
1174                if expr.fullname:
1175                    return expr.fullname
1176                # If we don't have a fullname look it up. This happens because base classes are
1177                # analyzed in a different manner (see exprtotype.py) and therefore those AST
1178                # nodes will not have full names.
1179                sym = self.lookup_type_node(expr)
1180                if sym:
1181                    return sym.fullname
1182            return None
1183
1184        for decorator in defn.decorators:
1185            decorator_name = get_fullname(decorator)
1186            if decorator_name:
1187                hook = self.plugin.get_class_decorator_hook(decorator_name)
1188                if hook:
1189                    hook(ClassDefContext(defn, decorator, self))
1190
1191        if defn.metaclass:
1192            metaclass_name = get_fullname(defn.metaclass)
1193            if metaclass_name:
1194                hook = self.plugin.get_metaclass_hook(metaclass_name)
1195                if hook:
1196                    hook(ClassDefContext(defn, defn.metaclass, self))
1197
1198        for base_expr in defn.base_type_exprs:
1199            base_name = get_fullname(base_expr)
1200            if base_name:
1201                hook = self.plugin.get_base_class_hook(base_name)
1202                if hook:
1203                    hook(ClassDefContext(defn, base_expr, self))
1204
1205    def analyze_class_keywords(self, defn: ClassDef) -> None:
1206        for value in defn.keywords.values():
1207            value.accept(self)
1208
1209    def enter_class(self, info: TypeInfo) -> None:
1210        # Remember previous active class
1211        self.type_stack.append(self.type)
1212        self.locals.append(None)  # Add class scope
1213        self.is_comprehension_stack.append(False)
1214        self.block_depth.append(-1)  # The class body increments this to 0
1215        self.type = info
1216        self.missing_names.append(set())
1217
1218    def leave_class(self) -> None:
1219        """ Restore analyzer state. """
1220        self.block_depth.pop()
1221        self.locals.pop()
1222        self.is_comprehension_stack.pop()
1223        self.type = self.type_stack.pop()
1224        self.missing_names.pop()
1225
1226    def analyze_class_decorator(self, defn: ClassDef, decorator: Expression) -> None:
1227        decorator.accept(self)
1228        if isinstance(decorator, RefExpr):
1229            if decorator.fullname in RUNTIME_PROTOCOL_DECOS:
1230                if defn.info.is_protocol:
1231                    defn.info.runtime_protocol = True
1232                else:
1233                    self.fail('@runtime_checkable can only be used with protocol classes',
1234                              defn)
1235            elif decorator.fullname in ('typing.final',
1236                                        'typing_extensions.final'):
1237                defn.info.is_final = True
1238
1239    def clean_up_bases_and_infer_type_variables(
1240            self,
1241            defn: ClassDef,
1242            base_type_exprs: List[Expression],
1243            context: Context) -> Tuple[List[Expression],
1244                                       List[TypeVarDef],
1245                                       bool]:
1246        """Remove extra base classes such as Generic and infer type vars.
1247
1248        For example, consider this class:
1249
1250          class Foo(Bar, Generic[T]): ...
1251
1252        Now we will remove Generic[T] from bases of Foo and infer that the
1253        type variable 'T' is a type argument of Foo.
1254
1255        Note that this is performed *before* semantic analysis.
1256
1257        Returns (remaining base expressions, inferred type variables, is protocol).
1258        """
1259        removed = []  # type: List[int]
1260        declared_tvars = []  # type: TypeVarLikeList
1261        is_protocol = False
1262        for i, base_expr in enumerate(base_type_exprs):
1263            self.analyze_type_expr(base_expr)
1264
1265            try:
1266                base = expr_to_unanalyzed_type(base_expr)
1267            except TypeTranslationError:
1268                # This error will be caught later.
1269                continue
1270            result = self.analyze_class_typevar_declaration(base)
1271            if result is not None:
1272                if declared_tvars:
1273                    self.fail('Only single Generic[...] or Protocol[...] can be in bases', context)
1274                removed.append(i)
1275                tvars = result[0]
1276                is_protocol |= result[1]
1277                declared_tvars.extend(tvars)
1278            if isinstance(base, UnboundType):
1279                sym = self.lookup_qualified(base.name, base)
1280                if sym is not None and sym.node is not None:
1281                    if (sym.node.fullname in ('typing.Protocol', 'typing_extensions.Protocol') and
1282                            i not in removed):
1283                        # also remove bare 'Protocol' bases
1284                        removed.append(i)
1285                        is_protocol = True
1286
1287        all_tvars = self.get_all_bases_tvars(base_type_exprs, removed)
1288        if declared_tvars:
1289            if len(remove_dups(declared_tvars)) < len(declared_tvars):
1290                self.fail("Duplicate type variables in Generic[...] or Protocol[...]", context)
1291            declared_tvars = remove_dups(declared_tvars)
1292            if not set(all_tvars).issubset(set(declared_tvars)):
1293                self.fail("If Generic[...] or Protocol[...] is present"
1294                          " it should list all type variables", context)
1295                # In case of error, Generic tvars will go first
1296                declared_tvars = remove_dups(declared_tvars + all_tvars)
1297        else:
1298            declared_tvars = all_tvars
1299        for i in reversed(removed):
1300            # We need to actually remove the base class expressions like Generic[T],
1301            # mostly because otherwise they will create spurious dependencies in fine
1302            # grained incremental mode.
1303            defn.removed_base_type_exprs.append(defn.base_type_exprs[i])
1304            del base_type_exprs[i]
1305        tvar_defs = []  # type: List[TypeVarDef]
1306        for name, tvar_expr in declared_tvars:
1307            tvar_def = self.tvar_scope.bind_new(name, tvar_expr)
1308            assert isinstance(tvar_def, TypeVarDef), (
1309                "mypy does not currently support ParamSpec use in generic classes"
1310            )
1311            tvar_defs.append(tvar_def)
1312        return base_type_exprs, tvar_defs, is_protocol
1313
1314    def analyze_class_typevar_declaration(
1315        self,
1316        base: Type
1317    ) -> Optional[Tuple[TypeVarLikeList, bool]]:
1318        """Analyze type variables declared using Generic[...] or Protocol[...].
1319
1320        Args:
1321            base: Non-analyzed base class
1322
1323        Return None if the base class does not declare type variables. Otherwise,
1324        return the type variables.
1325        """
1326        if not isinstance(base, UnboundType):
1327            return None
1328        unbound = base
1329        sym = self.lookup_qualified(unbound.name, unbound)
1330        if sym is None or sym.node is None:
1331            return None
1332        if (sym.node.fullname == 'typing.Generic' or
1333                sym.node.fullname == 'typing.Protocol' and base.args or
1334                sym.node.fullname == 'typing_extensions.Protocol' and base.args):
1335            is_proto = sym.node.fullname != 'typing.Generic'
1336            tvars = []  # type: TypeVarLikeList
1337            for arg in unbound.args:
1338                tag = self.track_incomplete_refs()
1339                tvar = self.analyze_unbound_tvar(arg)
1340                if tvar:
1341                    tvars.append(tvar)
1342                elif not self.found_incomplete_ref(tag):
1343                    self.fail('Free type variable expected in %s[...]' %
1344                              sym.node.name, base)
1345            return tvars, is_proto
1346        return None
1347
1348    def analyze_unbound_tvar(self, t: Type) -> Optional[Tuple[str, TypeVarExpr]]:
1349        if not isinstance(t, UnboundType):
1350            return None
1351        unbound = t
1352        sym = self.lookup_qualified(unbound.name, unbound)
1353        if sym and isinstance(sym.node, PlaceholderNode):
1354            self.record_incomplete_ref()
1355        if sym is None or not isinstance(sym.node, TypeVarExpr):
1356            return None
1357        elif sym.fullname and not self.tvar_scope.allow_binding(sym.fullname):
1358            # It's bound by our type variable scope
1359            return None
1360        else:
1361            assert isinstance(sym.node, TypeVarExpr)
1362            return unbound.name, sym.node
1363
1364    def get_all_bases_tvars(self,
1365                            base_type_exprs: List[Expression],
1366                            removed: List[int]) -> TypeVarLikeList:
1367        """Return all type variable references in bases."""
1368        tvars = []  # type: TypeVarLikeList
1369        for i, base_expr in enumerate(base_type_exprs):
1370            if i not in removed:
1371                try:
1372                    base = expr_to_unanalyzed_type(base_expr)
1373                except TypeTranslationError:
1374                    # This error will be caught later.
1375                    continue
1376                base_tvars = base.accept(TypeVarLikeQuery(self.lookup_qualified, self.tvar_scope))
1377                tvars.extend(base_tvars)
1378        return remove_dups(tvars)
1379
1380    def prepare_class_def(self, defn: ClassDef, info: Optional[TypeInfo] = None) -> None:
1381        """Prepare for the analysis of a class definition.
1382
1383        Create an empty TypeInfo and store it in a symbol table, or if the 'info'
1384        argument is provided, store it instead (used for magic type definitions).
1385        """
1386        if not defn.info:
1387            defn.fullname = self.qualified_name(defn.name)
1388            # TODO: Nested classes
1389            info = info or self.make_empty_type_info(defn)
1390            defn.info = info
1391            info.defn = defn
1392            if not self.is_func_scope():
1393                info._fullname = self.qualified_name(defn.name)
1394            else:
1395                info._fullname = info.name
1396        self.add_symbol(defn.name, defn.info, defn)
1397        if self.is_nested_within_func_scope():
1398            # We need to preserve local classes, let's store them
1399            # in globals under mangled unique names
1400            #
1401            # TODO: Putting local classes into globals breaks assumptions in fine-grained
1402            #       incremental mode and we should avoid it. In general, this logic is too
1403            #       ad-hoc and needs to be removed/refactored.
1404            if '@' not in defn.info._fullname:
1405                local_name = defn.info.name + '@' + str(defn.line)
1406                if defn.info.is_named_tuple:
1407                    # Module is already correctly set in _fullname for named tuples.
1408                    defn.info._fullname += '@' + str(defn.line)
1409                else:
1410                    defn.info._fullname = self.cur_mod_id + '.' + local_name
1411            else:
1412                # Preserve name from previous fine-grained incremental run.
1413                local_name = defn.info.name
1414            defn.fullname = defn.info._fullname
1415            self.globals[local_name] = SymbolTableNode(GDEF, defn.info)
1416
1417    def make_empty_type_info(self, defn: ClassDef) -> TypeInfo:
1418        if (self.is_module_scope()
1419                and self.cur_mod_id == 'builtins'
1420                and defn.name in CORE_BUILTIN_CLASSES):
1421            # Special case core built-in classes. A TypeInfo was already
1422            # created for it before semantic analysis, but with a dummy
1423            # ClassDef. Patch the real ClassDef object.
1424            info = self.globals[defn.name].node
1425            assert isinstance(info, TypeInfo)
1426        else:
1427            info = TypeInfo(SymbolTable(), defn, self.cur_mod_id)
1428            info.set_line(defn)
1429        return info
1430
1431    def get_name_repr_of_expr(self, expr: Expression) -> Optional[str]:
1432        """Try finding a short simplified textual representation of a base class expression."""
1433        if isinstance(expr, NameExpr):
1434            return expr.name
1435        if isinstance(expr, MemberExpr):
1436            return get_member_expr_fullname(expr)
1437        if isinstance(expr, IndexExpr):
1438            return self.get_name_repr_of_expr(expr.base)
1439        if isinstance(expr, CallExpr):
1440            return self.get_name_repr_of_expr(expr.callee)
1441        return None
1442
1443    def analyze_base_classes(
1444            self,
1445            base_type_exprs: List[Expression]) -> Optional[Tuple[List[Tuple[ProperType,
1446                                                                            Expression]],
1447                                                                 bool]]:
1448        """Analyze base class types.
1449
1450        Return None if some definition was incomplete. Otherwise, return a tuple
1451        with these items:
1452
1453         * List of (analyzed type, original expression) tuples
1454         * Boolean indicating whether one of the bases had a semantic analysis error
1455        """
1456        is_error = False
1457        bases = []
1458        for base_expr in base_type_exprs:
1459            if (isinstance(base_expr, RefExpr) and
1460                    base_expr.fullname in ('typing.NamedTuple',) + TPDICT_NAMES):
1461                # Ignore magic bases for now.
1462                continue
1463
1464            try:
1465                base = self.expr_to_analyzed_type(base_expr, allow_placeholder=True)
1466            except TypeTranslationError:
1467                name = self.get_name_repr_of_expr(base_expr)
1468                if isinstance(base_expr, CallExpr):
1469                    msg = 'Unsupported dynamic base class'
1470                else:
1471                    msg = 'Invalid base class'
1472                if name:
1473                    msg += ' "{}"'.format(name)
1474                self.fail(msg, base_expr)
1475                is_error = True
1476                continue
1477            if base is None:
1478                return None
1479            base = get_proper_type(base)
1480            bases.append((base, base_expr))
1481        return bases, is_error
1482
1483    def configure_base_classes(self,
1484                               defn: ClassDef,
1485                               bases: List[Tuple[ProperType, Expression]]) -> None:
1486        """Set up base classes.
1487
1488        This computes several attributes on the corresponding TypeInfo defn.info
1489        related to the base classes: defn.info.bases, defn.info.mro, and
1490        miscellaneous others (at least tuple_type, fallback_to_any, and is_enum.)
1491        """
1492        base_types = []  # type: List[Instance]
1493        info = defn.info
1494
1495        info.tuple_type = None
1496        for base, base_expr in bases:
1497            if isinstance(base, TupleType):
1498                actual_base = self.configure_tuple_base_class(defn, base, base_expr)
1499                base_types.append(actual_base)
1500            elif isinstance(base, Instance):
1501                if base.type.is_newtype:
1502                    self.fail('Cannot subclass "NewType"', defn)
1503                base_types.append(base)
1504            elif isinstance(base, AnyType):
1505                if self.options.disallow_subclassing_any:
1506                    if isinstance(base_expr, (NameExpr, MemberExpr)):
1507                        msg = 'Class cannot subclass "{}" (has type "Any")'.format(base_expr.name)
1508                    else:
1509                        msg = 'Class cannot subclass value of type "Any"'
1510                    self.fail(msg, base_expr)
1511                info.fallback_to_any = True
1512            else:
1513                msg = 'Invalid base class'
1514                name = self.get_name_repr_of_expr(base_expr)
1515                if name:
1516                    msg += ' "{}"'.format(name)
1517                self.fail(msg, base_expr)
1518                info.fallback_to_any = True
1519            if self.options.disallow_any_unimported and has_any_from_unimported_type(base):
1520                if isinstance(base_expr, (NameExpr, MemberExpr)):
1521                    prefix = "Base type {}".format(base_expr.name)
1522                else:
1523                    prefix = "Base type"
1524                self.msg.unimported_type_becomes_any(prefix, base, base_expr)
1525            check_for_explicit_any(base, self.options, self.is_typeshed_stub_file, self.msg,
1526                                   context=base_expr)
1527
1528        # Add 'object' as implicit base if there is no other base class.
1529        if not base_types and defn.fullname != 'builtins.object':
1530            base_types.append(self.object_type())
1531
1532        info.bases = base_types
1533
1534        # Calculate the MRO.
1535        if not self.verify_base_classes(defn):
1536            self.set_dummy_mro(defn.info)
1537            return
1538        self.calculate_class_mro(defn, self.object_type)
1539
1540    def configure_tuple_base_class(self,
1541                                   defn: ClassDef,
1542                                   base: TupleType,
1543                                   base_expr: Expression) -> Instance:
1544        info = defn.info
1545
1546        # There may be an existing valid tuple type from previous semanal iterations.
1547        # Use equality to check if it is the case.
1548        if info.tuple_type and info.tuple_type != base:
1549            self.fail("Class has two incompatible bases derived from tuple", defn)
1550            defn.has_incompatible_baseclass = True
1551        info.tuple_type = base
1552        if isinstance(base_expr, CallExpr):
1553            defn.analyzed = NamedTupleExpr(base.partial_fallback.type)
1554            defn.analyzed.line = defn.line
1555            defn.analyzed.column = defn.column
1556
1557        if base.partial_fallback.type.fullname == 'builtins.tuple':
1558            # Fallback can only be safely calculated after semantic analysis, since base
1559            # classes may be incomplete. Postpone the calculation.
1560            self.schedule_patch(PRIORITY_FALLBACKS, lambda: calculate_tuple_fallback(base))
1561
1562        return base.partial_fallback
1563
1564    def set_dummy_mro(self, info: TypeInfo) -> None:
1565        # Give it an MRO consisting of just the class itself and object.
1566        info.mro = [info, self.object_type().type]
1567        info.bad_mro = True
1568
1569    def calculate_class_mro(self, defn: ClassDef,
1570                            obj_type: Optional[Callable[[], Instance]] = None) -> None:
1571        """Calculate method resolution order for a class.
1572
1573        `obj_type` may be omitted in the third pass when all classes are already analyzed.
1574        It exists just to fill in empty base class list during second pass in case of
1575        an import cycle.
1576        """
1577        try:
1578            calculate_mro(defn.info, obj_type)
1579        except MroError:
1580            self.fail('Cannot determine consistent method resolution '
1581                      'order (MRO) for "%s"' % defn.name, defn)
1582            self.set_dummy_mro(defn.info)
1583        # Allow plugins to alter the MRO to handle the fact that `def mro()`
1584        # on metaclasses permits MRO rewriting.
1585        if defn.fullname:
1586            hook = self.plugin.get_customize_class_mro_hook(defn.fullname)
1587            if hook:
1588                hook(ClassDefContext(defn, FakeExpression(), self))
1589
1590    def update_metaclass(self, defn: ClassDef) -> None:
1591        """Lookup for special metaclass declarations, and update defn fields accordingly.
1592
1593        * __metaclass__ attribute in Python 2
1594        * six.with_metaclass(M, B1, B2, ...)
1595        * @six.add_metaclass(M)
1596        * future.utils.with_metaclass(M, B1, B2, ...)
1597        * past.utils.with_metaclass(M, B1, B2, ...)
1598        """
1599
1600        # Look for "__metaclass__ = <metaclass>" in Python 2
1601        python2_meta_expr = None  # type: Optional[Expression]
1602        if self.options.python_version[0] == 2:
1603            for body_node in defn.defs.body:
1604                if isinstance(body_node, ClassDef) and body_node.name == "__metaclass__":
1605                    self.fail("Metaclasses defined as inner classes are not supported", body_node)
1606                    break
1607                elif isinstance(body_node, AssignmentStmt) and len(body_node.lvalues) == 1:
1608                    lvalue = body_node.lvalues[0]
1609                    if isinstance(lvalue, NameExpr) and lvalue.name == "__metaclass__":
1610                        python2_meta_expr = body_node.rvalue
1611
1612        # Look for six.with_metaclass(M, B1, B2, ...)
1613        with_meta_expr = None  # type: Optional[Expression]
1614        if len(defn.base_type_exprs) == 1:
1615            base_expr = defn.base_type_exprs[0]
1616            if isinstance(base_expr, CallExpr) and isinstance(base_expr.callee, RefExpr):
1617                base_expr.accept(self)
1618                if (base_expr.callee.fullname in {'six.with_metaclass',
1619                                                  'future.utils.with_metaclass',
1620                                                  'past.utils.with_metaclass'}
1621                        and len(base_expr.args) >= 1
1622                        and all(kind == ARG_POS for kind in base_expr.arg_kinds)):
1623                    with_meta_expr = base_expr.args[0]
1624                    defn.base_type_exprs = base_expr.args[1:]
1625
1626        # Look for @six.add_metaclass(M)
1627        add_meta_expr = None  # type: Optional[Expression]
1628        for dec_expr in defn.decorators:
1629            if isinstance(dec_expr, CallExpr) and isinstance(dec_expr.callee, RefExpr):
1630                dec_expr.callee.accept(self)
1631                if (dec_expr.callee.fullname == 'six.add_metaclass'
1632                    and len(dec_expr.args) == 1
1633                        and dec_expr.arg_kinds[0] == ARG_POS):
1634                    add_meta_expr = dec_expr.args[0]
1635                    break
1636
1637        metas = {defn.metaclass, python2_meta_expr, with_meta_expr, add_meta_expr} - {None}
1638        if len(metas) == 0:
1639            return
1640        if len(metas) > 1:
1641            self.fail("Multiple metaclass definitions", defn)
1642            return
1643        defn.metaclass = metas.pop()
1644
1645    def verify_base_classes(self, defn: ClassDef) -> bool:
1646        info = defn.info
1647        cycle = False
1648        for base in info.bases:
1649            baseinfo = base.type
1650            if self.is_base_class(info, baseinfo):
1651                self.fail('Cycle in inheritance hierarchy', defn)
1652                cycle = True
1653            if baseinfo.fullname == 'builtins.bool':
1654                self.fail('"%s" is not a valid base class' %
1655                          baseinfo.name, defn, blocker=True)
1656                return False
1657        dup = find_duplicate(info.direct_base_classes())
1658        if dup:
1659            self.fail('Duplicate base class "%s"' % dup.name, defn, blocker=True)
1660            return False
1661        return not cycle
1662
1663    def is_base_class(self, t: TypeInfo, s: TypeInfo) -> bool:
1664        """Determine if t is a base class of s (but do not use mro)."""
1665        # Search the base class graph for t, starting from s.
1666        worklist = [s]
1667        visited = {s}
1668        while worklist:
1669            nxt = worklist.pop()
1670            if nxt == t:
1671                return True
1672            for base in nxt.bases:
1673                if base.type not in visited:
1674                    worklist.append(base.type)
1675                    visited.add(base.type)
1676        return False
1677
1678    def analyze_metaclass(self, defn: ClassDef) -> None:
1679        if defn.metaclass:
1680            metaclass_name = None
1681            if isinstance(defn.metaclass, NameExpr):
1682                metaclass_name = defn.metaclass.name
1683            elif isinstance(defn.metaclass, MemberExpr):
1684                metaclass_name = get_member_expr_fullname(defn.metaclass)
1685            if metaclass_name is None:
1686                self.fail('Dynamic metaclass not supported for "%s"' % defn.name, defn.metaclass)
1687                return
1688            sym = self.lookup_qualified(metaclass_name, defn.metaclass)
1689            if sym is None:
1690                # Probably a name error - it is already handled elsewhere
1691                return
1692            if isinstance(sym.node, Var) and isinstance(get_proper_type(sym.node.type), AnyType):
1693                # 'Any' metaclass -- just ignore it.
1694                #
1695                # TODO: A better approach would be to record this information
1696                #       and assume that the type object supports arbitrary
1697                #       attributes, similar to an 'Any' base class.
1698                return
1699            if isinstance(sym.node, PlaceholderNode):
1700                self.defer(defn)
1701                return
1702            if not isinstance(sym.node, TypeInfo) or sym.node.tuple_type is not None:
1703                self.fail('Invalid metaclass "%s"' % metaclass_name, defn.metaclass)
1704                return
1705            if not sym.node.is_metaclass():
1706                self.fail('Metaclasses not inheriting from "type" are not supported',
1707                          defn.metaclass)
1708                return
1709            inst = fill_typevars(sym.node)
1710            assert isinstance(inst, Instance)
1711            defn.info.declared_metaclass = inst
1712        defn.info.metaclass_type = defn.info.calculate_metaclass_type()
1713        if any(info.is_protocol for info in defn.info.mro):
1714            if (not defn.info.metaclass_type or
1715                    defn.info.metaclass_type.type.fullname == 'builtins.type'):
1716                # All protocols and their subclasses have ABCMeta metaclass by default.
1717                # TODO: add a metaclass conflict check if there is another metaclass.
1718                abc_meta = self.named_type_or_none('abc.ABCMeta', [])
1719                if abc_meta is not None:  # May be None in tests with incomplete lib-stub.
1720                    defn.info.metaclass_type = abc_meta
1721        if defn.info.metaclass_type is None:
1722            # Inconsistency may happen due to multiple baseclasses even in classes that
1723            # do not declare explicit metaclass, but it's harder to catch at this stage
1724            if defn.metaclass is not None:
1725                self.fail('Inconsistent metaclass structure for "%s"' % defn.name, defn)
1726        else:
1727            if defn.info.metaclass_type.type.has_base('enum.EnumMeta'):
1728                defn.info.is_enum = True
1729                if defn.type_vars:
1730                    self.fail("Enum class cannot be generic", defn)
1731
1732    #
1733    # Imports
1734    #
1735
1736    def visit_import(self, i: Import) -> None:
1737        self.statement = i
1738        for id, as_id in i.ids:
1739            # Modules imported in a stub file without using 'import X as X' won't get exported
1740            # When implicit re-exporting is disabled, we have the same behavior as stubs.
1741            use_implicit_reexport = not self.is_stub_file and self.options.implicit_reexport
1742            if as_id is not None:
1743                base_id = id
1744                imported_id = as_id
1745                module_public = use_implicit_reexport or id.split(".")[-1] == as_id
1746            else:
1747                base_id = id.split('.')[0]
1748                imported_id = base_id
1749                module_public = use_implicit_reexport
1750            self.add_module_symbol(base_id, imported_id, context=i, module_public=module_public,
1751                                   module_hidden=not module_public)
1752
1753    def visit_import_from(self, imp: ImportFrom) -> None:
1754        self.statement = imp
1755        module_id = self.correct_relative_import(imp)
1756        module = self.modules.get(module_id)
1757        for id, as_id in imp.names:
1758            fullname = module_id + '.' + id
1759            self.set_future_import_flags(fullname)
1760            if module is None:
1761                node = None
1762            elif module_id == self.cur_mod_id and fullname in self.modules:
1763                # Submodule takes precedence over definition in surround package, for
1764                # compatibility with runtime semantics in typical use cases. This
1765                # could more precisely model runtime semantics by taking into account
1766                # the line number beyond which the local definition should take
1767                # precedence, but doesn't seem to be important in most use cases.
1768                node = SymbolTableNode(GDEF, self.modules[fullname])
1769            else:
1770                if id == as_id == '__all__' and module_id in self.export_map:
1771                    self.all_exports[:] = self.export_map[module_id]
1772                node = module.names.get(id)
1773
1774            missing_submodule = False
1775            imported_id = as_id or id
1776
1777            # If the module does not contain a symbol with the name 'id',
1778            # try checking if it's a module instead.
1779            if not node:
1780                mod = self.modules.get(fullname)
1781                if mod is not None:
1782                    kind = self.current_symbol_kind()
1783                    node = SymbolTableNode(kind, mod)
1784                elif fullname in self.missing_modules:
1785                    missing_submodule = True
1786            # If it is still not resolved, check for a module level __getattr__
1787            if (module and not node and (module.is_stub or self.options.python_version >= (3, 7))
1788                    and '__getattr__' in module.names):
1789                # We store the fullname of the original definition so that we can
1790                # detect whether two imported names refer to the same thing.
1791                fullname = module_id + '.' + id
1792                gvar = self.create_getattr_var(module.names['__getattr__'], imported_id, fullname)
1793                if gvar:
1794                    self.add_symbol(imported_id, gvar, imp)
1795                    continue
1796
1797            # Modules imported in a stub file without using 'from Y import X as X' will
1798            # not get exported.
1799            # When implicit re-exporting is disabled, we have the same behavior as stubs.
1800            use_implicit_reexport = not self.is_stub_file and self.options.implicit_reexport
1801            module_public = use_implicit_reexport or (as_id is not None and id == as_id)
1802
1803            if node and not node.module_hidden:
1804                self.process_imported_symbol(
1805                    node, module_id, id, imported_id, fullname, module_public, context=imp
1806                )
1807            elif module and not missing_submodule:
1808                # Target module exists but the imported name is missing or hidden.
1809                self.report_missing_module_attribute(
1810                    module_id, id, imported_id, module_public=module_public,
1811                    module_hidden=not module_public, context=imp
1812                )
1813            else:
1814                # Import of a missing (sub)module.
1815                self.add_unknown_imported_symbol(
1816                    imported_id, imp, target_name=fullname, module_public=module_public,
1817                    module_hidden=not module_public
1818                )
1819
1820    def process_imported_symbol(self,
1821                                node: SymbolTableNode,
1822                                module_id: str,
1823                                id: str,
1824                                imported_id: str,
1825                                fullname: str,
1826                                module_public: bool,
1827                                context: ImportBase) -> None:
1828        module_hidden = not module_public and fullname not in self.modules
1829
1830        if isinstance(node.node, PlaceholderNode):
1831            if self.final_iteration:
1832                self.report_missing_module_attribute(
1833                    module_id, id, imported_id, module_public=module_public,
1834                    module_hidden=module_hidden, context=context
1835                )
1836                return
1837            else:
1838                # This might become a type.
1839                self.mark_incomplete(imported_id, node.node,
1840                                     module_public=module_public,
1841                                     module_hidden=module_hidden,
1842                                     becomes_typeinfo=True)
1843        existing_symbol = self.globals.get(imported_id)
1844        if (existing_symbol and not isinstance(existing_symbol.node, PlaceholderNode) and
1845                not isinstance(node.node, PlaceholderNode)):
1846            # Import can redefine a variable. They get special treatment.
1847            if self.process_import_over_existing_name(
1848                    imported_id, existing_symbol, node, context):
1849                return
1850        if existing_symbol and isinstance(node.node, PlaceholderNode):
1851            # Imports are special, some redefinitions are allowed, so wait until
1852            # we know what is the new symbol node.
1853            return
1854        # NOTE: we take the original node even for final `Var`s. This is to support
1855        # a common pattern when constants are re-exported (same applies to import *).
1856        self.add_imported_symbol(imported_id, node, context,
1857                                 module_public=module_public,
1858                                 module_hidden=module_hidden)
1859
1860    def report_missing_module_attribute(
1861        self, import_id: str, source_id: str, imported_id: str, module_public: bool,
1862        module_hidden: bool, context: Node
1863    ) -> None:
1864        # Missing attribute.
1865        if self.is_incomplete_namespace(import_id):
1866            # We don't know whether the name will be there, since the namespace
1867            # is incomplete. Defer the current target.
1868            self.mark_incomplete(imported_id, context)
1869            return
1870        message = 'Module "{}" has no attribute "{}"'.format(import_id, source_id)
1871        # Suggest alternatives, if any match is found.
1872        module = self.modules.get(import_id)
1873        if module:
1874            if not self.options.implicit_reexport and source_id in module.names.keys():
1875                message = ('Module "{}" does not explicitly export attribute "{}"'
1876                           '; implicit reexport disabled'.format(import_id, source_id))
1877            else:
1878                alternatives = set(module.names.keys()).difference({source_id})
1879                matches = best_matches(source_id, alternatives)[:3]
1880                if matches:
1881                    suggestion = "; maybe {}?".format(pretty_seq(matches, "or"))
1882                    message += "{}".format(suggestion)
1883        self.fail(message, context, code=codes.ATTR_DEFINED)
1884        self.add_unknown_imported_symbol(
1885            imported_id, context, target_name=None, module_public=module_public,
1886            module_hidden=not module_public
1887        )
1888
1889        if import_id == 'typing':
1890            # The user probably has a missing definition in a test fixture. Let's verify.
1891            fullname = 'builtins.{}'.format(source_id.lower())
1892            if (self.lookup_fully_qualified_or_none(fullname) is None and
1893                    fullname in SUGGESTED_TEST_FIXTURES):
1894                # Yes. Generate a helpful note.
1895                self.msg.add_fixture_note(fullname, context)
1896
1897    def process_import_over_existing_name(self,
1898                                          imported_id: str, existing_symbol: SymbolTableNode,
1899                                          module_symbol: SymbolTableNode,
1900                                          import_node: ImportBase) -> bool:
1901        if existing_symbol.node is module_symbol.node:
1902            # We added this symbol on previous iteration.
1903            return False
1904        if (existing_symbol.kind in (LDEF, GDEF, MDEF) and
1905                isinstance(existing_symbol.node, (Var, FuncDef, TypeInfo, Decorator, TypeAlias))):
1906            # This is a valid import over an existing definition in the file. Construct a dummy
1907            # assignment that we'll use to type check the import.
1908            lvalue = NameExpr(imported_id)
1909            lvalue.kind = existing_symbol.kind
1910            lvalue.node = existing_symbol.node
1911            rvalue = NameExpr(imported_id)
1912            rvalue.kind = module_symbol.kind
1913            rvalue.node = module_symbol.node
1914            if isinstance(rvalue.node, TypeAlias):
1915                # Suppress bogus errors from the dummy assignment if rvalue is an alias.
1916                # Otherwise mypy may complain that alias is invalid in runtime context.
1917                rvalue.is_alias_rvalue = True
1918            assignment = AssignmentStmt([lvalue], rvalue)
1919            for node in assignment, lvalue, rvalue:
1920                node.set_line(import_node)
1921            import_node.assignments.append(assignment)
1922            return True
1923        return False
1924
1925    def correct_relative_import(self, node: Union[ImportFrom, ImportAll]) -> str:
1926        import_id, ok = correct_relative_import(self.cur_mod_id, node.relative, node.id,
1927                                                self.cur_mod_node.is_package_init_file())
1928        if not ok:
1929            self.fail("Relative import climbs too many namespaces", node)
1930        return import_id
1931
1932    def visit_import_all(self, i: ImportAll) -> None:
1933        i_id = self.correct_relative_import(i)
1934        if i_id in self.modules:
1935            m = self.modules[i_id]
1936            if self.is_incomplete_namespace(i_id):
1937                # Any names could be missing from the current namespace if the target module
1938                # namespace is incomplete.
1939                self.mark_incomplete('*', i)
1940            for name, node in m.names.items():
1941                fullname = i_id + '.' + name
1942                self.set_future_import_flags(fullname)
1943                if node is None:
1944                    continue
1945                # if '__all__' exists, all nodes not included have had module_public set to
1946                # False, and we can skip checking '_' because it's been explicitly included.
1947                if node.module_public and (not name.startswith('_') or '__all__' in m.names):
1948                    if isinstance(node.node, MypyFile):
1949                        # Star import of submodule from a package, add it as a dependency.
1950                        self.imports.add(node.node.fullname)
1951                    existing_symbol = self.lookup_current_scope(name)
1952                    if existing_symbol and not isinstance(node.node, PlaceholderNode):
1953                        # Import can redefine a variable. They get special treatment.
1954                        if self.process_import_over_existing_name(
1955                                name, existing_symbol, node, i):
1956                            continue
1957                    # In stub files, `from x import *` always reexports the symbols.
1958                    # In regular files, only if implicit reexports are enabled.
1959                    module_public = self.is_stub_file or self.options.implicit_reexport
1960                    self.add_imported_symbol(name, node, i,
1961                                             module_public=module_public,
1962                                             module_hidden=not module_public)
1963
1964        else:
1965            # Don't add any dummy symbols for 'from x import *' if 'x' is unknown.
1966            pass
1967
1968    #
1969    # Assignment
1970    #
1971
1972    def visit_assignment_expr(self, s: AssignmentExpr) -> None:
1973        s.value.accept(self)
1974        self.analyze_lvalue(s.target, escape_comprehensions=True)
1975
1976    def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
1977        self.statement = s
1978
1979        # Special case assignment like X = X.
1980        if self.analyze_identity_global_assignment(s):
1981            return
1982
1983        tag = self.track_incomplete_refs()
1984        s.rvalue.accept(self)
1985        if self.found_incomplete_ref(tag) or self.should_wait_rhs(s.rvalue):
1986            # Initializer couldn't be fully analyzed. Defer the current node and give up.
1987            # Make sure that if we skip the definition of some local names, they can't be
1988            # added later in this scope, since an earlier definition should take precedence.
1989            for expr in names_modified_by_assignment(s):
1990                self.mark_incomplete(expr.name, expr)
1991            return
1992
1993        # The r.h.s. is now ready to be classified, first check if it is a special form:
1994        special_form = False
1995        # * type alias
1996        if self.check_and_set_up_type_alias(s):
1997            s.is_alias_def = True
1998            special_form = True
1999        # * type variable definition
2000        elif self.process_typevar_declaration(s):
2001            special_form = True
2002        elif self.process_paramspec_declaration(s):
2003            special_form = True
2004        # * type constructors
2005        elif self.analyze_namedtuple_assign(s):
2006            special_form = True
2007        elif self.analyze_typeddict_assign(s):
2008            special_form = True
2009        elif self.newtype_analyzer.process_newtype_declaration(s):
2010            special_form = True
2011        elif self.analyze_enum_assign(s):
2012            special_form = True
2013        if special_form:
2014            self.record_special_form_lvalue(s)
2015            return
2016
2017        # OK, this is a regular assignment, perform the necessary analysis steps.
2018        s.is_final_def = self.unwrap_final(s)
2019        self.analyze_lvalues(s)
2020        self.check_final_implicit_def(s)
2021        self.check_classvar(s)
2022        self.process_type_annotation(s)
2023        self.apply_dynamic_class_hook(s)
2024        self.store_final_status(s)
2025        if not s.type:
2026            self.process_module_assignment(s.lvalues, s.rvalue, s)
2027        self.process__all__(s)
2028
2029    def analyze_identity_global_assignment(self, s: AssignmentStmt) -> bool:
2030        """Special case 'X = X' in global scope.
2031
2032        This allows supporting some important use cases.
2033
2034        Return true if special casing was applied.
2035        """
2036        if not isinstance(s.rvalue, NameExpr) or len(s.lvalues) != 1:
2037            # Not of form 'X = X'
2038            return False
2039        lvalue = s.lvalues[0]
2040        if not isinstance(lvalue, NameExpr) or s.rvalue.name != lvalue.name:
2041            # Not of form 'X = X'
2042            return False
2043        if self.type is not None or self.is_func_scope():
2044            # Not in global scope
2045            return False
2046        # It's an assignment like 'X = X' in the global scope.
2047        name = lvalue.name
2048        sym = self.lookup(name, s)
2049        if sym is None:
2050            if self.final_iteration:
2051                # Fall back to normal assignment analysis.
2052                return False
2053            else:
2054                self.defer()
2055                return True
2056        else:
2057            if sym.node is None:
2058                # Something special -- fall back to normal assignment analysis.
2059                return False
2060            if name not in self.globals:
2061                # The name is from builtins. Add an alias to the current module.
2062                self.add_symbol(name, sym.node, s)
2063            if not isinstance(sym.node, PlaceholderNode):
2064                for node in s.rvalue, lvalue:
2065                    node.node = sym.node
2066                    node.kind = GDEF
2067                    node.fullname = sym.node.fullname
2068            return True
2069
2070    def should_wait_rhs(self, rv: Expression) -> bool:
2071        """Can we already classify this r.h.s. of an assignment or should we wait?
2072
2073        This returns True if we don't have enough information to decide whether
2074        an assignment is just a normal variable definition or a special form.
2075        Always return False if this is a final iteration. This will typically cause
2076        the lvalue to be classified as a variable plus emit an error.
2077        """
2078        if self.final_iteration:
2079            # No chance, nothing has changed.
2080            return False
2081        if isinstance(rv, NameExpr):
2082            n = self.lookup(rv.name, rv)
2083            if n and isinstance(n.node, PlaceholderNode) and not n.node.becomes_typeinfo:
2084                return True
2085        elif isinstance(rv, MemberExpr):
2086            fname = get_member_expr_fullname(rv)
2087            if fname:
2088                n = self.lookup_qualified(fname, rv, suppress_errors=True)
2089                if n and isinstance(n.node, PlaceholderNode) and not n.node.becomes_typeinfo:
2090                    return True
2091        elif isinstance(rv, IndexExpr) and isinstance(rv.base, RefExpr):
2092            return self.should_wait_rhs(rv.base)
2093        elif isinstance(rv, CallExpr) and isinstance(rv.callee, RefExpr):
2094            # This is only relevant for builtin SCC where things like 'TypeVar'
2095            # may be not ready.
2096            return self.should_wait_rhs(rv.callee)
2097        return False
2098
2099    def can_be_type_alias(self, rv: Expression) -> bool:
2100        """Is this a valid r.h.s. for an alias definition?
2101
2102        Note: this function should be only called for expressions where self.should_wait_rhs()
2103        returns False.
2104        """
2105        if isinstance(rv, RefExpr) and self.is_type_ref(rv, bare=True):
2106            return True
2107        if isinstance(rv, IndexExpr) and self.is_type_ref(rv.base, bare=False):
2108            return True
2109        if self.is_none_alias(rv):
2110            return True
2111        return False
2112
2113    def is_type_ref(self, rv: Expression, bare: bool = False) -> bool:
2114        """Does this expression refer to a type?
2115
2116        This includes:
2117          * Special forms, like Any or Union
2118          * Classes (except subscripted enums)
2119          * Other type aliases
2120          * PlaceholderNodes with becomes_typeinfo=True (these can be not ready class
2121            definitions, and not ready aliases).
2122
2123        If bare is True, this is not a base of an index expression, so some special
2124        forms are not valid (like a bare Union).
2125
2126        Note: This method should be only used in context of a type alias definition.
2127        This method can only return True for RefExprs, to check if C[int] is a valid
2128        target for type alias call this method on expr.base (i.e. on C in C[int]).
2129        See also can_be_type_alias().
2130        """
2131        if not isinstance(rv, RefExpr):
2132            return False
2133        if isinstance(rv.node, TypeVarExpr):
2134            self.fail('Type variable "{}" is invalid as target for type alias'.format(
2135                rv.fullname), rv)
2136            return False
2137
2138        if bare:
2139            # These three are valid even if bare, for example
2140            # A = Tuple is just equivalent to A = Tuple[Any, ...].
2141            valid_refs = {'typing.Any', 'typing.Tuple', 'typing.Callable'}
2142        else:
2143            valid_refs = type_constructors
2144
2145        if isinstance(rv.node, TypeAlias) or rv.fullname in valid_refs:
2146            return True
2147        if isinstance(rv.node, TypeInfo):
2148            if bare:
2149                return True
2150            # Assignment color = Color['RED'] defines a variable, not an alias.
2151            return not rv.node.is_enum
2152
2153        if isinstance(rv, NameExpr):
2154            n = self.lookup(rv.name, rv)
2155            if n and isinstance(n.node, PlaceholderNode) and n.node.becomes_typeinfo:
2156                return True
2157        elif isinstance(rv, MemberExpr):
2158            fname = get_member_expr_fullname(rv)
2159            if fname:
2160                # The r.h.s. for variable definitions may not be a type reference but just
2161                # an instance attribute, so suppress the errors.
2162                n = self.lookup_qualified(fname, rv, suppress_errors=True)
2163                if n and isinstance(n.node, PlaceholderNode) and n.node.becomes_typeinfo:
2164                    return True
2165        return False
2166
2167    def is_none_alias(self, node: Expression) -> bool:
2168        """Is this a r.h.s. for a None alias?
2169
2170        We special case the assignments like Void = type(None), to allow using
2171        Void in type annotations.
2172        """
2173        if isinstance(node, CallExpr):
2174            if (isinstance(node.callee, NameExpr) and len(node.args) == 1 and
2175                    isinstance(node.args[0], NameExpr)):
2176                call = self.lookup_qualified(node.callee.name, node.callee)
2177                arg = self.lookup_qualified(node.args[0].name, node.args[0])
2178                if (call is not None and call.node and call.node.fullname == 'builtins.type' and
2179                        arg is not None and arg.node and arg.node.fullname == 'builtins.None'):
2180                    return True
2181        return False
2182
2183    def record_special_form_lvalue(self, s: AssignmentStmt) -> None:
2184        """Record minimal necessary information about l.h.s. of a special form.
2185
2186        This exists mostly for compatibility with the old semantic analyzer.
2187        """
2188        lvalue = s.lvalues[0]
2189        assert isinstance(lvalue, NameExpr)
2190        lvalue.is_special_form = True
2191        if self.current_symbol_kind() == GDEF:
2192            lvalue.fullname = self.qualified_name(lvalue.name)
2193        lvalue.kind = self.current_symbol_kind()
2194
2195    def analyze_enum_assign(self, s: AssignmentStmt) -> bool:
2196        """Check if s defines an Enum."""
2197        if isinstance(s.rvalue, CallExpr) and isinstance(s.rvalue.analyzed, EnumCallExpr):
2198            # Already analyzed enum -- nothing to do here.
2199            return True
2200        return self.enum_call_analyzer.process_enum_call(s, self.is_func_scope())
2201
2202    def analyze_namedtuple_assign(self, s: AssignmentStmt) -> bool:
2203        """Check if s defines a namedtuple."""
2204        if isinstance(s.rvalue, CallExpr) and isinstance(s.rvalue.analyzed, NamedTupleExpr):
2205            return True  # This is a valid and analyzed named tuple definition, nothing to do here.
2206        if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], (NameExpr, MemberExpr)):
2207            return False
2208        lvalue = s.lvalues[0]
2209        name = lvalue.name
2210        internal_name, info = self.named_tuple_analyzer.check_namedtuple(s.rvalue, name,
2211                                                                         self.is_func_scope())
2212        if internal_name is None:
2213            return False
2214        if isinstance(lvalue, MemberExpr):
2215            self.fail("NamedTuple type as an attribute is not supported", lvalue)
2216            return False
2217        if internal_name != name:
2218            self.fail('First argument to namedtuple() should be "{}", not "{}"'.format(
2219                name, internal_name), s.rvalue, code=codes.NAME_MATCH)
2220            return True
2221        # Yes, it's a valid namedtuple, but defer if it is not ready.
2222        if not info:
2223            self.mark_incomplete(name, lvalue, becomes_typeinfo=True)
2224        return True
2225
2226    def analyze_typeddict_assign(self, s: AssignmentStmt) -> bool:
2227        """Check if s defines a typed dict."""
2228        if isinstance(s.rvalue, CallExpr) and isinstance(s.rvalue.analyzed, TypedDictExpr):
2229            return True  # This is a valid and analyzed typed dict definition, nothing to do here.
2230        if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], (NameExpr, MemberExpr)):
2231            return False
2232        lvalue = s.lvalues[0]
2233        name = lvalue.name
2234        is_typed_dict, info = self.typed_dict_analyzer.check_typeddict(s.rvalue, name,
2235                                                                       self.is_func_scope())
2236        if not is_typed_dict:
2237            return False
2238        if isinstance(lvalue, MemberExpr):
2239            self.fail("TypedDict type as attribute is not supported", lvalue)
2240            return False
2241        # Yes, it's a valid typed dict, but defer if it is not ready.
2242        if not info:
2243            self.mark_incomplete(name, lvalue, becomes_typeinfo=True)
2244        return True
2245
2246    def analyze_lvalues(self, s: AssignmentStmt) -> None:
2247        # We cannot use s.type, because analyze_simple_literal_type() will set it.
2248        explicit = s.unanalyzed_type is not None
2249        if self.is_final_type(s.unanalyzed_type):
2250            # We need to exclude bare Final.
2251            assert isinstance(s.unanalyzed_type, UnboundType)
2252            if not s.unanalyzed_type.args:
2253                explicit = False
2254        for lval in s.lvalues:
2255            self.analyze_lvalue(lval,
2256                                explicit_type=explicit,
2257                                is_final=s.is_final_def)
2258
2259    def apply_dynamic_class_hook(self, s: AssignmentStmt) -> None:
2260        if len(s.lvalues) > 1:
2261            return
2262        lval = s.lvalues[0]
2263        if not isinstance(lval, NameExpr) or not isinstance(s.rvalue, CallExpr):
2264            return
2265        call = s.rvalue
2266        fname = None
2267        if isinstance(call.callee, RefExpr):
2268            fname = call.callee.fullname
2269        # check if method call
2270        if fname is None and isinstance(call.callee, MemberExpr):
2271            callee_expr = call.callee.expr
2272            if isinstance(callee_expr, RefExpr) and callee_expr.fullname:
2273                method_name = call.callee.name
2274                fname = callee_expr.fullname + '.' + method_name
2275        if fname:
2276            hook = self.plugin.get_dynamic_class_hook(fname)
2277            if hook:
2278                hook(DynamicClassDefContext(call, lval.name, self))
2279
2280    def unwrap_final(self, s: AssignmentStmt) -> bool:
2281        """Strip Final[...] if present in an assignment.
2282
2283        This is done to invoke type inference during type checking phase for this
2284        assignment. Also, Final[...] desn't affect type in any way -- it is rather an
2285        access qualifier for given `Var`.
2286
2287        Also perform various consistency checks.
2288
2289        Returns True if Final[...] was present.
2290        """
2291        if not s.unanalyzed_type or not self.is_final_type(s.unanalyzed_type):
2292            return False
2293        assert isinstance(s.unanalyzed_type, UnboundType)
2294        if len(s.unanalyzed_type.args) > 1:
2295            self.fail("Final[...] takes at most one type argument", s.unanalyzed_type)
2296        invalid_bare_final = False
2297        if not s.unanalyzed_type.args:
2298            s.type = None
2299            if isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs:
2300                invalid_bare_final = True
2301                self.fail("Type in Final[...] can only be omitted if there is an initializer", s)
2302        else:
2303            s.type = s.unanalyzed_type.args[0]
2304
2305        if s.type is not None and self.is_classvar(s.type):
2306            self.fail("Variable should not be annotated with both ClassVar and Final", s)
2307            return False
2308
2309        if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], RefExpr):
2310            self.fail("Invalid final declaration", s)
2311            return False
2312        lval = s.lvalues[0]
2313        assert isinstance(lval, RefExpr)
2314
2315        # Reset inferred status if it was set due to simple literal rvalue on previous iteration.
2316        # TODO: this is a best-effort quick fix, we should avoid the need to manually sync this,
2317        # see https://github.com/python/mypy/issues/6458.
2318        if lval.is_new_def:
2319            lval.is_inferred_def = s.type is None
2320
2321        if self.loop_depth > 0:
2322            self.fail("Cannot use Final inside a loop", s)
2323        if self.type and self.type.is_protocol:
2324            self.msg.protocol_members_cant_be_final(s)
2325        if (isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs and
2326                not self.is_stub_file and not self.is_class_scope()):
2327            if not invalid_bare_final:  # Skip extra error messages.
2328                self.msg.final_without_value(s)
2329        return True
2330
2331    def check_final_implicit_def(self, s: AssignmentStmt) -> None:
2332        """Do basic checks for final declaration on self in __init__.
2333
2334        Additional re-definition checks are performed by `analyze_lvalue`.
2335        """
2336        if not s.is_final_def:
2337            return
2338        lval = s.lvalues[0]
2339        assert isinstance(lval, RefExpr)
2340        if isinstance(lval, MemberExpr):
2341            if not self.is_self_member_ref(lval):
2342                self.fail("Final can be only applied to a name or an attribute on self", s)
2343                s.is_final_def = False
2344                return
2345            else:
2346                assert self.function_stack
2347                if self.function_stack[-1].name != '__init__':
2348                    self.fail("Can only declare a final attribute in class body or __init__", s)
2349                    s.is_final_def = False
2350                    return
2351
2352    def store_final_status(self, s: AssignmentStmt) -> None:
2353        """If this is a locally valid final declaration, set the corresponding flag on `Var`."""
2354        if s.is_final_def:
2355            if len(s.lvalues) == 1 and isinstance(s.lvalues[0], RefExpr):
2356                node = s.lvalues[0].node
2357                if isinstance(node, Var):
2358                    node.is_final = True
2359                    node.final_value = self.unbox_literal(s.rvalue)
2360                    if (self.is_class_scope() and
2361                            (isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs)):
2362                        node.final_unset_in_class = True
2363        else:
2364            # Special case: deferred initialization of a final attribute in __init__.
2365            # In this case we just pretend this is a valid final definition to suppress
2366            # errors about assigning to final attribute.
2367            for lval in self.flatten_lvalues(s.lvalues):
2368                if isinstance(lval, MemberExpr) and self.is_self_member_ref(lval):
2369                    assert self.type, "Self member outside a class"
2370                    cur_node = self.type.names.get(lval.name, None)
2371                    if cur_node and isinstance(cur_node.node, Var) and cur_node.node.is_final:
2372                        assert self.function_stack
2373                        top_function = self.function_stack[-1]
2374                        if (top_function.name == '__init__' and
2375                                cur_node.node.final_unset_in_class and
2376                                not cur_node.node.final_set_in_init and
2377                                not (isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs)):
2378                            cur_node.node.final_set_in_init = True
2379                            s.is_final_def = True
2380
2381    def flatten_lvalues(self, lvalues: List[Expression]) -> List[Expression]:
2382        res = []  # type: List[Expression]
2383        for lv in lvalues:
2384            if isinstance(lv, (TupleExpr, ListExpr)):
2385                res.extend(self.flatten_lvalues(lv.items))
2386            else:
2387                res.append(lv)
2388        return res
2389
2390    def unbox_literal(self, e: Expression) -> Optional[Union[int, float, bool, str]]:
2391        if isinstance(e, (IntExpr, FloatExpr, StrExpr)):
2392            return e.value
2393        elif isinstance(e, NameExpr) and e.name in ('True', 'False'):
2394            return True if e.name == 'True' else False
2395        return None
2396
2397    def process_type_annotation(self, s: AssignmentStmt) -> None:
2398        """Analyze type annotation or infer simple literal type."""
2399        if s.type:
2400            lvalue = s.lvalues[-1]
2401            allow_tuple_literal = isinstance(lvalue, TupleExpr)
2402            analyzed = self.anal_type(s.type, allow_tuple_literal=allow_tuple_literal)
2403            # Don't store not ready types (including placeholders).
2404            if analyzed is None or has_placeholder(analyzed):
2405                return
2406            s.type = analyzed
2407            if (self.type and self.type.is_protocol and isinstance(lvalue, NameExpr) and
2408                    isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs):
2409                if isinstance(lvalue.node, Var):
2410                    lvalue.node.is_abstract_var = True
2411        else:
2412            if (self.type and self.type.is_protocol and
2413                    self.is_annotated_protocol_member(s) and not self.is_func_scope()):
2414                self.fail('All protocol members must have explicitly declared types', s)
2415            # Set the type if the rvalue is a simple literal (even if the above error occurred).
2416            if len(s.lvalues) == 1 and isinstance(s.lvalues[0], RefExpr):
2417                if s.lvalues[0].is_inferred_def:
2418                    s.type = self.analyze_simple_literal_type(s.rvalue, s.is_final_def)
2419        if s.type:
2420            # Store type into nodes.
2421            for lvalue in s.lvalues:
2422                self.store_declared_types(lvalue, s.type)
2423
2424    def is_annotated_protocol_member(self, s: AssignmentStmt) -> bool:
2425        """Check whether a protocol member is annotated.
2426
2427        There are some exceptions that can be left unannotated, like ``__slots__``."""
2428        return any(
2429            (
2430                isinstance(lv, NameExpr)
2431                and lv.name != '__slots__'
2432                and lv.is_inferred_def
2433            )
2434            for lv in s.lvalues
2435        )
2436
2437    def analyze_simple_literal_type(self, rvalue: Expression, is_final: bool) -> Optional[Type]:
2438        """Return builtins.int if rvalue is an int literal, etc.
2439
2440        If this is a 'Final' context, we return "Literal[...]" instead."""
2441        if self.options.semantic_analysis_only or self.function_stack:
2442            # Skip this if we're only doing the semantic analysis pass.
2443            # This is mostly to avoid breaking unit tests.
2444            # Also skip inside a function; this is to avoid confusing
2445            # the code that handles dead code due to isinstance()
2446            # inside type variables with value restrictions (like
2447            # AnyStr).
2448            return None
2449        if isinstance(rvalue, FloatExpr):
2450            return self.named_type_or_none('builtins.float')
2451
2452        value = None  # type: Optional[LiteralValue]
2453        type_name = None  # type: Optional[str]
2454        if isinstance(rvalue, IntExpr):
2455            value, type_name = rvalue.value, 'builtins.int'
2456        if isinstance(rvalue, StrExpr):
2457            value, type_name = rvalue.value, 'builtins.str'
2458        if isinstance(rvalue, BytesExpr):
2459            value, type_name = rvalue.value, 'builtins.bytes'
2460        if isinstance(rvalue, UnicodeExpr):
2461            value, type_name = rvalue.value, 'builtins.unicode'
2462
2463        if type_name is not None:
2464            assert value is not None
2465            typ = self.named_type_or_none(type_name)
2466            if typ and is_final:
2467                return typ.copy_modified(last_known_value=LiteralType(
2468                    value=value,
2469                    fallback=typ,
2470                    line=typ.line,
2471                    column=typ.column,
2472                ))
2473            return typ
2474
2475        return None
2476
2477    def analyze_alias(self, rvalue: Expression,
2478                      allow_placeholder: bool = False) -> Tuple[Optional[Type], List[str],
2479                                                                Set[str], List[str]]:
2480        """Check if 'rvalue' is a valid type allowed for aliasing (e.g. not a type variable).
2481
2482        If yes, return the corresponding type, a list of
2483        qualified type variable names for generic aliases, a set of names the alias depends on,
2484        and a list of type variables if the alias is generic.
2485        An schematic example for the dependencies:
2486            A = int
2487            B = str
2488            analyze_alias(Dict[A, B])[2] == {'__main__.A', '__main__.B'}
2489        """
2490        dynamic = bool(self.function_stack and self.function_stack[-1].is_dynamic())
2491        global_scope = not self.type and not self.function_stack
2492        res = analyze_type_alias(rvalue,
2493                                 self,
2494                                 self.tvar_scope,
2495                                 self.plugin,
2496                                 self.options,
2497                                 self.is_typeshed_stub_file,
2498                                 allow_unnormalized=self.is_stub_file,
2499                                 allow_placeholder=allow_placeholder,
2500                                 in_dynamic_func=dynamic,
2501                                 global_scope=global_scope)
2502        typ = None  # type: Optional[Type]
2503        if res:
2504            typ, depends_on = res
2505            found_type_vars = typ.accept(TypeVarLikeQuery(self.lookup_qualified, self.tvar_scope))
2506            alias_tvars = [name for (name, node) in found_type_vars]
2507            qualified_tvars = [node.fullname for (name, node) in found_type_vars]
2508        else:
2509            alias_tvars = []
2510            depends_on = set()
2511            qualified_tvars = []
2512        return typ, alias_tvars, depends_on, qualified_tvars
2513
2514    def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
2515        """Check if assignment creates a type alias and set it up as needed.
2516
2517        Return True if it is a type alias (even if the target is not ready),
2518        or False otherwise.
2519
2520        Note: the resulting types for subscripted (including generic) aliases
2521        are also stored in rvalue.analyzed.
2522        """
2523        lvalue = s.lvalues[0]
2524        if len(s.lvalues) > 1 or not isinstance(lvalue, NameExpr):
2525            # First rule: Only simple assignments like Alias = ... create aliases.
2526            return False
2527        if s.unanalyzed_type is not None:
2528            # Second rule: Explicit type (cls: Type[A] = A) always creates variable, not alias.
2529            return False
2530
2531        existing = self.current_symbol_table().get(lvalue.name)
2532        # Third rule: type aliases can't be re-defined. For example:
2533        #     A: Type[float] = int
2534        #     A = float  # OK, but this doesn't define an alias
2535        #     B = int
2536        #     B = float  # Error!
2537        # Don't create an alias in these cases:
2538        if (existing
2539                and (isinstance(existing.node, Var)  # existing variable
2540                     or (isinstance(existing.node, TypeAlias)
2541                         and not s.is_alias_def)  # existing alias
2542                     or (isinstance(existing.node, PlaceholderNode)
2543                         and existing.node.node.line < s.line))):  # previous incomplete definition
2544            # TODO: find a more robust way to track the order of definitions.
2545            # Note: if is_alias_def=True, this is just a node from previous iteration.
2546            if isinstance(existing.node, TypeAlias) and not s.is_alias_def:
2547                self.fail('Cannot assign multiple types to name "{}"'
2548                          ' without an explicit "Type[...]" annotation'
2549                          .format(lvalue.name), lvalue)
2550            return False
2551
2552        non_global_scope = self.type or self.is_func_scope()
2553        if isinstance(s.rvalue, RefExpr) and non_global_scope:
2554            # Fourth rule (special case): Non-subscripted right hand side creates a variable
2555            # at class and function scopes. For example:
2556            #
2557            #   class Model:
2558            #       ...
2559            #   class C:
2560            #       model = Model # this is automatically a variable with type 'Type[Model]'
2561            #
2562            # without this rule, this typical use case will require a lot of explicit
2563            # annotations (see the second rule).
2564            return False
2565        rvalue = s.rvalue
2566        if not self.can_be_type_alias(rvalue):
2567            return False
2568
2569        if existing and not isinstance(existing.node, (PlaceholderNode, TypeAlias)):
2570            # Cannot redefine existing node as type alias.
2571            return False
2572
2573        res = None  # type: Optional[Type]
2574        if self.is_none_alias(rvalue):
2575            res = NoneType()
2576            alias_tvars, depends_on, qualified_tvars = \
2577                [], set(), []  # type: List[str], Set[str], List[str]
2578        else:
2579            tag = self.track_incomplete_refs()
2580            res, alias_tvars, depends_on, qualified_tvars = \
2581                self.analyze_alias(rvalue, allow_placeholder=True)
2582            if not res:
2583                return False
2584            # TODO: Maybe we only need to reject top-level placeholders, similar
2585            #       to base classes.
2586            if self.found_incomplete_ref(tag) or has_placeholder(res):
2587                # Since we have got here, we know this must be a type alias (incomplete refs
2588                # may appear in nested positions), therefore use becomes_typeinfo=True.
2589                self.mark_incomplete(lvalue.name, rvalue, becomes_typeinfo=True)
2590                return True
2591        self.add_type_alias_deps(depends_on)
2592        # In addition to the aliases used, we add deps on unbound
2593        # type variables, since they are erased from target type.
2594        self.add_type_alias_deps(qualified_tvars)
2595        # The above are only direct deps on other aliases.
2596        # For subscripted aliases, type deps from expansion are added in deps.py
2597        # (because the type is stored).
2598        check_for_explicit_any(res, self.options, self.is_typeshed_stub_file, self.msg,
2599                               context=s)
2600        # When this type alias gets "inlined", the Any is not explicit anymore,
2601        # so we need to replace it with non-explicit Anys.
2602        if not has_placeholder(res):
2603            res = make_any_non_explicit(res)
2604        # Note: with the new (lazy) type alias representation we only need to set no_args to True
2605        # if the expected number of arguments is non-zero, so that aliases like A = List work.
2606        # However, eagerly expanding aliases like Text = str is a nice performance optimization.
2607        no_args = isinstance(res, Instance) and not res.args  # type: ignore[misc]
2608        fix_instance_types(res, self.fail, self.note, self.options.python_version)
2609        # Aliases defined within functions can't be accessed outside
2610        # the function, since the symbol table will no longer
2611        # exist. Work around by expanding them eagerly when used.
2612        eager = self.is_func_scope()
2613        alias_node = TypeAlias(res,
2614                               self.qualified_name(lvalue.name),
2615                               s.line,
2616                               s.column,
2617                               alias_tvars=alias_tvars,
2618                               no_args=no_args,
2619                               eager=eager)
2620        if isinstance(s.rvalue, (IndexExpr, CallExpr)):  # CallExpr is for `void = type(None)`
2621            s.rvalue.analyzed = TypeAliasExpr(alias_node)
2622            s.rvalue.analyzed.line = s.line
2623            # we use the column from resulting target, to get better location for errors
2624            s.rvalue.analyzed.column = res.column
2625        elif isinstance(s.rvalue, RefExpr):
2626            s.rvalue.is_alias_rvalue = True
2627
2628        if existing:
2629            # An alias gets updated.
2630            updated = False
2631            if isinstance(existing.node, TypeAlias):
2632                if existing.node.target != res:
2633                    # Copy expansion to the existing alias, this matches how we update base classes
2634                    # for a TypeInfo _in place_ if there are nested placeholders.
2635                    existing.node.target = res
2636                    existing.node.alias_tvars = alias_tvars
2637                    existing.node.no_args = no_args
2638                    updated = True
2639            else:
2640                # Otherwise just replace existing placeholder with type alias.
2641                existing.node = alias_node
2642                updated = True
2643            if updated:
2644                if self.final_iteration:
2645                    self.cannot_resolve_name(lvalue.name, 'name', s)
2646                    return True
2647                else:
2648                    self.progress = True
2649                    # We need to defer so that this change can get propagated to base classes.
2650                    self.defer(s)
2651        else:
2652            self.add_symbol(lvalue.name, alias_node, s)
2653        if isinstance(rvalue, RefExpr) and isinstance(rvalue.node, TypeAlias):
2654            alias_node.normalized = rvalue.node.normalized
2655        return True
2656
2657    def analyze_lvalue(self,
2658                       lval: Lvalue,
2659                       nested: bool = False,
2660                       explicit_type: bool = False,
2661                       is_final: bool = False,
2662                       escape_comprehensions: bool = False) -> None:
2663        """Analyze an lvalue or assignment target.
2664
2665        Args:
2666            lval: The target lvalue
2667            nested: If true, the lvalue is within a tuple or list lvalue expression
2668            explicit_type: Assignment has type annotation
2669            escape_comprehensions: If we are inside a comprehension, set the variable
2670                in the enclosing scope instead. This implements
2671                https://www.python.org/dev/peps/pep-0572/#scope-of-the-target
2672        """
2673        if escape_comprehensions:
2674            assert isinstance(lval, NameExpr), "assignment expression target must be NameExpr"
2675        if isinstance(lval, NameExpr):
2676            self.analyze_name_lvalue(lval, explicit_type, is_final, escape_comprehensions)
2677        elif isinstance(lval, MemberExpr):
2678            self.analyze_member_lvalue(lval, explicit_type, is_final)
2679            if explicit_type and not self.is_self_member_ref(lval):
2680                self.fail('Type cannot be declared in assignment to non-self '
2681                          'attribute', lval)
2682        elif isinstance(lval, IndexExpr):
2683            if explicit_type:
2684                self.fail('Unexpected type declaration', lval)
2685            lval.accept(self)
2686        elif isinstance(lval, TupleExpr):
2687            self.analyze_tuple_or_list_lvalue(lval, explicit_type)
2688        elif isinstance(lval, StarExpr):
2689            if nested:
2690                self.analyze_lvalue(lval.expr, nested, explicit_type)
2691            else:
2692                self.fail('Starred assignment target must be in a list or tuple', lval)
2693        else:
2694            self.fail('Invalid assignment target', lval)
2695
2696    def analyze_name_lvalue(self,
2697                            lvalue: NameExpr,
2698                            explicit_type: bool,
2699                            is_final: bool,
2700                            escape_comprehensions: bool) -> None:
2701        """Analyze an lvalue that targets a name expression.
2702
2703        Arguments are similar to "analyze_lvalue".
2704        """
2705        if lvalue.node:
2706            # This has been bound already in a previous iteration.
2707            return
2708
2709        name = lvalue.name
2710        if self.is_alias_for_final_name(name):
2711            if is_final:
2712                self.fail("Cannot redefine an existing name as final", lvalue)
2713            else:
2714                self.msg.cant_assign_to_final(name, self.type is not None, lvalue)
2715
2716        kind = self.current_symbol_kind()
2717        names = self.current_symbol_table()
2718        existing = names.get(name)
2719
2720        outer = self.is_global_or_nonlocal(name)
2721        if (not existing or isinstance(existing.node, PlaceholderNode)) and not outer:
2722            # Define new variable.
2723            var = self.make_name_lvalue_var(lvalue, kind, not explicit_type)
2724            added = self.add_symbol(name, var, lvalue, escape_comprehensions=escape_comprehensions)
2725            # Only bind expression if we successfully added name to symbol table.
2726            if added:
2727                lvalue.is_new_def = True
2728                lvalue.is_inferred_def = True
2729                lvalue.kind = kind
2730                lvalue.node = var
2731                if kind == GDEF:
2732                    lvalue.fullname = var._fullname
2733                else:
2734                    lvalue.fullname = lvalue.name
2735                if self.is_func_scope():
2736                    if unmangle(name) == '_':
2737                        # Special case for assignment to local named '_': always infer 'Any'.
2738                        typ = AnyType(TypeOfAny.special_form)
2739                        self.store_declared_types(lvalue, typ)
2740            if is_final and self.is_final_redefinition(kind, name):
2741                self.fail("Cannot redefine an existing name as final", lvalue)
2742        else:
2743            self.make_name_lvalue_point_to_existing_def(lvalue, explicit_type, is_final)
2744
2745    def is_final_redefinition(self, kind: int, name: str) -> bool:
2746        if kind == GDEF:
2747            return self.is_mangled_global(name) and not self.is_initial_mangled_global(name)
2748        elif kind == MDEF and self.type:
2749            return unmangle(name) + "'" in self.type.names
2750        return False
2751
2752    def is_alias_for_final_name(self, name: str) -> bool:
2753        if self.is_func_scope():
2754            if not name.endswith("'"):
2755                # Not a mangled name -- can't be an alias
2756                return False
2757            name = unmangle(name)
2758            assert self.locals[-1] is not None, "No locals at function scope"
2759            existing = self.locals[-1].get(name)
2760            return existing is not None and is_final_node(existing.node)
2761        elif self.type is not None:
2762            orig_name = unmangle(name) + "'"
2763            if name == orig_name:
2764                return False
2765            existing = self.type.names.get(orig_name)
2766            return existing is not None and is_final_node(existing.node)
2767        else:
2768            orig_name = unmangle(name) + "'"
2769            if name == orig_name:
2770                return False
2771            existing = self.globals.get(orig_name)
2772            return existing is not None and is_final_node(existing.node)
2773
2774    def make_name_lvalue_var(self, lvalue: NameExpr, kind: int, inferred: bool) -> Var:
2775        """Return a Var node for an lvalue that is a name expression."""
2776        v = Var(lvalue.name)
2777        v.set_line(lvalue)
2778        v.is_inferred = inferred
2779        if kind == MDEF:
2780            assert self.type is not None
2781            v.info = self.type
2782            v.is_initialized_in_class = True
2783        if kind != LDEF:
2784            v._fullname = self.qualified_name(lvalue.name)
2785        else:
2786            # fullanme should never stay None
2787            v._fullname = lvalue.name
2788        v.is_ready = False  # Type not inferred yet
2789        return v
2790
2791    def make_name_lvalue_point_to_existing_def(
2792            self,
2793            lval: NameExpr,
2794            explicit_type: bool,
2795            is_final: bool) -> None:
2796        """Update an lvalue to point to existing definition in the same scope.
2797
2798        Arguments are similar to "analyze_lvalue".
2799
2800        Assume that an existing name exists.
2801        """
2802        if is_final:
2803            # Redefining an existing name with final is always an error.
2804            self.fail("Cannot redefine an existing name as final", lval)
2805        original_def = self.lookup(lval.name, lval, suppress_errors=True)
2806        if original_def is None and self.type and not self.is_func_scope():
2807            # Workaround to allow "x, x = ..." in class body.
2808            original_def = self.type.get(lval.name)
2809        if explicit_type:
2810            # Don't re-bind if there is a type annotation.
2811            self.name_already_defined(lval.name, lval, original_def)
2812        else:
2813            # Bind to an existing name.
2814            if original_def:
2815                self.bind_name_expr(lval, original_def)
2816            else:
2817                self.name_not_defined(lval.name, lval)
2818            self.check_lvalue_validity(lval.node, lval)
2819
2820    def analyze_tuple_or_list_lvalue(self, lval: TupleExpr,
2821                                     explicit_type: bool = False) -> None:
2822        """Analyze an lvalue or assignment target that is a list or tuple."""
2823        items = lval.items
2824        star_exprs = [item for item in items if isinstance(item, StarExpr)]
2825
2826        if len(star_exprs) > 1:
2827            self.fail('Two starred expressions in assignment', lval)
2828        else:
2829            if len(star_exprs) == 1:
2830                star_exprs[0].valid = True
2831            for i in items:
2832                self.analyze_lvalue(i, nested=True, explicit_type=explicit_type)
2833
2834    def analyze_member_lvalue(self, lval: MemberExpr, explicit_type: bool, is_final: bool) -> None:
2835        """Analyze lvalue that is a member expression.
2836
2837        Arguments:
2838            lval: The target lvalue
2839            explicit_type: Assignment has type annotation
2840            is_final: Is the target final
2841        """
2842        if lval.node:
2843            # This has been bound already in a previous iteration.
2844            return
2845        lval.accept(self)
2846        if self.is_self_member_ref(lval):
2847            assert self.type, "Self member outside a class"
2848            cur_node = self.type.names.get(lval.name)
2849            node = self.type.get(lval.name)
2850            if cur_node and is_final:
2851                # Overrides will be checked in type checker.
2852                self.fail("Cannot redefine an existing name as final", lval)
2853            # On first encounter with this definition, if this attribute was defined before
2854            # with an inferred type and it's marked with an explicit type now, give an error.
2855            if (not lval.node and cur_node and isinstance(cur_node.node, Var) and
2856                    cur_node.node.is_inferred and explicit_type):
2857                self.attribute_already_defined(lval.name, lval, cur_node)
2858            # If the attribute of self is not defined in superclasses, create a new Var, ...
2859            if (node is None
2860                    or (isinstance(node.node, Var) and node.node.is_abstract_var)
2861                    # ... also an explicit declaration on self also creates a new Var.
2862                    # Note that `explicit_type` might has been erased for bare `Final`,
2863                    # so we also check if `is_final` is passed.
2864                    or (cur_node is None and (explicit_type or is_final))):
2865                if self.type.is_protocol and node is None:
2866                    self.fail("Protocol members cannot be defined via assignment to self", lval)
2867                else:
2868                    # Implicit attribute definition in __init__.
2869                    lval.is_new_def = True
2870                    lval.is_inferred_def = True
2871                    v = Var(lval.name)
2872                    v.set_line(lval)
2873                    v._fullname = self.qualified_name(lval.name)
2874                    v.info = self.type
2875                    v.is_ready = False
2876                    v.explicit_self_type = explicit_type or is_final
2877                    lval.def_var = v
2878                    lval.node = v
2879                    # TODO: should we also set lval.kind = MDEF?
2880                    self.type.names[lval.name] = SymbolTableNode(MDEF, v, implicit=True)
2881        self.check_lvalue_validity(lval.node, lval)
2882
2883    def is_self_member_ref(self, memberexpr: MemberExpr) -> bool:
2884        """Does memberexpr to refer to an attribute of self?"""
2885        if not isinstance(memberexpr.expr, NameExpr):
2886            return False
2887        node = memberexpr.expr.node
2888        return isinstance(node, Var) and node.is_self
2889
2890    def check_lvalue_validity(self, node: Union[Expression, SymbolNode, None],
2891                              ctx: Context) -> None:
2892        if isinstance(node, TypeVarExpr):
2893            self.fail('Invalid assignment target', ctx)
2894        elif isinstance(node, TypeInfo):
2895            self.fail(message_registry.CANNOT_ASSIGN_TO_TYPE, ctx)
2896
2897    def store_declared_types(self, lvalue: Lvalue, typ: Type) -> None:
2898        if isinstance(typ, StarType) and not isinstance(lvalue, StarExpr):
2899            self.fail('Star type only allowed for starred expressions', lvalue)
2900        if isinstance(lvalue, RefExpr):
2901            lvalue.is_inferred_def = False
2902            if isinstance(lvalue.node, Var):
2903                var = lvalue.node
2904                var.type = typ
2905                var.is_ready = True
2906            # If node is not a variable, we'll catch it elsewhere.
2907        elif isinstance(lvalue, TupleExpr):
2908            typ = get_proper_type(typ)
2909            if isinstance(typ, TupleType):
2910                if len(lvalue.items) != len(typ.items):
2911                    self.fail('Incompatible number of tuple items', lvalue)
2912                    return
2913                for item, itemtype in zip(lvalue.items, typ.items):
2914                    self.store_declared_types(item, itemtype)
2915            else:
2916                self.fail('Tuple type expected for multiple variables',
2917                          lvalue)
2918        elif isinstance(lvalue, StarExpr):
2919            # Historical behavior for the old parser
2920            if isinstance(typ, StarType):
2921                self.store_declared_types(lvalue.expr, typ.type)
2922            else:
2923                self.store_declared_types(lvalue.expr, typ)
2924        else:
2925            # This has been flagged elsewhere as an error, so just ignore here.
2926            pass
2927
2928    def process_typevar_declaration(self, s: AssignmentStmt) -> bool:
2929        """Check if s declares a TypeVar; it yes, store it in symbol table.
2930
2931        Return True if this looks like a type variable declaration (but maybe
2932        with errors), otherwise return False.
2933        """
2934        call = self.get_typevarlike_declaration(s, ("typing.TypeVar",))
2935        if not call:
2936            return False
2937
2938        lvalue = s.lvalues[0]
2939        assert isinstance(lvalue, NameExpr)
2940        if s.type:
2941            self.fail("Cannot declare the type of a type variable", s)
2942            return False
2943
2944        name = lvalue.name
2945        if not self.check_typevarlike_name(call, name, s):
2946            return False
2947
2948        # Constraining types
2949        n_values = call.arg_kinds[1:].count(ARG_POS)
2950        values = self.analyze_value_types(call.args[1:1 + n_values])
2951
2952        res = self.process_typevar_parameters(call.args[1 + n_values:],
2953                                              call.arg_names[1 + n_values:],
2954                                              call.arg_kinds[1 + n_values:],
2955                                              n_values,
2956                                              s)
2957        if res is None:
2958            return False
2959        variance, upper_bound = res
2960
2961        existing = self.current_symbol_table().get(name)
2962        if existing and not (isinstance(existing.node, PlaceholderNode) or
2963                             # Also give error for another type variable with the same name.
2964                             (isinstance(existing.node, TypeVarExpr) and
2965                              existing.node is call.analyzed)):
2966            self.fail('Cannot redefine "%s" as a type variable' % name, s)
2967            return False
2968
2969        if self.options.disallow_any_unimported:
2970            for idx, constraint in enumerate(values, start=1):
2971                if has_any_from_unimported_type(constraint):
2972                    prefix = "Constraint {}".format(idx)
2973                    self.msg.unimported_type_becomes_any(prefix, constraint, s)
2974
2975            if has_any_from_unimported_type(upper_bound):
2976                prefix = "Upper bound of type variable"
2977                self.msg.unimported_type_becomes_any(prefix, upper_bound, s)
2978
2979        for t in values + [upper_bound]:
2980            check_for_explicit_any(t, self.options, self.is_typeshed_stub_file, self.msg,
2981                                   context=s)
2982
2983        # mypyc suppresses making copies of a function to check each
2984        # possible type, so set the upper bound to Any to prevent that
2985        # from causing errors.
2986        if values and self.options.mypyc:
2987            upper_bound = AnyType(TypeOfAny.implementation_artifact)
2988
2989        # Yes, it's a valid type variable definition! Add it to the symbol table.
2990        if not call.analyzed:
2991            type_var = TypeVarExpr(name, self.qualified_name(name),
2992                                   values, upper_bound, variance)
2993            type_var.line = call.line
2994            call.analyzed = type_var
2995        else:
2996            assert isinstance(call.analyzed, TypeVarExpr)
2997            if call.analyzed.values != values or call.analyzed.upper_bound != upper_bound:
2998                self.progress = True
2999            call.analyzed.upper_bound = upper_bound
3000            call.analyzed.values = values
3001
3002        self.add_symbol(name, call.analyzed, s)
3003        return True
3004
3005    def check_typevarlike_name(self, call: CallExpr, name: str, context: Context) -> bool:
3006        """Checks that the name of a TypeVar or ParamSpec matches its variable."""
3007        name = unmangle(name)
3008        assert isinstance(call.callee, RefExpr)
3009        typevarlike_type = (
3010            call.callee.name if isinstance(call.callee, NameExpr) else call.callee.fullname
3011        )
3012        if len(call.args) < 1:
3013            self.fail("Too few arguments for {}()".format(typevarlike_type), context)
3014            return False
3015        if (not isinstance(call.args[0], (StrExpr, BytesExpr, UnicodeExpr))
3016                or not call.arg_kinds[0] == ARG_POS):
3017            self.fail("{}() expects a string literal as first argument".format(typevarlike_type),
3018                      context)
3019            return False
3020        elif call.args[0].value != name:
3021            msg = 'String argument 1 "{}" to {}(...) does not match variable name "{}"'
3022            self.fail(msg.format(call.args[0].value, typevarlike_type, name), context)
3023            return False
3024        return True
3025
3026    def get_typevarlike_declaration(self, s: AssignmentStmt,
3027                                    typevarlike_types: Tuple[str, ...]) -> Optional[CallExpr]:
3028        """Returns the call expression if `s` is a declaration of `typevarlike_type`
3029        (TypeVar or ParamSpec), or None otherwise.
3030        """
3031        if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], NameExpr):
3032            return None
3033        if not isinstance(s.rvalue, CallExpr):
3034            return None
3035        call = s.rvalue
3036        callee = call.callee
3037        if not isinstance(callee, RefExpr):
3038            return None
3039        if callee.fullname not in typevarlike_types:
3040            return None
3041        return call
3042
3043    def process_typevar_parameters(self, args: List[Expression],
3044                                   names: List[Optional[str]],
3045                                   kinds: List[int],
3046                                   num_values: int,
3047                                   context: Context) -> Optional[Tuple[int, Type]]:
3048        has_values = (num_values > 0)
3049        covariant = False
3050        contravariant = False
3051        upper_bound = self.object_type()   # type: Type
3052        for param_value, param_name, param_kind in zip(args, names, kinds):
3053            if not param_kind == ARG_NAMED:
3054                self.fail("Unexpected argument to TypeVar()", context)
3055                return None
3056            if param_name == 'covariant':
3057                if isinstance(param_value, NameExpr):
3058                    if param_value.name == 'True':
3059                        covariant = True
3060                    else:
3061                        self.fail("TypeVar 'covariant' may only be 'True'", context)
3062                        return None
3063                else:
3064                    self.fail("TypeVar 'covariant' may only be 'True'", context)
3065                    return None
3066            elif param_name == 'contravariant':
3067                if isinstance(param_value, NameExpr):
3068                    if param_value.name == 'True':
3069                        contravariant = True
3070                    else:
3071                        self.fail("TypeVar 'contravariant' may only be 'True'", context)
3072                        return None
3073                else:
3074                    self.fail("TypeVar 'contravariant' may only be 'True'", context)
3075                    return None
3076            elif param_name == 'bound':
3077                if has_values:
3078                    self.fail("TypeVar cannot have both values and an upper bound", context)
3079                    return None
3080                try:
3081                    # We want to use our custom error message below, so we suppress
3082                    # the default error message for invalid types here.
3083                    analyzed = self.expr_to_analyzed_type(param_value,
3084                                                          allow_placeholder=True,
3085                                                          report_invalid_types=False)
3086                    if analyzed is None:
3087                        # Type variables are special: we need to place them in the symbol table
3088                        # soon, even if upper bound is not ready yet. Otherwise avoiding
3089                        # a "deadlock" in this common pattern would be tricky:
3090                        #     T = TypeVar('T', bound=Custom[Any])
3091                        #     class Custom(Generic[T]):
3092                        #         ...
3093                        analyzed = PlaceholderType(None, [], context.line)
3094                    upper_bound = get_proper_type(analyzed)
3095                    if isinstance(upper_bound, AnyType) and upper_bound.is_from_error:
3096                        self.fail('TypeVar "bound" must be a type', param_value)
3097                        # Note: we do not return 'None' here -- we want to continue
3098                        # using the AnyType as the upper bound.
3099                except TypeTranslationError:
3100                    self.fail('TypeVar "bound" must be a type', param_value)
3101                    return None
3102            elif param_name == 'values':
3103                # Probably using obsolete syntax with values=(...). Explain the current syntax.
3104                self.fail('TypeVar "values" argument not supported', context)
3105                self.fail("Use TypeVar('T', t, ...) instead of TypeVar('T', values=(t, ...))",
3106                          context)
3107                return None
3108            else:
3109                self.fail('Unexpected argument to TypeVar(): "{}"'.format(param_name), context)
3110                return None
3111
3112        if covariant and contravariant:
3113            self.fail("TypeVar cannot be both covariant and contravariant", context)
3114            return None
3115        elif num_values == 1:
3116            self.fail("TypeVar cannot have only a single constraint", context)
3117            return None
3118        elif covariant:
3119            variance = COVARIANT
3120        elif contravariant:
3121            variance = CONTRAVARIANT
3122        else:
3123            variance = INVARIANT
3124        return variance, upper_bound
3125
3126    def process_paramspec_declaration(self, s: AssignmentStmt) -> bool:
3127        """Checks if s declares a ParamSpec; if yes, store it in symbol table.
3128
3129        Return True if this looks like a ParamSpec (maybe with errors), otherwise return False.
3130
3131        In the future, ParamSpec may accept bounds and variance arguments, in which
3132        case more aggressive sharing of code with process_typevar_declaration should be pursued.
3133        """
3134        if not self.options.wip_pep_612:
3135            return False
3136        call = self.get_typevarlike_declaration(
3137            s, ("typing_extensions.ParamSpec", "typing.ParamSpec")
3138        )
3139        if not call:
3140            return False
3141
3142        lvalue = s.lvalues[0]
3143        assert isinstance(lvalue, NameExpr)
3144        if s.type:
3145            self.fail("Cannot declare the type of a parameter specification", s)
3146            return False
3147
3148        name = lvalue.name
3149        if not self.check_typevarlike_name(call, name, s):
3150            return False
3151
3152        # PEP 612 reserves the right to define bound, covariant and contravariant arguments to
3153        # ParamSpec in a later PEP. If and when that happens, we should do something
3154        # on the lines of process_typevar_parameters
3155        paramspec_var = ParamSpecExpr(
3156            name, self.qualified_name(name), self.object_type(), INVARIANT
3157        )
3158        paramspec_var.line = call.line
3159        call.analyzed = paramspec_var
3160        self.add_symbol(name, call.analyzed, s)
3161        return True
3162
3163    def basic_new_typeinfo(self, name: str,
3164                           basetype_or_fallback: Instance,
3165                           line: int) -> TypeInfo:
3166        if self.is_func_scope() and not self.type and '@' not in name:
3167            name += '@' + str(line)
3168        class_def = ClassDef(name, Block([]))
3169        if self.is_func_scope() and not self.type:
3170            # Full names of generated classes should always be prefixed with the module names
3171            # even if they are nested in a function, since these classes will be (de-)serialized.
3172            # (Note that the caller should append @line to the name to avoid collisions.)
3173            # TODO: clean this up, see #6422.
3174            class_def.fullname = self.cur_mod_id + '.' + self.qualified_name(name)
3175        else:
3176            class_def.fullname = self.qualified_name(name)
3177
3178        info = TypeInfo(SymbolTable(), class_def, self.cur_mod_id)
3179        class_def.info = info
3180        mro = basetype_or_fallback.type.mro
3181        if not mro:
3182            # Forward reference, MRO should be recalculated in third pass.
3183            mro = [basetype_or_fallback.type, self.object_type().type]
3184        info.mro = [info] + mro
3185        info.bases = [basetype_or_fallback]
3186        return info
3187
3188    def analyze_value_types(self, items: List[Expression]) -> List[Type]:
3189        """Analyze types from values expressions in type variable definition."""
3190        result = []  # type: List[Type]
3191        for node in items:
3192            try:
3193                analyzed = self.anal_type(expr_to_unanalyzed_type(node),
3194                                          allow_placeholder=True)
3195                if analyzed is None:
3196                    # Type variables are special: we need to place them in the symbol table
3197                    # soon, even if some value is not ready yet, see process_typevar_parameters()
3198                    # for an example.
3199                    analyzed = PlaceholderType(None, [], node.line)
3200                result.append(analyzed)
3201            except TypeTranslationError:
3202                self.fail('Type expected', node)
3203                result.append(AnyType(TypeOfAny.from_error))
3204        return result
3205
3206    def check_classvar(self, s: AssignmentStmt) -> None:
3207        """Check if assignment defines a class variable."""
3208        lvalue = s.lvalues[0]
3209        if len(s.lvalues) != 1 or not isinstance(lvalue, RefExpr):
3210            return
3211        if not s.type or not self.is_classvar(s.type):
3212            return
3213        if self.is_class_scope() and isinstance(lvalue, NameExpr):
3214            node = lvalue.node
3215            if isinstance(node, Var):
3216                node.is_classvar = True
3217        elif not isinstance(lvalue, MemberExpr) or self.is_self_member_ref(lvalue):
3218            # In case of member access, report error only when assigning to self
3219            # Other kinds of member assignments should be already reported
3220            self.fail_invalid_classvar(lvalue)
3221
3222    def is_classvar(self, typ: Type) -> bool:
3223        if not isinstance(typ, UnboundType):
3224            return False
3225        sym = self.lookup_qualified(typ.name, typ)
3226        if not sym or not sym.node:
3227            return False
3228        return sym.node.fullname == 'typing.ClassVar'
3229
3230    def is_final_type(self, typ: Optional[Type]) -> bool:
3231        if not isinstance(typ, UnboundType):
3232            return False
3233        sym = self.lookup_qualified(typ.name, typ)
3234        if not sym or not sym.node:
3235            return False
3236        return sym.node.fullname in ('typing.Final', 'typing_extensions.Final')
3237
3238    def fail_invalid_classvar(self, context: Context) -> None:
3239        self.fail('ClassVar can only be used for assignments in class body', context)
3240
3241    def process_module_assignment(self, lvals: List[Lvalue], rval: Expression,
3242                                  ctx: AssignmentStmt) -> None:
3243        """Propagate module references across assignments.
3244
3245        Recursively handles the simple form of iterable unpacking; doesn't
3246        handle advanced unpacking with *rest, dictionary unpacking, etc.
3247
3248        In an expression like x = y = z, z is the rval and lvals will be [x,
3249        y].
3250
3251        """
3252        if (isinstance(rval, (TupleExpr, ListExpr))
3253                and all(isinstance(v, TupleExpr) for v in lvals)):
3254            # rval and all lvals are either list or tuple, so we are dealing
3255            # with unpacking assignment like `x, y = a, b`. Mypy didn't
3256            # understand our all(isinstance(...)), so cast them as TupleExpr
3257            # so mypy knows it is safe to access their .items attribute.
3258            seq_lvals = cast(List[TupleExpr], lvals)
3259            # given an assignment like:
3260            #     (x, y) = (m, n) = (a, b)
3261            # we now have:
3262            #     seq_lvals = [(x, y), (m, n)]
3263            #     seq_rval = (a, b)
3264            # We now zip this into:
3265            #     elementwise_assignments = [(a, x, m), (b, y, n)]
3266            # where each elementwise assignment includes one element of rval and the
3267            # corresponding element of each lval. Basically we unpack
3268            #     (x, y) = (m, n) = (a, b)
3269            # into elementwise assignments
3270            #     x = m = a
3271            #     y = n = b
3272            # and then we recursively call this method for each of those assignments.
3273            # If the rval and all lvals are not all of the same length, zip will just ignore
3274            # extra elements, so no error will be raised here; mypy will later complain
3275            # about the length mismatch in type-checking.
3276            elementwise_assignments = zip(rval.items, *[v.items for v in seq_lvals])
3277            for rv, *lvs in elementwise_assignments:
3278                self.process_module_assignment(lvs, rv, ctx)
3279        elif isinstance(rval, RefExpr):
3280            rnode = self.lookup_type_node(rval)
3281            if rnode and isinstance(rnode.node, MypyFile):
3282                for lval in lvals:
3283                    if not isinstance(lval, RefExpr):
3284                        continue
3285                    # respect explicitly annotated type
3286                    if (isinstance(lval.node, Var) and lval.node.type is not None):
3287                        continue
3288
3289                    # We can handle these assignments to locals and to self
3290                    if isinstance(lval, NameExpr):
3291                        lnode = self.current_symbol_table().get(lval.name)
3292                    elif isinstance(lval, MemberExpr) and self.is_self_member_ref(lval):
3293                        assert self.type is not None
3294                        lnode = self.type.names.get(lval.name)
3295                    else:
3296                        continue
3297
3298                    if lnode:
3299                        if isinstance(lnode.node, MypyFile) and lnode.node is not rnode.node:
3300                            assert isinstance(lval, (NameExpr, MemberExpr))
3301                            self.fail(
3302                                'Cannot assign multiple modules to name "{}" '
3303                                'without explicit "types.ModuleType" annotation'.format(lval.name),
3304                                ctx)
3305                        # never create module alias except on initial var definition
3306                        elif lval.is_inferred_def:
3307                            assert rnode.node is not None
3308                            lnode.node = rnode.node
3309
3310    def process__all__(self, s: AssignmentStmt) -> None:
3311        """Export names if argument is a __all__ assignment."""
3312        if (len(s.lvalues) == 1 and isinstance(s.lvalues[0], NameExpr) and
3313                s.lvalues[0].name == '__all__' and s.lvalues[0].kind == GDEF and
3314                isinstance(s.rvalue, (ListExpr, TupleExpr))):
3315            self.add_exports(s.rvalue.items)
3316
3317    #
3318    # Misc statements
3319    #
3320
3321    def visit_block(self, b: Block) -> None:
3322        if b.is_unreachable:
3323            return
3324        self.block_depth[-1] += 1
3325        for s in b.body:
3326            self.accept(s)
3327        self.block_depth[-1] -= 1
3328
3329    def visit_block_maybe(self, b: Optional[Block]) -> None:
3330        if b:
3331            self.visit_block(b)
3332
3333    def visit_expression_stmt(self, s: ExpressionStmt) -> None:
3334        self.statement = s
3335        s.expr.accept(self)
3336
3337    def visit_return_stmt(self, s: ReturnStmt) -> None:
3338        self.statement = s
3339        if not self.is_func_scope():
3340            self.fail('"return" outside function', s)
3341        if s.expr:
3342            s.expr.accept(self)
3343
3344    def visit_raise_stmt(self, s: RaiseStmt) -> None:
3345        self.statement = s
3346        if s.expr:
3347            s.expr.accept(self)
3348        if s.from_expr:
3349            s.from_expr.accept(self)
3350
3351    def visit_assert_stmt(self, s: AssertStmt) -> None:
3352        self.statement = s
3353        if s.expr:
3354            s.expr.accept(self)
3355        if s.msg:
3356            s.msg.accept(self)
3357
3358    def visit_operator_assignment_stmt(self,
3359                                       s: OperatorAssignmentStmt) -> None:
3360        self.statement = s
3361        s.lvalue.accept(self)
3362        s.rvalue.accept(self)
3363        if (isinstance(s.lvalue, NameExpr) and s.lvalue.name == '__all__' and
3364                s.lvalue.kind == GDEF and isinstance(s.rvalue, (ListExpr, TupleExpr))):
3365            self.add_exports(s.rvalue.items)
3366
3367    def visit_while_stmt(self, s: WhileStmt) -> None:
3368        self.statement = s
3369        s.expr.accept(self)
3370        self.loop_depth += 1
3371        s.body.accept(self)
3372        self.loop_depth -= 1
3373        self.visit_block_maybe(s.else_body)
3374
3375    def visit_for_stmt(self, s: ForStmt) -> None:
3376        self.statement = s
3377        s.expr.accept(self)
3378
3379        # Bind index variables and check if they define new names.
3380        self.analyze_lvalue(s.index, explicit_type=s.index_type is not None)
3381        if s.index_type:
3382            if self.is_classvar(s.index_type):
3383                self.fail_invalid_classvar(s.index)
3384            allow_tuple_literal = isinstance(s.index, TupleExpr)
3385            analyzed = self.anal_type(s.index_type, allow_tuple_literal=allow_tuple_literal)
3386            if analyzed is not None:
3387                self.store_declared_types(s.index, analyzed)
3388                s.index_type = analyzed
3389
3390        self.loop_depth += 1
3391        self.visit_block(s.body)
3392        self.loop_depth -= 1
3393
3394        self.visit_block_maybe(s.else_body)
3395
3396    def visit_break_stmt(self, s: BreakStmt) -> None:
3397        self.statement = s
3398        if self.loop_depth == 0:
3399            self.fail('"break" outside loop', s, serious=True, blocker=True)
3400
3401    def visit_continue_stmt(self, s: ContinueStmt) -> None:
3402        self.statement = s
3403        if self.loop_depth == 0:
3404            self.fail('"continue" outside loop', s, serious=True, blocker=True)
3405
3406    def visit_if_stmt(self, s: IfStmt) -> None:
3407        self.statement = s
3408        infer_reachability_of_if_statement(s, self.options)
3409        for i in range(len(s.expr)):
3410            s.expr[i].accept(self)
3411            self.visit_block(s.body[i])
3412        self.visit_block_maybe(s.else_body)
3413
3414    def visit_try_stmt(self, s: TryStmt) -> None:
3415        self.statement = s
3416        self.analyze_try_stmt(s, self)
3417
3418    def analyze_try_stmt(self, s: TryStmt, visitor: NodeVisitor[None]) -> None:
3419        s.body.accept(visitor)
3420        for type, var, handler in zip(s.types, s.vars, s.handlers):
3421            if type:
3422                type.accept(visitor)
3423            if var:
3424                self.analyze_lvalue(var)
3425            handler.accept(visitor)
3426        if s.else_body:
3427            s.else_body.accept(visitor)
3428        if s.finally_body:
3429            s.finally_body.accept(visitor)
3430
3431    def visit_with_stmt(self, s: WithStmt) -> None:
3432        self.statement = s
3433        types = []  # type: List[Type]
3434
3435        if s.unanalyzed_type:
3436            assert isinstance(s.unanalyzed_type, ProperType)
3437            actual_targets = [t for t in s.target if t is not None]
3438            if len(actual_targets) == 0:
3439                # We have a type for no targets
3440                self.fail('Invalid type comment: "with" statement has no targets', s)
3441            elif len(actual_targets) == 1:
3442                # We have one target and one type
3443                types = [s.unanalyzed_type]
3444            elif isinstance(s.unanalyzed_type, TupleType):
3445                # We have multiple targets and multiple types
3446                if len(actual_targets) == len(s.unanalyzed_type.items):
3447                    types = s.unanalyzed_type.items.copy()
3448                else:
3449                    # But it's the wrong number of items
3450                    self.fail('Incompatible number of types for "with" targets', s)
3451            else:
3452                # We have multiple targets and one type
3453                self.fail('Multiple types expected for multiple "with" targets', s)
3454
3455        new_types = []  # type: List[Type]
3456        for e, n in zip(s.expr, s.target):
3457            e.accept(self)
3458            if n:
3459                self.analyze_lvalue(n, explicit_type=s.unanalyzed_type is not None)
3460
3461                # Since we have a target, pop the next type from types
3462                if types:
3463                    t = types.pop(0)
3464                    if self.is_classvar(t):
3465                        self.fail_invalid_classvar(n)
3466                    allow_tuple_literal = isinstance(n, TupleExpr)
3467                    analyzed = self.anal_type(t, allow_tuple_literal=allow_tuple_literal)
3468                    if analyzed is not None:
3469                        # TODO: Deal with this better
3470                        new_types.append(analyzed)
3471                        self.store_declared_types(n, analyzed)
3472
3473        s.analyzed_types = new_types
3474
3475        self.visit_block(s.body)
3476
3477    def visit_del_stmt(self, s: DelStmt) -> None:
3478        self.statement = s
3479        s.expr.accept(self)
3480        if not self.is_valid_del_target(s.expr):
3481            self.fail('Invalid delete target', s)
3482
3483    def is_valid_del_target(self, s: Expression) -> bool:
3484        if isinstance(s, (IndexExpr, NameExpr, MemberExpr)):
3485            return True
3486        elif isinstance(s, (TupleExpr, ListExpr)):
3487            return all(self.is_valid_del_target(item) for item in s.items)
3488        else:
3489            return False
3490
3491    def visit_global_decl(self, g: GlobalDecl) -> None:
3492        self.statement = g
3493        for name in g.names:
3494            if name in self.nonlocal_decls[-1]:
3495                self.fail('Name "{}" is nonlocal and global'.format(name), g)
3496            self.global_decls[-1].add(name)
3497
3498    def visit_nonlocal_decl(self, d: NonlocalDecl) -> None:
3499        self.statement = d
3500        if not self.is_func_scope():
3501            self.fail("nonlocal declaration not allowed at module level", d)
3502        else:
3503            for name in d.names:
3504                for table in reversed(self.locals[:-1]):
3505                    if table is not None and name in table:
3506                        break
3507                else:
3508                    self.fail('No binding for nonlocal "{}" found'.format(name), d)
3509
3510                if self.locals[-1] is not None and name in self.locals[-1]:
3511                    self.fail('Name "{}" is already defined in local '
3512                              'scope before nonlocal declaration'.format(name), d)
3513
3514                if name in self.global_decls[-1]:
3515                    self.fail('Name "{}" is nonlocal and global'.format(name), d)
3516                self.nonlocal_decls[-1].add(name)
3517
3518    def visit_print_stmt(self, s: PrintStmt) -> None:
3519        self.statement = s
3520        for arg in s.args:
3521            arg.accept(self)
3522        if s.target:
3523            s.target.accept(self)
3524
3525    def visit_exec_stmt(self, s: ExecStmt) -> None:
3526        self.statement = s
3527        s.expr.accept(self)
3528        if s.globals:
3529            s.globals.accept(self)
3530        if s.locals:
3531            s.locals.accept(self)
3532
3533    #
3534    # Expressions
3535    #
3536
3537    def visit_name_expr(self, expr: NameExpr) -> None:
3538        n = self.lookup(expr.name, expr)
3539        if n:
3540            self.bind_name_expr(expr, n)
3541
3542    def bind_name_expr(self, expr: NameExpr, sym: SymbolTableNode) -> None:
3543        """Bind name expression to a symbol table node."""
3544        if isinstance(sym.node, TypeVarExpr) and self.tvar_scope.get_binding(sym):
3545            self.fail('"{}" is a type variable and only valid in type '
3546                      'context'.format(expr.name), expr)
3547        elif isinstance(sym.node, PlaceholderNode):
3548            self.process_placeholder(expr.name, 'name', expr)
3549        else:
3550            expr.kind = sym.kind
3551            expr.node = sym.node
3552            expr.fullname = sym.fullname
3553
3554    def visit_super_expr(self, expr: SuperExpr) -> None:
3555        if not self.type and not expr.call.args:
3556            self.fail('"super" used outside class', expr)
3557            return
3558        expr.info = self.type
3559        for arg in expr.call.args:
3560            arg.accept(self)
3561
3562    def visit_tuple_expr(self, expr: TupleExpr) -> None:
3563        for item in expr.items:
3564            if isinstance(item, StarExpr):
3565                item.valid = True
3566            item.accept(self)
3567
3568    def visit_list_expr(self, expr: ListExpr) -> None:
3569        for item in expr.items:
3570            if isinstance(item, StarExpr):
3571                item.valid = True
3572            item.accept(self)
3573
3574    def visit_set_expr(self, expr: SetExpr) -> None:
3575        for item in expr.items:
3576            if isinstance(item, StarExpr):
3577                item.valid = True
3578            item.accept(self)
3579
3580    def visit_dict_expr(self, expr: DictExpr) -> None:
3581        for key, value in expr.items:
3582            if key is not None:
3583                key.accept(self)
3584            value.accept(self)
3585
3586    def visit_star_expr(self, expr: StarExpr) -> None:
3587        if not expr.valid:
3588            # XXX TODO Change this error message
3589            self.fail('Can use starred expression only as assignment target', expr)
3590        else:
3591            expr.expr.accept(self)
3592
3593    def visit_yield_from_expr(self, e: YieldFromExpr) -> None:
3594        if not self.is_func_scope():  # not sure
3595            self.fail('"yield from" outside function', e, serious=True, blocker=True)
3596        else:
3597            if self.function_stack[-1].is_coroutine:
3598                self.fail('"yield from" in async function', e, serious=True, blocker=True)
3599            else:
3600                self.function_stack[-1].is_generator = True
3601        if e.expr:
3602            e.expr.accept(self)
3603
3604    def visit_call_expr(self, expr: CallExpr) -> None:
3605        """Analyze a call expression.
3606
3607        Some call expressions are recognized as special forms, including
3608        cast(...).
3609        """
3610        expr.callee.accept(self)
3611        if refers_to_fullname(expr.callee, 'typing.cast'):
3612            # Special form cast(...).
3613            if not self.check_fixed_args(expr, 2, 'cast'):
3614                return
3615            # Translate first argument to an unanalyzed type.
3616            try:
3617                target = expr_to_unanalyzed_type(expr.args[0])
3618            except TypeTranslationError:
3619                self.fail('Cast target is not a type', expr)
3620                return
3621            # Piggyback CastExpr object to the CallExpr object; it takes
3622            # precedence over the CallExpr semantics.
3623            expr.analyzed = CastExpr(expr.args[1], target)
3624            expr.analyzed.line = expr.line
3625            expr.analyzed.column = expr.column
3626            expr.analyzed.accept(self)
3627        elif refers_to_fullname(expr.callee, 'builtins.reveal_type'):
3628            if not self.check_fixed_args(expr, 1, 'reveal_type'):
3629                return
3630            expr.analyzed = RevealExpr(kind=REVEAL_TYPE, expr=expr.args[0])
3631            expr.analyzed.line = expr.line
3632            expr.analyzed.column = expr.column
3633            expr.analyzed.accept(self)
3634        elif refers_to_fullname(expr.callee, 'builtins.reveal_locals'):
3635            # Store the local variable names into the RevealExpr for use in the
3636            # type checking pass
3637            local_nodes = []  # type: List[Var]
3638            if self.is_module_scope():
3639                # try to determine just the variable declarations in module scope
3640                # self.globals.values() contains SymbolTableNode's
3641                # Each SymbolTableNode has an attribute node that is nodes.Var
3642                # look for variable nodes that marked as is_inferred
3643                # Each symboltable node has a Var node as .node
3644                local_nodes = [n.node
3645                               for name, n in self.globals.items()
3646                               if getattr(n.node, 'is_inferred', False)
3647                               and isinstance(n.node, Var)]
3648            elif self.is_class_scope():
3649                # type = None  # type: Optional[TypeInfo]
3650                if self.type is not None:
3651                    local_nodes = [st.node
3652                                   for st in self.type.names.values()
3653                                   if isinstance(st.node, Var)]
3654            elif self.is_func_scope():
3655                # locals = None  # type: List[Optional[SymbolTable]]
3656                if self.locals is not None:
3657                    symbol_table = self.locals[-1]
3658                    if symbol_table is not None:
3659                        local_nodes = [st.node
3660                                       for st in symbol_table.values()
3661                                       if isinstance(st.node, Var)]
3662            expr.analyzed = RevealExpr(kind=REVEAL_LOCALS, local_nodes=local_nodes)
3663            expr.analyzed.line = expr.line
3664            expr.analyzed.column = expr.column
3665            expr.analyzed.accept(self)
3666        elif refers_to_fullname(expr.callee, 'typing.Any'):
3667            # Special form Any(...) no longer supported.
3668            self.fail('Any(...) is no longer supported. Use cast(Any, ...) instead', expr)
3669        elif refers_to_fullname(expr.callee, 'typing._promote'):
3670            # Special form _promote(...).
3671            if not self.check_fixed_args(expr, 1, '_promote'):
3672                return
3673            # Translate first argument to an unanalyzed type.
3674            try:
3675                target = expr_to_unanalyzed_type(expr.args[0])
3676            except TypeTranslationError:
3677                self.fail('Argument 1 to _promote is not a type', expr)
3678                return
3679            expr.analyzed = PromoteExpr(target)
3680            expr.analyzed.line = expr.line
3681            expr.analyzed.accept(self)
3682        elif refers_to_fullname(expr.callee, 'builtins.dict'):
3683            expr.analyzed = self.translate_dict_call(expr)
3684        elif refers_to_fullname(expr.callee, 'builtins.divmod'):
3685            if not self.check_fixed_args(expr, 2, 'divmod'):
3686                return
3687            expr.analyzed = OpExpr('divmod', expr.args[0], expr.args[1])
3688            expr.analyzed.line = expr.line
3689            expr.analyzed.accept(self)
3690        else:
3691            # Normal call expression.
3692            for a in expr.args:
3693                a.accept(self)
3694
3695            if (isinstance(expr.callee, MemberExpr) and
3696                    isinstance(expr.callee.expr, NameExpr) and
3697                    expr.callee.expr.name == '__all__' and
3698                    expr.callee.expr.kind == GDEF and
3699                    expr.callee.name in ('append', 'extend')):
3700                if expr.callee.name == 'append' and expr.args:
3701                    self.add_exports(expr.args[0])
3702                elif (expr.callee.name == 'extend' and expr.args and
3703                        isinstance(expr.args[0], (ListExpr, TupleExpr))):
3704                    self.add_exports(expr.args[0].items)
3705
3706    def translate_dict_call(self, call: CallExpr) -> Optional[DictExpr]:
3707        """Translate 'dict(x=y, ...)' to {'x': y, ...} and 'dict()' to {}.
3708
3709        For other variants of dict(...), return None.
3710        """
3711        if not all(kind == ARG_NAMED for kind in call.arg_kinds):
3712            # Must still accept those args.
3713            for a in call.args:
3714                a.accept(self)
3715            return None
3716        expr = DictExpr([(StrExpr(cast(str, key)), value)  # since they are all ARG_NAMED
3717                         for key, value in zip(call.arg_names, call.args)])
3718        expr.set_line(call)
3719        expr.accept(self)
3720        return expr
3721
3722    def check_fixed_args(self, expr: CallExpr, numargs: int,
3723                         name: str) -> bool:
3724        """Verify that expr has specified number of positional args.
3725
3726        Return True if the arguments are valid.
3727        """
3728        s = 's'
3729        if numargs == 1:
3730            s = ''
3731        if len(expr.args) != numargs:
3732            self.fail('"%s" expects %d argument%s' % (name, numargs, s),
3733                      expr)
3734            return False
3735        if expr.arg_kinds != [ARG_POS] * numargs:
3736            self.fail('"%s" must be called with %s positional argument%s' %
3737                      (name, numargs, s), expr)
3738            return False
3739        return True
3740
3741    def visit_member_expr(self, expr: MemberExpr) -> None:
3742        base = expr.expr
3743        base.accept(self)
3744        if isinstance(base, RefExpr) and isinstance(base.node, MypyFile):
3745            # Handle module attribute.
3746            sym = self.get_module_symbol(base.node, expr.name)
3747            if sym:
3748                if isinstance(sym.node, PlaceholderNode):
3749                    self.process_placeholder(expr.name, 'attribute', expr)
3750                    return
3751                expr.kind = sym.kind
3752                expr.fullname = sym.fullname
3753                expr.node = sym.node
3754        elif isinstance(base, RefExpr):
3755            # This branch handles the case C.bar (or cls.bar or self.bar inside
3756            # a classmethod/method), where C is a class and bar is a type
3757            # definition or a module resulting from `import bar` (or a module
3758            # assignment) inside class C. We look up bar in the class' TypeInfo
3759            # namespace.  This is done only when bar is a module or a type;
3760            # other things (e.g. methods) are handled by other code in
3761            # checkmember.
3762            type_info = None
3763            if isinstance(base.node, TypeInfo):
3764                # C.bar where C is a class
3765                type_info = base.node
3766            elif isinstance(base.node, Var) and self.type and self.function_stack:
3767                # check for self.bar or cls.bar in method/classmethod
3768                func_def = self.function_stack[-1]
3769                if not func_def.is_static and isinstance(func_def.type, CallableType):
3770                    formal_arg = func_def.type.argument_by_name(base.node.name)
3771                    if formal_arg and formal_arg.pos == 0:
3772                        type_info = self.type
3773            elif isinstance(base.node, TypeAlias) and base.node.no_args:
3774                assert isinstance(base.node.target, ProperType)
3775                if isinstance(base.node.target, Instance):
3776                    type_info = base.node.target.type
3777
3778            if type_info:
3779                n = type_info.names.get(expr.name)
3780                if n is not None and isinstance(n.node, (MypyFile, TypeInfo, TypeAlias)):
3781                    if not n:
3782                        return
3783                    expr.kind = n.kind
3784                    expr.fullname = n.fullname
3785                    expr.node = n.node
3786
3787    def visit_op_expr(self, expr: OpExpr) -> None:
3788        expr.left.accept(self)
3789
3790        if expr.op in ('and', 'or'):
3791            inferred = infer_condition_value(expr.left, self.options)
3792            if ((inferred in (ALWAYS_FALSE, MYPY_FALSE) and expr.op == 'and') or
3793                    (inferred in (ALWAYS_TRUE, MYPY_TRUE) and expr.op == 'or')):
3794                expr.right_unreachable = True
3795                return
3796            elif ((inferred in (ALWAYS_TRUE, MYPY_TRUE) and expr.op == 'and') or
3797                    (inferred in (ALWAYS_FALSE, MYPY_FALSE) and expr.op == 'or')):
3798                expr.right_always = True
3799
3800        expr.right.accept(self)
3801
3802    def visit_comparison_expr(self, expr: ComparisonExpr) -> None:
3803        for operand in expr.operands:
3804            operand.accept(self)
3805
3806    def visit_unary_expr(self, expr: UnaryExpr) -> None:
3807        expr.expr.accept(self)
3808
3809    def visit_index_expr(self, expr: IndexExpr) -> None:
3810        base = expr.base
3811        base.accept(self)
3812        if (isinstance(base, RefExpr)
3813                and isinstance(base.node, TypeInfo)
3814                and not base.node.is_generic()):
3815            expr.index.accept(self)
3816        elif ((isinstance(base, RefExpr) and isinstance(base.node, TypeAlias))
3817              or refers_to_class_or_function(base)):
3818            # We need to do full processing on every iteration, since some type
3819            # arguments may contain placeholder types.
3820            self.analyze_type_application(expr)
3821        else:
3822            expr.index.accept(self)
3823
3824    def analyze_type_application(self, expr: IndexExpr) -> None:
3825        """Analyze special form -- type application (either direct or via type aliasing)."""
3826        types = self.analyze_type_application_args(expr)
3827        if types is None:
3828            return
3829        base = expr.base
3830        expr.analyzed = TypeApplication(base, types)
3831        expr.analyzed.line = expr.line
3832        expr.analyzed.column = expr.column
3833        # Types list, dict, set are not subscriptable, prohibit this if
3834        # subscripted either via type alias...
3835        if isinstance(base, RefExpr) and isinstance(base.node, TypeAlias):
3836            alias = base.node
3837            target = get_proper_type(alias.target)
3838            if isinstance(target, Instance):
3839                name = target.type.fullname
3840                if (alias.no_args and  # this avoids bogus errors for already reported aliases
3841                        name in get_nongen_builtins(self.options.python_version) and
3842                        not alias.normalized):
3843                    self.fail(no_subscript_builtin_alias(name, propose_alt=False), expr)
3844        # ...or directly.
3845        else:
3846            n = self.lookup_type_node(base)
3847            if n and n.fullname in get_nongen_builtins(self.options.python_version):
3848                self.fail(no_subscript_builtin_alias(n.fullname, propose_alt=False), expr)
3849
3850    def analyze_type_application_args(self, expr: IndexExpr) -> Optional[List[Type]]:
3851        """Analyze type arguments (index) in a type application.
3852
3853        Return None if anything was incomplete.
3854        """
3855        index = expr.index
3856        tag = self.track_incomplete_refs()
3857        self.analyze_type_expr(index)
3858        if self.found_incomplete_ref(tag):
3859            return None
3860        types = []  # type: List[Type]
3861        if isinstance(index, TupleExpr):
3862            items = index.items
3863        else:
3864            items = [index]
3865        for item in items:
3866            try:
3867                typearg = expr_to_unanalyzed_type(item)
3868            except TypeTranslationError:
3869                self.fail('Type expected within [...]', expr)
3870                return None
3871            # We always allow unbound type variables in IndexExpr, since we
3872            # may be analysing a type alias definition rvalue. The error will be
3873            # reported elsewhere if it is not the case.
3874            analyzed = self.anal_type(typearg, allow_unbound_tvars=True,
3875                                      allow_placeholder=True)
3876            if analyzed is None:
3877                return None
3878            types.append(analyzed)
3879        return types
3880
3881    def visit_slice_expr(self, expr: SliceExpr) -> None:
3882        if expr.begin_index:
3883            expr.begin_index.accept(self)
3884        if expr.end_index:
3885            expr.end_index.accept(self)
3886        if expr.stride:
3887            expr.stride.accept(self)
3888
3889    def visit_cast_expr(self, expr: CastExpr) -> None:
3890        expr.expr.accept(self)
3891        analyzed = self.anal_type(expr.type)
3892        if analyzed is not None:
3893            expr.type = analyzed
3894
3895    def visit_reveal_expr(self, expr: RevealExpr) -> None:
3896        if expr.kind == REVEAL_TYPE:
3897            if expr.expr is not None:
3898                expr.expr.accept(self)
3899        else:
3900            # Reveal locals doesn't have an inner expression, there's no
3901            # need to traverse inside it
3902            pass
3903
3904    def visit_type_application(self, expr: TypeApplication) -> None:
3905        expr.expr.accept(self)
3906        for i in range(len(expr.types)):
3907            analyzed = self.anal_type(expr.types[i])
3908            if analyzed is not None:
3909                expr.types[i] = analyzed
3910
3911    def visit_list_comprehension(self, expr: ListComprehension) -> None:
3912        expr.generator.accept(self)
3913
3914    def visit_set_comprehension(self, expr: SetComprehension) -> None:
3915        expr.generator.accept(self)
3916
3917    def visit_dictionary_comprehension(self, expr: DictionaryComprehension) -> None:
3918        self.enter(expr)
3919        self.analyze_comp_for(expr)
3920        expr.key.accept(self)
3921        expr.value.accept(self)
3922        self.leave()
3923        self.analyze_comp_for_2(expr)
3924
3925    def visit_generator_expr(self, expr: GeneratorExpr) -> None:
3926        self.enter(expr)
3927        self.analyze_comp_for(expr)
3928        expr.left_expr.accept(self)
3929        self.leave()
3930        self.analyze_comp_for_2(expr)
3931
3932    def analyze_comp_for(self, expr: Union[GeneratorExpr,
3933                                           DictionaryComprehension]) -> None:
3934        """Analyses the 'comp_for' part of comprehensions (part 1).
3935
3936        That is the part after 'for' in (x for x in l if p). This analyzes
3937        variables and conditions which are analyzed in a local scope.
3938        """
3939        for i, (index, sequence, conditions) in enumerate(zip(expr.indices,
3940                                                              expr.sequences,
3941                                                              expr.condlists)):
3942            if i > 0:
3943                sequence.accept(self)
3944            # Bind index variables.
3945            self.analyze_lvalue(index)
3946            for cond in conditions:
3947                cond.accept(self)
3948
3949    def analyze_comp_for_2(self, expr: Union[GeneratorExpr,
3950                                             DictionaryComprehension]) -> None:
3951        """Analyses the 'comp_for' part of comprehensions (part 2).
3952
3953        That is the part after 'for' in (x for x in l if p). This analyzes
3954        the 'l' part which is analyzed in the surrounding scope.
3955        """
3956        expr.sequences[0].accept(self)
3957
3958    def visit_lambda_expr(self, expr: LambdaExpr) -> None:
3959        self.analyze_arg_initializers(expr)
3960        self.analyze_function_body(expr)
3961
3962    def visit_conditional_expr(self, expr: ConditionalExpr) -> None:
3963        expr.if_expr.accept(self)
3964        expr.cond.accept(self)
3965        expr.else_expr.accept(self)
3966
3967    def visit_backquote_expr(self, expr: BackquoteExpr) -> None:
3968        expr.expr.accept(self)
3969
3970    def visit__promote_expr(self, expr: PromoteExpr) -> None:
3971        analyzed = self.anal_type(expr.type)
3972        if analyzed is not None:
3973            expr.type = analyzed
3974
3975    def visit_yield_expr(self, expr: YieldExpr) -> None:
3976        if not self.is_func_scope():
3977            self.fail('"yield" outside function', expr, serious=True, blocker=True)
3978        else:
3979            if self.function_stack[-1].is_coroutine:
3980                if self.options.python_version < (3, 6):
3981                    self.fail('"yield" in async function', expr, serious=True, blocker=True)
3982                else:
3983                    self.function_stack[-1].is_generator = True
3984                    self.function_stack[-1].is_async_generator = True
3985            else:
3986                self.function_stack[-1].is_generator = True
3987        if expr.expr:
3988            expr.expr.accept(self)
3989
3990    def visit_await_expr(self, expr: AwaitExpr) -> None:
3991        if not self.is_func_scope():
3992            self.fail('"await" outside function', expr)
3993        elif not self.function_stack[-1].is_coroutine:
3994            self.fail('"await" outside coroutine ("async def")', expr)
3995        expr.expr.accept(self)
3996
3997    #
3998    # Lookup functions
3999    #
4000
4001    def lookup(self, name: str, ctx: Context,
4002               suppress_errors: bool = False) -> Optional[SymbolTableNode]:
4003        """Look up an unqualified (no dots) name in all active namespaces.
4004
4005        Note that the result may contain a PlaceholderNode. The caller may
4006        want to defer in that case.
4007
4008        Generate an error if the name is not defined unless suppress_errors
4009        is true or the current namespace is incomplete. In the latter case
4010        defer.
4011        """
4012        implicit_name = False
4013        # 1a. Name declared using 'global x' takes precedence
4014        if name in self.global_decls[-1]:
4015            if name in self.globals:
4016                return self.globals[name]
4017            if not suppress_errors:
4018                self.name_not_defined(name, ctx)
4019            return None
4020        # 1b. Name declared using 'nonlocal x' takes precedence
4021        if name in self.nonlocal_decls[-1]:
4022            for table in reversed(self.locals[:-1]):
4023                if table is not None and name in table:
4024                    return table[name]
4025            else:
4026                if not suppress_errors:
4027                    self.name_not_defined(name, ctx)
4028                return None
4029        # 2. Class attributes (if within class definition)
4030        if self.type and not self.is_func_scope() and name in self.type.names:
4031            node = self.type.names[name]
4032            if not node.implicit:
4033                if self.is_active_symbol_in_class_body(node.node):
4034                    return node
4035            else:
4036                # Defined through self.x assignment
4037                implicit_name = True
4038                implicit_node = node
4039        # 3. Local (function) scopes
4040        for table in reversed(self.locals):
4041            if table is not None and name in table:
4042                return table[name]
4043        # 4. Current file global scope
4044        if name in self.globals:
4045            return self.globals[name]
4046        # 5. Builtins
4047        b = self.globals.get('__builtins__', None)
4048        if b:
4049            assert isinstance(b.node, MypyFile)
4050            table = b.node.names
4051            if name in table:
4052                if name[0] == "_" and name[1] != "_":
4053                    if not suppress_errors:
4054                        self.name_not_defined(name, ctx)
4055                    return None
4056                node = table[name]
4057                return node
4058        # Give up.
4059        if not implicit_name and not suppress_errors:
4060            self.name_not_defined(name, ctx)
4061        else:
4062            if implicit_name:
4063                return implicit_node
4064        return None
4065
4066    def is_active_symbol_in_class_body(self, node: Optional[SymbolNode]) -> bool:
4067        """Can a symbol defined in class body accessed at current statement?
4068
4069        Only allow access to class attributes textually after
4070        the definition, so that it's possible to fall back to the
4071        outer scope. Example:
4072
4073            class X: ...
4074
4075            class C:
4076                X = X  # Initializer refers to outer scope
4077
4078        Nested classes are an exception, since we want to support
4079        arbitrary forward references in type annotations.
4080        """
4081        # TODO: Forward reference to name imported in class body is not
4082        #       caught.
4083        assert self.statement  # we are at class scope
4084        return (node is None
4085                or self.is_textually_before_statement(node)
4086                or not self.is_defined_in_current_module(node.fullname)
4087                or isinstance(node, TypeInfo)
4088                or (isinstance(node, PlaceholderNode) and node.becomes_typeinfo))
4089
4090    def is_textually_before_statement(self, node: SymbolNode) -> bool:
4091        """Check if a node is defined textually before the current statement
4092
4093        Note that decorated functions' line number are the same as
4094        the top decorator.
4095        """
4096        assert self.statement
4097        line_diff = self.statement.line - node.line
4098
4099        # The first branch handles reference an overloaded function variant inside itself,
4100        # this is a corner case where mypy technically deviates from runtime name resolution,
4101        # but it is fine because we want an overloaded function to be treated as a single unit.
4102        if self.is_overloaded_item(node, self.statement):
4103            return False
4104        elif isinstance(node, Decorator) and not node.is_overload:
4105            return line_diff > len(node.original_decorators)
4106        else:
4107            return line_diff > 0
4108
4109    def is_overloaded_item(self, node: SymbolNode, statement: Statement) -> bool:
4110        """Check whehter the function belongs to the overloaded variants"""
4111        if isinstance(node, OverloadedFuncDef) and isinstance(statement, FuncDef):
4112            in_items = statement in {item.func if isinstance(item, Decorator)
4113                                     else item for item in node.items}
4114            in_impl = (node.impl is not None and
4115                      ((isinstance(node.impl, Decorator) and statement is node.impl.func)
4116                       or statement is node.impl))
4117            return in_items or in_impl
4118        return False
4119
4120    def is_defined_in_current_module(self, fullname: Optional[str]) -> bool:
4121        if fullname is None:
4122            return False
4123        return module_prefix(self.modules, fullname) == self.cur_mod_id
4124
4125    def lookup_qualified(self, name: str, ctx: Context,
4126                         suppress_errors: bool = False) -> Optional[SymbolTableNode]:
4127        """Lookup a qualified name in all activate namespaces.
4128
4129        Note that the result may contain a PlaceholderNode. The caller may
4130        want to defer in that case.
4131
4132        Generate an error if the name is not defined unless suppress_errors
4133        is true or the current namespace is incomplete. In the latter case
4134        defer.
4135        """
4136        if '.' not in name:
4137            # Simple case: look up a short name.
4138            return self.lookup(name, ctx, suppress_errors=suppress_errors)
4139        parts = name.split('.')
4140        namespace = self.cur_mod_id
4141        sym = self.lookup(parts[0], ctx, suppress_errors=suppress_errors)
4142        if sym:
4143            for i in range(1, len(parts)):
4144                node = sym.node
4145                part = parts[i]
4146                if isinstance(node, TypeInfo):
4147                    nextsym = node.get(part)
4148                elif isinstance(node, MypyFile):
4149                    nextsym = self.get_module_symbol(node, part)
4150                    namespace = node.fullname
4151                elif isinstance(node, PlaceholderNode):
4152                    return sym
4153                elif isinstance(node, TypeAlias) and node.no_args:
4154                    assert isinstance(node.target, ProperType)
4155                    if isinstance(node.target, Instance):
4156                        nextsym = node.target.type.get(part)
4157                else:
4158                    if isinstance(node, Var):
4159                        typ = get_proper_type(node.type)
4160                        if isinstance(typ, AnyType):
4161                            # Allow access through Var with Any type without error.
4162                            return self.implicit_symbol(sym, name, parts[i:], typ)
4163                    # Lookup through invalid node, such as variable or function
4164                    nextsym = None
4165                if not nextsym or nextsym.module_hidden:
4166                    if not suppress_errors:
4167                        self.name_not_defined(name, ctx, namespace=namespace)
4168                    return None
4169                sym = nextsym
4170        return sym
4171
4172    def lookup_type_node(self, expr: Expression) -> Optional[SymbolTableNode]:
4173        try:
4174            t = expr_to_unanalyzed_type(expr)
4175        except TypeTranslationError:
4176            return None
4177        if isinstance(t, UnboundType):
4178            n = self.lookup_qualified(t.name, expr, suppress_errors=True)
4179            return n
4180        return None
4181
4182    def get_module_symbol(self, node: MypyFile, name: str) -> Optional[SymbolTableNode]:
4183        """Look up a symbol from a module.
4184
4185        Return None if no matching symbol could be bound.
4186        """
4187        module = node.fullname
4188        names = node.names
4189        sym = names.get(name)
4190        if not sym:
4191            fullname = module + '.' + name
4192            if fullname in self.modules:
4193                sym = SymbolTableNode(GDEF, self.modules[fullname])
4194            elif self.is_incomplete_namespace(module):
4195                self.record_incomplete_ref()
4196            elif ('__getattr__' in names
4197                    and (node.is_stub
4198                         or self.options.python_version >= (3, 7))):
4199                gvar = self.create_getattr_var(names['__getattr__'], name, fullname)
4200                if gvar:
4201                    sym = SymbolTableNode(GDEF, gvar)
4202            elif self.is_missing_module(fullname):
4203                # We use the fullname of the original definition so that we can
4204                # detect whether two names refer to the same thing.
4205                var_type = AnyType(TypeOfAny.from_unimported_type)
4206                v = Var(name, type=var_type)
4207                v._fullname = fullname
4208                sym = SymbolTableNode(GDEF, v)
4209        elif sym.module_hidden:
4210            sym = None
4211        return sym
4212
4213    def is_missing_module(self, module: str) -> bool:
4214        return module in self.missing_modules
4215
4216    def implicit_symbol(self, sym: SymbolTableNode, name: str, parts: List[str],
4217                        source_type: AnyType) -> SymbolTableNode:
4218        """Create symbol for a qualified name reference through Any type."""
4219        if sym.node is None:
4220            basename = None
4221        else:
4222            basename = sym.node.fullname
4223        if basename is None:
4224            fullname = name
4225        else:
4226            fullname = basename + '.' + '.'.join(parts)
4227        var_type = AnyType(TypeOfAny.from_another_any, source_type)
4228        var = Var(parts[-1], var_type)
4229        var._fullname = fullname
4230        return SymbolTableNode(GDEF, var)
4231
4232    def create_getattr_var(self, getattr_defn: SymbolTableNode,
4233                           name: str, fullname: str) -> Optional[Var]:
4234        """Create a dummy variable using module-level __getattr__ return type.
4235
4236        If not possible, return None.
4237
4238        Note that multiple Var nodes can be created for a single name. We
4239        can use the from_module_getattr and the fullname attributes to
4240        check if two dummy Var nodes refer to the same thing. Reusing Var
4241        nodes would require non-local mutable state, which we prefer to
4242        avoid.
4243        """
4244        if isinstance(getattr_defn.node, (FuncDef, Var)):
4245            node_type = get_proper_type(getattr_defn.node.type)
4246            if isinstance(node_type, CallableType):
4247                typ = node_type.ret_type
4248            else:
4249                typ = AnyType(TypeOfAny.from_error)
4250            v = Var(name, type=typ)
4251            v._fullname = fullname
4252            v.from_module_getattr = True
4253            return v
4254        return None
4255
4256    def lookup_fully_qualified(self, name: str) -> SymbolTableNode:
4257        """Lookup a fully qualified name.
4258
4259        Assume that the name is defined. This happens in the global namespace --
4260        the local module namespace is ignored.
4261
4262        Note that this doesn't support visibility, module-level __getattr__, or
4263        nested classes.
4264        """
4265        parts = name.split('.')
4266        n = self.modules[parts[0]]
4267        for i in range(1, len(parts) - 1):
4268            next_sym = n.names[parts[i]]
4269            assert isinstance(next_sym.node, MypyFile)
4270            n = next_sym.node
4271        return n.names[parts[-1]]
4272
4273    def lookup_fully_qualified_or_none(self, fullname: str) -> Optional[SymbolTableNode]:
4274        """Lookup a fully qualified name that refers to a module-level definition.
4275
4276        Don't assume that the name is defined. This happens in the global namespace --
4277        the local module namespace is ignored. This does not dereference indirect
4278        refs.
4279
4280        Note that this can't be used for names nested in class namespaces.
4281        """
4282        # TODO: unify/clean-up/simplify lookup methods, see #4157.
4283        # TODO: support nested classes (but consider performance impact,
4284        #       we might keep the module level only lookup for thing like 'builtins.int').
4285        assert '.' in fullname
4286        module, name = fullname.rsplit('.', maxsplit=1)
4287        if module not in self.modules:
4288            return None
4289        filenode = self.modules[module]
4290        result = filenode.names.get(name)
4291        if result is None and self.is_incomplete_namespace(module):
4292            # TODO: More explicit handling of incomplete refs?
4293            self.record_incomplete_ref()
4294        return result
4295
4296    def builtin_type(self, fully_qualified_name: str) -> Instance:
4297        sym = self.lookup_fully_qualified(fully_qualified_name)
4298        node = sym.node
4299        assert isinstance(node, TypeInfo)
4300        return Instance(node, [AnyType(TypeOfAny.special_form)] * len(node.defn.type_vars))
4301
4302    def object_type(self) -> Instance:
4303        return self.named_type('__builtins__.object')
4304
4305    def str_type(self) -> Instance:
4306        return self.named_type('__builtins__.str')
4307
4308    def named_type(self, qualified_name: str, args: Optional[List[Type]] = None) -> Instance:
4309        sym = self.lookup_qualified(qualified_name, Context())
4310        assert sym, "Internal error: attempted to construct unknown type"
4311        node = sym.node
4312        assert isinstance(node, TypeInfo)
4313        if args:
4314            # TODO: assert len(args) == len(node.defn.type_vars)
4315            return Instance(node, args)
4316        return Instance(node, [AnyType(TypeOfAny.special_form)] * len(node.defn.type_vars))
4317
4318    def named_type_or_none(self, qualified_name: str,
4319                           args: Optional[List[Type]] = None) -> Optional[Instance]:
4320        sym = self.lookup_fully_qualified_or_none(qualified_name)
4321        if not sym or isinstance(sym.node, PlaceholderNode):
4322            return None
4323        node = sym.node
4324        if isinstance(node, TypeAlias):
4325            assert isinstance(node.target, Instance)  # type: ignore
4326            node = node.target.type
4327        assert isinstance(node, TypeInfo), node
4328        if args is not None:
4329            # TODO: assert len(args) == len(node.defn.type_vars)
4330            return Instance(node, args)
4331        return Instance(node, [AnyType(TypeOfAny.unannotated)] * len(node.defn.type_vars))
4332
4333    def lookup_current_scope(self, name: str) -> Optional[SymbolTableNode]:
4334        if self.locals[-1] is not None:
4335            return self.locals[-1].get(name)
4336        elif self.type is not None:
4337            return self.type.names.get(name)
4338        else:
4339            return self.globals.get(name)
4340
4341    #
4342    # Adding symbols
4343    #
4344
4345    def add_symbol(self,
4346                   name: str,
4347                   node: SymbolNode,
4348                   context: Context,
4349                   module_public: bool = True,
4350                   module_hidden: bool = False,
4351                   can_defer: bool = True,
4352                   escape_comprehensions: bool = False) -> bool:
4353        """Add symbol to the currently active symbol table.
4354
4355        Generally additions to symbol table should go through this method or
4356        one of the methods below so that kinds, redefinitions, conditional
4357        definitions, and skipped names are handled consistently.
4358
4359        Return True if we actually added the symbol, or False if we refused to do so
4360        (because something is not ready).
4361
4362        If can_defer is True, defer current target if adding a placeholder.
4363        """
4364        if self.is_func_scope():
4365            kind = LDEF
4366        elif self.type is not None:
4367            kind = MDEF
4368        else:
4369            kind = GDEF
4370        symbol = SymbolTableNode(kind,
4371                                 node,
4372                                 module_public=module_public,
4373                                 module_hidden=module_hidden)
4374        return self.add_symbol_table_node(name, symbol, context, can_defer, escape_comprehensions)
4375
4376    def add_symbol_skip_local(self, name: str, node: SymbolNode) -> None:
4377        """Same as above, but skipping the local namespace.
4378
4379        This doesn't check for previous definition and is only used
4380        for serialization of method-level classes.
4381
4382        Classes defined within methods can be exposed through an
4383        attribute type, but method-level symbol tables aren't serialized.
4384        This method can be used to add such classes to an enclosing,
4385        serialized symbol table.
4386        """
4387        # TODO: currently this is only used by named tuples. Use this method
4388        # also by typed dicts and normal classes, see issue #6422.
4389        if self.type is not None:
4390            names = self.type.names
4391            kind = MDEF
4392        else:
4393            names = self.globals
4394            kind = GDEF
4395        symbol = SymbolTableNode(kind, node)
4396        names[name] = symbol
4397
4398    def add_symbol_table_node(self,
4399                              name: str,
4400                              symbol: SymbolTableNode,
4401                              context: Optional[Context] = None,
4402                              can_defer: bool = True,
4403                              escape_comprehensions: bool = False) -> bool:
4404        """Add symbol table node to the currently active symbol table.
4405
4406        Return True if we actually added the symbol, or False if we refused
4407        to do so (because something is not ready or it was a no-op).
4408
4409        Generate an error if there is an invalid redefinition.
4410
4411        If context is None, unconditionally add node, since we can't report
4412        an error. Note that this is used by plugins to forcibly replace nodes!
4413
4414        TODO: Prevent plugins from replacing nodes, as it could cause problems?
4415
4416        Args:
4417            name: short name of symbol
4418            symbol: Node to add
4419            can_defer: if True, defer current target if adding a placeholder
4420            context: error context (see above about None value)
4421        """
4422        names = self.current_symbol_table(escape_comprehensions=escape_comprehensions)
4423        existing = names.get(name)
4424        if isinstance(symbol.node, PlaceholderNode) and can_defer:
4425            self.defer(context)
4426        if (existing is not None
4427                and context is not None
4428                and not is_valid_replacement(existing, symbol)):
4429            # There is an existing node, so this may be a redefinition.
4430            # If the new node points to the same node as the old one,
4431            # or if both old and new nodes are placeholders, we don't
4432            # need to do anything.
4433            old = existing.node
4434            new = symbol.node
4435            if isinstance(new, PlaceholderNode):
4436                # We don't know whether this is okay. Let's wait until the next iteration.
4437                return False
4438            if not is_same_symbol(old, new):
4439                if isinstance(new, (FuncDef, Decorator, OverloadedFuncDef, TypeInfo)):
4440                    self.add_redefinition(names, name, symbol)
4441                if not (isinstance(new, (FuncDef, Decorator))
4442                        and self.set_original_def(old, new)):
4443                    self.name_already_defined(name, context, existing)
4444        elif (name not in self.missing_names[-1] and '*' not in self.missing_names[-1]):
4445            names[name] = symbol
4446            self.progress = True
4447            return True
4448        return False
4449
4450    def add_redefinition(self,
4451                         names: SymbolTable,
4452                         name: str,
4453                         symbol: SymbolTableNode) -> None:
4454        """Add a symbol table node that reflects a redefinition as a function or a class.
4455
4456        Redefinitions need to be added to the symbol table so that they can be found
4457        through AST traversal, but they have dummy names of form 'name-redefinition[N]',
4458        where N ranges over 2, 3, ... (omitted for the first redefinition).
4459
4460        Note: we always store redefinitions independently of whether they are valid or not
4461        (so they will be semantically analyzed), the caller should give an error for invalid
4462        redefinitions (such as e.g. variable redefined as a class).
4463        """
4464        i = 1
4465        # Don't serialize redefined nodes. They are likely to have
4466        # busted internal references which can cause problems with
4467        # serialization and they can't have any external references to
4468        # them.
4469        symbol.no_serialize = True
4470        while True:
4471            if i == 1:
4472                new_name = '{}-redefinition'.format(name)
4473            else:
4474                new_name = '{}-redefinition{}'.format(name, i)
4475            existing = names.get(new_name)
4476            if existing is None:
4477                names[new_name] = symbol
4478                return
4479            elif existing.node is symbol.node:
4480                # Already there
4481                return
4482            i += 1
4483
4484    def add_local(self, node: Union[Var, FuncDef, OverloadedFuncDef], context: Context) -> None:
4485        """Add local variable or function."""
4486        assert self.is_func_scope()
4487        name = node.name
4488        node._fullname = name
4489        self.add_symbol(name, node, context)
4490
4491    def add_module_symbol(self,
4492                          id: str,
4493                          as_id: str,
4494                          context: Context,
4495                          module_public: bool,
4496                          module_hidden: bool) -> None:
4497        """Add symbol that is a reference to a module object."""
4498        if id in self.modules:
4499            node = self.modules[id]
4500            self.add_symbol(as_id, node, context,
4501                            module_public=module_public,
4502                            module_hidden=module_hidden)
4503        else:
4504            self.add_unknown_imported_symbol(
4505                as_id, context, target_name=id, module_public=module_public,
4506                module_hidden=module_hidden
4507            )
4508
4509    def add_imported_symbol(self,
4510                            name: str,
4511                            node: SymbolTableNode,
4512                            context: Context,
4513                            module_public: bool,
4514                            module_hidden: bool) -> None:
4515        """Add an alias to an existing symbol through import."""
4516        assert not module_hidden or not module_public
4517        symbol = SymbolTableNode(node.kind, node.node,
4518                                 module_public=module_public,
4519                                 module_hidden=module_hidden)
4520        self.add_symbol_table_node(name, symbol, context)
4521
4522    def add_unknown_imported_symbol(self,
4523                                    name: str,
4524                                    context: Context,
4525                                    target_name: Optional[str],
4526                                    module_public: bool,
4527                                    module_hidden: bool) -> None:
4528        """Add symbol that we don't know what it points to because resolving an import failed.
4529
4530        This can happen if a module is missing, or it is present, but doesn't have
4531        the imported attribute. The `target_name` is the name of symbol in the namespace
4532        it is imported from. For example, for 'from mod import x as y' the target_name is
4533        'mod.x'. This is currently used only to track logical dependencies.
4534        """
4535        existing = self.current_symbol_table().get(name)
4536        if existing and isinstance(existing.node, Var) and existing.node.is_suppressed_import:
4537            # This missing import was already added -- nothing to do here.
4538            return
4539        var = Var(name)
4540        if self.options.logical_deps and target_name is not None:
4541            # This makes it possible to add logical fine-grained dependencies
4542            # from a missing module. We can't use this by default, since in a
4543            # few places we assume that the full name points to a real
4544            # definition, but this name may point to nothing.
4545            var._fullname = target_name
4546        elif self.type:
4547            var._fullname = self.type.fullname + "." + name
4548            var.info = self.type
4549        else:
4550            var._fullname = self.qualified_name(name)
4551        var.is_ready = True
4552        any_type = AnyType(TypeOfAny.from_unimported_type, missing_import_name=var._fullname)
4553        var.type = any_type
4554        var.is_suppressed_import = True
4555        self.add_symbol(
4556            name, var, context, module_public=module_public, module_hidden=module_hidden
4557        )
4558
4559    #
4560    # Other helpers
4561    #
4562
4563    @contextmanager
4564    def tvar_scope_frame(self, frame: TypeVarLikeScope) -> Iterator[None]:
4565        old_scope = self.tvar_scope
4566        self.tvar_scope = frame
4567        yield
4568        self.tvar_scope = old_scope
4569
4570    def defer(self, debug_context: Optional[Context] = None) -> None:
4571        """Defer current analysis target to be analyzed again.
4572
4573        This must be called if something in the current target is
4574        incomplete or has a placeholder node. However, this must *not*
4575        be called during the final analysis iteration! Instead, an error
4576        should be generated. Often 'process_placeholder' is a good
4577        way to either defer or generate an error.
4578
4579        NOTE: Some methods, such as 'anal_type', 'mark_incomplete' and
4580              'record_incomplete_ref', call this implicitly, or when needed.
4581              They are usually preferable to a direct defer() call.
4582        """
4583        assert not self.final_iteration, 'Must not defer during final iteration'
4584        self.deferred = True
4585        # Store debug info for this deferral.
4586        line = (debug_context.line if debug_context else
4587                self.statement.line if self.statement else -1)
4588        self.deferral_debug_context.append((self.cur_mod_id, line))
4589
4590    def track_incomplete_refs(self) -> Tag:
4591        """Return tag that can be used for tracking references to incomplete names."""
4592        return self.num_incomplete_refs
4593
4594    def found_incomplete_ref(self, tag: Tag) -> bool:
4595        """Have we encountered an incomplete reference since starting tracking?"""
4596        return self.num_incomplete_refs != tag
4597
4598    def record_incomplete_ref(self) -> None:
4599        """Record the encounter of an incomplete reference and defer current analysis target."""
4600        self.defer()
4601        self.num_incomplete_refs += 1
4602
4603    def mark_incomplete(self, name: str, node: Node,
4604                        becomes_typeinfo: bool = False,
4605                        module_public: bool = True,
4606                        module_hidden: bool = False) -> None:
4607        """Mark a definition as incomplete (and defer current analysis target).
4608
4609        Also potentially mark the current namespace as incomplete.
4610
4611        Args:
4612            name: The name that we weren't able to define (or '*' if the name is unknown)
4613            node: The node that refers to the name (definition or lvalue)
4614            becomes_typeinfo: Pass this to PlaceholderNode (used by special forms like
4615                named tuples that will create TypeInfos).
4616        """
4617        self.defer(node)
4618        if name == '*':
4619            self.incomplete = True
4620        elif not self.is_global_or_nonlocal(name):
4621            fullname = self.qualified_name(name)
4622            assert self.statement
4623            placeholder = PlaceholderNode(fullname, node, self.statement.line,
4624                                          becomes_typeinfo=becomes_typeinfo)
4625            self.add_symbol(name, placeholder,
4626                            module_public=module_public, module_hidden=module_hidden,
4627                            context=dummy_context())
4628        self.missing_names[-1].add(name)
4629
4630    def is_incomplete_namespace(self, fullname: str) -> bool:
4631        """Is a module or class namespace potentially missing some definitions?
4632
4633        If a name is missing from an incomplete namespace, we'll need to defer the
4634        current analysis target.
4635        """
4636        return fullname in self.incomplete_namespaces
4637
4638    def process_placeholder(self, name: str, kind: str, ctx: Context) -> None:
4639        """Process a reference targeting placeholder node.
4640
4641        If this is not a final iteration, defer current node,
4642        otherwise report an error.
4643
4644        The 'kind' argument indicates if this a name or attribute expression
4645        (used for better error message).
4646        """
4647        if self.final_iteration:
4648            self.cannot_resolve_name(name, kind, ctx)
4649        else:
4650            self.defer(ctx)
4651
4652    def cannot_resolve_name(self, name: str, kind: str, ctx: Context) -> None:
4653        self.fail('Cannot resolve {} "{}" (possible cyclic definition)'.format(kind, name), ctx)
4654
4655    def qualified_name(self, name: str) -> str:
4656        if self.type is not None:
4657            return self.type._fullname + '.' + name
4658        elif self.is_func_scope():
4659            return name
4660        else:
4661            return self.cur_mod_id + '.' + name
4662
4663    def enter(self, function: Union[FuncItem, GeneratorExpr, DictionaryComprehension]) -> None:
4664        """Enter a function, generator or comprehension scope."""
4665        names = self.saved_locals.setdefault(function, SymbolTable())
4666        self.locals.append(names)
4667        is_comprehension = isinstance(function, (GeneratorExpr, DictionaryComprehension))
4668        self.is_comprehension_stack.append(is_comprehension)
4669        self.global_decls.append(set())
4670        self.nonlocal_decls.append(set())
4671        # -1 since entering block will increment this to 0.
4672        self.block_depth.append(-1)
4673        self.missing_names.append(set())
4674
4675    def leave(self) -> None:
4676        self.locals.pop()
4677        self.is_comprehension_stack.pop()
4678        self.global_decls.pop()
4679        self.nonlocal_decls.pop()
4680        self.block_depth.pop()
4681        self.missing_names.pop()
4682
4683    def is_func_scope(self) -> bool:
4684        return self.locals[-1] is not None
4685
4686    def is_nested_within_func_scope(self) -> bool:
4687        """Are we underneath a function scope, even if we are in a nested class also?"""
4688        return any(l is not None for l in self.locals)
4689
4690    def is_class_scope(self) -> bool:
4691        return self.type is not None and not self.is_func_scope()
4692
4693    def is_module_scope(self) -> bool:
4694        return not (self.is_class_scope() or self.is_func_scope())
4695
4696    def current_symbol_kind(self) -> int:
4697        if self.is_class_scope():
4698            kind = MDEF
4699        elif self.is_func_scope():
4700            kind = LDEF
4701        else:
4702            kind = GDEF
4703        return kind
4704
4705    def current_symbol_table(self, escape_comprehensions: bool = False) -> SymbolTable:
4706        if self.is_func_scope():
4707            assert self.locals[-1] is not None
4708            if escape_comprehensions:
4709                assert len(self.locals) == len(self.is_comprehension_stack)
4710                # Retrieve the symbol table from the enclosing non-comprehension scope.
4711                for i, is_comprehension in enumerate(reversed(self.is_comprehension_stack)):
4712                    if not is_comprehension:
4713                        if i == len(self.locals) - 1:  # The last iteration.
4714                            # The caller of the comprehension is in the global space.
4715                            names = self.globals
4716                        else:
4717                            names_candidate = self.locals[-1 - i]
4718                            assert names_candidate is not None, \
4719                                "Escaping comprehension from invalid scope"
4720                            names = names_candidate
4721                        break
4722                else:
4723                    assert False, "Should have at least one non-comprehension scope"
4724            else:
4725                names = self.locals[-1]
4726            assert names is not None
4727        elif self.type is not None:
4728            names = self.type.names
4729        else:
4730            names = self.globals
4731        return names
4732
4733    def is_global_or_nonlocal(self, name: str) -> bool:
4734        return (self.is_func_scope()
4735                and (name in self.global_decls[-1]
4736                     or name in self.nonlocal_decls[-1]))
4737
4738    def add_exports(self, exp_or_exps: Union[Iterable[Expression], Expression]) -> None:
4739        exps = [exp_or_exps] if isinstance(exp_or_exps, Expression) else exp_or_exps
4740        for exp in exps:
4741            if isinstance(exp, StrExpr):
4742                self.all_exports.append(exp.value)
4743
4744    def check_no_global(self,
4745                        name: str,
4746                        ctx: Context,
4747                        is_overloaded_func: bool = False) -> None:
4748        if name in self.globals:
4749            prev_is_overloaded = isinstance(self.globals[name], OverloadedFuncDef)
4750            if is_overloaded_func and prev_is_overloaded:
4751                self.fail("Nonconsecutive overload {} found".format(name), ctx)
4752            elif prev_is_overloaded:
4753                self.fail("Definition of '{}' missing 'overload'".format(name), ctx)
4754            else:
4755                self.name_already_defined(name, ctx, self.globals[name])
4756
4757    def name_not_defined(self, name: str, ctx: Context, namespace: Optional[str] = None) -> None:
4758        incomplete = self.is_incomplete_namespace(namespace or self.cur_mod_id)
4759        if (namespace is None
4760                and self.type
4761                and not self.is_func_scope()
4762                and self.incomplete_type_stack[-1]
4763                and not self.final_iteration):
4764            # We are processing a class body for the first time, so it is incomplete.
4765            incomplete = True
4766        if incomplete:
4767            # Target namespace is incomplete, so it's possible that the name will be defined
4768            # later on. Defer current target.
4769            self.record_incomplete_ref()
4770            return
4771        message = 'Name "{}" is not defined'.format(name)
4772        self.fail(message, ctx, code=codes.NAME_DEFINED)
4773
4774        if 'builtins.{}'.format(name) in SUGGESTED_TEST_FIXTURES:
4775            # The user probably has a missing definition in a test fixture. Let's verify.
4776            fullname = 'builtins.{}'.format(name)
4777            if self.lookup_fully_qualified_or_none(fullname) is None:
4778                # Yes. Generate a helpful note.
4779                self.msg.add_fixture_note(fullname, ctx)
4780
4781        modules_with_unimported_hints = {
4782            name.split('.', 1)[0]
4783            for name in TYPES_FOR_UNIMPORTED_HINTS
4784        }
4785        lowercased = {
4786            name.lower(): name
4787            for name in TYPES_FOR_UNIMPORTED_HINTS
4788        }
4789        for module in modules_with_unimported_hints:
4790            fullname = '{}.{}'.format(module, name).lower()
4791            if fullname not in lowercased:
4792                continue
4793            # User probably forgot to import these types.
4794            hint = (
4795                'Did you forget to import it from "{module}"?'
4796                ' (Suggestion: "from {module} import {name}")'
4797            ).format(module=module, name=lowercased[fullname].rsplit('.', 1)[-1])
4798            self.note(hint, ctx, code=codes.NAME_DEFINED)
4799
4800    def already_defined(self,
4801                        name: str,
4802                        ctx: Context,
4803                        original_ctx: Optional[Union[SymbolTableNode, SymbolNode]],
4804                        noun: str) -> None:
4805        if isinstance(original_ctx, SymbolTableNode):
4806            node = original_ctx.node  # type: Optional[SymbolNode]
4807        elif isinstance(original_ctx, SymbolNode):
4808            node = original_ctx
4809        else:
4810            node = None
4811
4812        if isinstance(original_ctx, SymbolTableNode) and isinstance(original_ctx.node, MypyFile):
4813            # Since this is an import, original_ctx.node points to the module definition.
4814            # Therefore its line number is always 1, which is not useful for this
4815            # error message.
4816            extra_msg = ' (by an import)'
4817        elif node and node.line != -1 and self.is_local_name(node.fullname):
4818            # TODO: Using previous symbol node may give wrong line. We should use
4819            #       the line number where the binding was established instead.
4820            extra_msg = ' on line {}'.format(node.line)
4821        else:
4822            extra_msg = ' (possibly by an import)'
4823        self.fail('{} "{}" already defined{}'.format(noun, unmangle(name), extra_msg), ctx,
4824                  code=codes.NO_REDEF)
4825
4826    def name_already_defined(self,
4827                             name: str,
4828                             ctx: Context,
4829                             original_ctx: Optional[Union[SymbolTableNode, SymbolNode]] = None
4830                             ) -> None:
4831        self.already_defined(name, ctx, original_ctx, noun='Name')
4832
4833    def attribute_already_defined(self,
4834                                  name: str,
4835                                  ctx: Context,
4836                                  original_ctx: Optional[Union[SymbolTableNode, SymbolNode]] = None
4837                                  ) -> None:
4838        self.already_defined(name, ctx, original_ctx, noun='Attribute')
4839
4840    def is_local_name(self, name: str) -> bool:
4841        """Does name look like reference to a definition in the current module?"""
4842        return self.is_defined_in_current_module(name) or '.' not in name
4843
4844    def fail(self,
4845             msg: str,
4846             ctx: Context,
4847             serious: bool = False,
4848             *,
4849             code: Optional[ErrorCode] = None,
4850             blocker: bool = False) -> None:
4851        if (not serious and
4852                not self.options.check_untyped_defs and
4853                self.function_stack and
4854                self.function_stack[-1].is_dynamic()):
4855            return
4856        # In case it's a bug and we don't really have context
4857        assert ctx is not None, msg
4858        self.errors.report(ctx.get_line(), ctx.get_column(), msg, blocker=blocker, code=code)
4859
4860    def fail_blocker(self, msg: str, ctx: Context) -> None:
4861        self.fail(msg, ctx, blocker=True)
4862
4863    def note(self, msg: str, ctx: Context, code: Optional[ErrorCode] = None) -> None:
4864        if (not self.options.check_untyped_defs and
4865                self.function_stack and
4866                self.function_stack[-1].is_dynamic()):
4867            return
4868        self.errors.report(ctx.get_line(), ctx.get_column(), msg, severity='note', code=code)
4869
4870    def accept(self, node: Node) -> None:
4871        try:
4872            node.accept(self)
4873        except Exception as err:
4874            report_internal_error(err, self.errors.file, node.line, self.errors, self.options)
4875
4876    def expr_to_analyzed_type(self,
4877                              expr: Expression,
4878                              report_invalid_types: bool = True,
4879                              allow_placeholder: bool = False) -> Optional[Type]:
4880        if isinstance(expr, CallExpr):
4881            expr.accept(self)
4882            internal_name, info = self.named_tuple_analyzer.check_namedtuple(expr, None,
4883                                                                             self.is_func_scope())
4884            if internal_name is None:
4885                # Some form of namedtuple is the only valid type that looks like a call
4886                # expression. This isn't a valid type.
4887                raise TypeTranslationError()
4888            elif not info:
4889                self.defer(expr)
4890                return None
4891            assert info.tuple_type, "NamedTuple without tuple type"
4892            fallback = Instance(info, [])
4893            return TupleType(info.tuple_type.items, fallback=fallback)
4894        typ = expr_to_unanalyzed_type(expr)
4895        return self.anal_type(typ, report_invalid_types=report_invalid_types,
4896                              allow_placeholder=allow_placeholder)
4897
4898    def analyze_type_expr(self, expr: Expression) -> None:
4899        # There are certain expressions that mypy does not need to semantically analyze,
4900        # since they analyzed solely as type. (For example, indexes in type alias definitions
4901        # and base classes in class defs). External consumers of the mypy AST may need
4902        # them semantically analyzed, however, if they need to treat it as an expression
4903        # and not a type. (Which is to say, mypyc needs to do this.) Do the analysis
4904        # in a fresh tvar scope in order to suppress any errors about using type variables.
4905        with self.tvar_scope_frame(TypeVarLikeScope()):
4906            expr.accept(self)
4907
4908    def type_analyzer(self, *,
4909                      tvar_scope: Optional[TypeVarLikeScope] = None,
4910                      allow_tuple_literal: bool = False,
4911                      allow_unbound_tvars: bool = False,
4912                      allow_placeholder: bool = False,
4913                      report_invalid_types: bool = True) -> TypeAnalyser:
4914        if tvar_scope is None:
4915            tvar_scope = self.tvar_scope
4916        tpan = TypeAnalyser(self,
4917                            tvar_scope,
4918                            self.plugin,
4919                            self.options,
4920                            self.is_typeshed_stub_file,
4921                            allow_unbound_tvars=allow_unbound_tvars,
4922                            allow_tuple_literal=allow_tuple_literal,
4923                            report_invalid_types=report_invalid_types,
4924                            allow_unnormalized=self.is_stub_file,
4925                            allow_placeholder=allow_placeholder)
4926        tpan.in_dynamic_func = bool(self.function_stack and self.function_stack[-1].is_dynamic())
4927        tpan.global_scope = not self.type and not self.function_stack
4928        return tpan
4929
4930    def anal_type(self,
4931                  typ: Type, *,
4932                  tvar_scope: Optional[TypeVarLikeScope] = None,
4933                  allow_tuple_literal: bool = False,
4934                  allow_unbound_tvars: bool = False,
4935                  allow_placeholder: bool = False,
4936                  report_invalid_types: bool = True,
4937                  third_pass: bool = False) -> Optional[Type]:
4938        """Semantically analyze a type.
4939
4940        Args:
4941            typ: Type to analyze (if already analyzed, this is a no-op)
4942            allow_placeholder: If True, may return PlaceholderType if
4943                encountering an incomplete definition
4944            third_pass: Unused; only for compatibility with old semantic
4945                analyzer
4946
4947        Return None only if some part of the type couldn't be bound *and* it
4948        referred to an incomplete namespace or definition. In this case also
4949        defer as needed. During a final iteration this won't return None;
4950        instead report an error if the type can't be analyzed and return
4951        AnyType.
4952
4953        In case of other errors, report an error message and return AnyType.
4954
4955        NOTE: The caller shouldn't defer even if this returns None or a
4956              placeholder type.
4957        """
4958        a = self.type_analyzer(tvar_scope=tvar_scope,
4959                               allow_unbound_tvars=allow_unbound_tvars,
4960                               allow_tuple_literal=allow_tuple_literal,
4961                               allow_placeholder=allow_placeholder,
4962                               report_invalid_types=report_invalid_types)
4963        tag = self.track_incomplete_refs()
4964        typ = typ.accept(a)
4965        if self.found_incomplete_ref(tag):
4966            # Something could not be bound yet.
4967            return None
4968        self.add_type_alias_deps(a.aliases_used)
4969        return typ
4970
4971    def class_type(self, self_type: Type) -> Type:
4972        return TypeType.make_normalized(self_type)
4973
4974    def schedule_patch(self, priority: int, patch: Callable[[], None]) -> None:
4975        self.patches.append((priority, patch))
4976
4977    def report_hang(self) -> None:
4978        print('Deferral trace:')
4979        for mod, line in self.deferral_debug_context:
4980            print('    {}:{}'.format(mod, line))
4981        self.errors.report(-1, -1,
4982                           'INTERNAL ERROR: maximum semantic analysis iteration count reached',
4983                           blocker=True)
4984
4985    def add_plugin_dependency(self, trigger: str, target: Optional[str] = None) -> None:
4986        """Add dependency from trigger to a target.
4987
4988        If the target is not given explicitly, use the current target.
4989        """
4990        if target is None:
4991            target = self.scope.current_target()
4992        self.cur_mod_node.plugin_deps.setdefault(trigger, set()).add(target)
4993
4994    def add_type_alias_deps(self,
4995                            aliases_used: Iterable[str],
4996                            target: Optional[str] = None) -> None:
4997        """Add full names of type aliases on which the current node depends.
4998
4999        This is used by fine-grained incremental mode to re-check the corresponding nodes.
5000        If `target` is None, then the target node used will be the current scope.
5001        """
5002        if not aliases_used:
5003            # A basic optimization to avoid adding targets with no dependencies to
5004            # the `alias_deps` dict.
5005            return
5006        if target is None:
5007            target = self.scope.current_target()
5008        self.cur_mod_node.alias_deps[target].update(aliases_used)
5009
5010    def is_mangled_global(self, name: str) -> bool:
5011        # A global is mangled if there exists at least one renamed variant.
5012        return unmangle(name) + "'" in self.globals
5013
5014    def is_initial_mangled_global(self, name: str) -> bool:
5015        # If there are renamed definitions for a global, the first one has exactly one prime.
5016        return name == unmangle(name) + "'"
5017
5018    def parse_bool(self, expr: Expression) -> Optional[bool]:
5019        if isinstance(expr, NameExpr):
5020            if expr.fullname == 'builtins.True':
5021                return True
5022            if expr.fullname == 'builtins.False':
5023                return False
5024        return None
5025
5026    def set_future_import_flags(self, module_name: str) -> None:
5027        if module_name in FUTURE_IMPORTS:
5028            self.future_import_flags.add(FUTURE_IMPORTS[module_name])
5029
5030    def is_future_flag_set(self, flag: str) -> bool:
5031        return flag in self.future_import_flags
5032
5033
5034class HasPlaceholders(TypeQuery[bool]):
5035    def __init__(self) -> None:
5036        super().__init__(any)
5037
5038    def visit_placeholder_type(self, t: PlaceholderType) -> bool:
5039        return True
5040
5041
5042def has_placeholder(typ: Type) -> bool:
5043    """Check if a type contains any placeholder types (recursively)."""
5044    return typ.accept(HasPlaceholders())
5045
5046
5047def replace_implicit_first_type(sig: FunctionLike, new: Type) -> FunctionLike:
5048    if isinstance(sig, CallableType):
5049        if len(sig.arg_types) == 0:
5050            return sig
5051        return sig.copy_modified(arg_types=[new] + sig.arg_types[1:])
5052    elif isinstance(sig, Overloaded):
5053        return Overloaded([cast(CallableType, replace_implicit_first_type(i, new))
5054                           for i in sig.items()])
5055    else:
5056        assert False
5057
5058
5059def refers_to_fullname(node: Expression, fullname: str) -> bool:
5060    """Is node a name or member expression with the given full name?"""
5061    if not isinstance(node, RefExpr):
5062        return False
5063    if node.fullname == fullname:
5064        return True
5065    if isinstance(node.node, TypeAlias):
5066        target = get_proper_type(node.node.target)
5067        if isinstance(target, Instance) and target.type.fullname == fullname:
5068            return True
5069    return False
5070
5071
5072def refers_to_class_or_function(node: Expression) -> bool:
5073    """Does semantically analyzed node refer to a class?"""
5074    return (isinstance(node, RefExpr) and
5075            isinstance(node.node, (TypeInfo, FuncDef, OverloadedFuncDef)))
5076
5077
5078def find_duplicate(list: List[T]) -> Optional[T]:
5079    """If the list has duplicates, return one of the duplicates.
5080
5081    Otherwise, return None.
5082    """
5083    for i in range(1, len(list)):
5084        if list[i] in list[:i]:
5085            return list[i]
5086    return None
5087
5088
5089def remove_imported_names_from_symtable(names: SymbolTable,
5090                                        module: str) -> None:
5091    """Remove all imported names from the symbol table of a module."""
5092    removed = []  # type: List[str]
5093    for name, node in names.items():
5094        if node.node is None:
5095            continue
5096        fullname = node.node.fullname
5097        prefix = fullname[:fullname.rfind('.')]
5098        if prefix != module:
5099            removed.append(name)
5100    for name in removed:
5101        del names[name]
5102
5103
5104def make_any_non_explicit(t: Type) -> Type:
5105    """Replace all Any types within in with Any that has attribute 'explicit' set to False"""
5106    return t.accept(MakeAnyNonExplicit())
5107
5108
5109class MakeAnyNonExplicit(TypeTranslator):
5110    def visit_any(self, t: AnyType) -> Type:
5111        if t.type_of_any == TypeOfAny.explicit:
5112            return t.copy_modified(TypeOfAny.special_form)
5113        return t
5114
5115    def visit_type_alias_type(self, t: TypeAliasType) -> Type:
5116        return t.copy_modified(args=[a.accept(self) for a in t.args])
5117
5118
5119def apply_semantic_analyzer_patches(patches: List[Tuple[int, Callable[[], None]]]) -> None:
5120    """Call patch callbacks in the right order.
5121
5122    This should happen after semantic analyzer pass 3.
5123    """
5124    patches_by_priority = sorted(patches, key=lambda x: x[0])
5125    for priority, patch_func in patches_by_priority:
5126        patch_func()
5127
5128
5129def names_modified_by_assignment(s: AssignmentStmt) -> List[NameExpr]:
5130    """Return all unqualified (short) names assigned to in an assignment statement."""
5131    result = []  # type: List[NameExpr]
5132    for lvalue in s.lvalues:
5133        result += names_modified_in_lvalue(lvalue)
5134    return result
5135
5136
5137def names_modified_in_lvalue(lvalue: Lvalue) -> List[NameExpr]:
5138    """Return all NameExpr assignment targets in an Lvalue."""
5139    if isinstance(lvalue, NameExpr):
5140        return [lvalue]
5141    elif isinstance(lvalue, StarExpr):
5142        return names_modified_in_lvalue(lvalue.expr)
5143    elif isinstance(lvalue, (ListExpr, TupleExpr)):
5144        result = []  # type: List[NameExpr]
5145        for item in lvalue.items:
5146            result += names_modified_in_lvalue(item)
5147        return result
5148    return []
5149
5150
5151def is_same_var_from_getattr(n1: Optional[SymbolNode], n2: Optional[SymbolNode]) -> bool:
5152    """Do n1 and n2 refer to the same Var derived from module-level __getattr__?"""
5153    return (isinstance(n1, Var)
5154            and n1.from_module_getattr
5155            and isinstance(n2, Var)
5156            and n2.from_module_getattr
5157            and n1.fullname == n2.fullname)
5158
5159
5160def dummy_context() -> Context:
5161    return TempNode(AnyType(TypeOfAny.special_form))
5162
5163
5164def is_valid_replacement(old: SymbolTableNode, new: SymbolTableNode) -> bool:
5165    """Can symbol table node replace an existing one?
5166
5167    These are the only valid cases:
5168
5169    1. Placeholder gets replaced with a non-placeholder
5170    2. Placeholder that isn't known to become type replaced with a
5171       placeholder that can become a type
5172    """
5173    if isinstance(old.node, PlaceholderNode):
5174        if isinstance(new.node, PlaceholderNode):
5175            return not old.node.becomes_typeinfo and new.node.becomes_typeinfo
5176        else:
5177            return True
5178    return False
5179
5180
5181def is_same_symbol(a: Optional[SymbolNode], b: Optional[SymbolNode]) -> bool:
5182    return (a == b
5183            or (isinstance(a, PlaceholderNode)
5184                and isinstance(b, PlaceholderNode))
5185            or is_same_var_from_getattr(a, b))
5186