1"""Abstract syntax tree node classes (i.e. parse tree)."""
2
3import os
4from abc import abstractmethod
5from mypy.ordered_dict import OrderedDict
6from collections import defaultdict
7from typing import (
8    Any, TypeVar, List, Tuple, cast, Set, Dict, Union, Optional, Callable, Sequence, Iterator
9)
10from typing_extensions import DefaultDict, Final, TYPE_CHECKING
11from mypy_extensions import trait
12
13import mypy.strconv
14from mypy.util import short_type
15from mypy.visitor import NodeVisitor, StatementVisitor, ExpressionVisitor
16
17from mypy.bogus_type import Bogus
18
19
20class Context:
21    """Base type for objects that are valid as error message locations."""
22    __slots__ = ('line', 'column', 'end_line')
23
24    def __init__(self, line: int = -1, column: int = -1) -> None:
25        self.line = line
26        self.column = column
27        self.end_line = None  # type: Optional[int]
28
29    def set_line(self,
30                 target: Union['Context', int],
31                 column: Optional[int] = None,
32                 end_line: Optional[int] = None) -> None:
33        """If target is a node, pull line (and column) information
34        into this node. If column is specified, this will override any column
35        information coming from a node.
36        """
37        if isinstance(target, int):
38            self.line = target
39        else:
40            self.line = target.line
41            self.column = target.column
42            self.end_line = target.end_line
43
44        if column is not None:
45            self.column = column
46
47        if end_line is not None:
48            self.end_line = end_line
49
50    def get_line(self) -> int:
51        """Don't use. Use x.line."""
52        return self.line
53
54    def get_column(self) -> int:
55        """Don't use. Use x.column."""
56        return self.column
57
58
59if TYPE_CHECKING:
60    # break import cycle only needed for mypy
61    import mypy.types
62
63
64T = TypeVar('T')
65
66JsonDict = Dict[str, Any]
67
68
69# Symbol table node kinds
70#
71# TODO rename to use more descriptive names
72
73LDEF = 0  # type: Final
74GDEF = 1  # type: Final
75MDEF = 2  # type: Final
76
77# Placeholder for a name imported via 'from ... import'. Second phase of
78# semantic will replace this the actual imported reference. This is
79# needed so that we can detect whether a name has been imported during
80# XXX what?
81UNBOUND_IMPORTED = 3  # type: Final
82
83# RevealExpr node kinds
84REVEAL_TYPE = 0  # type: Final
85REVEAL_LOCALS = 1  # type: Final
86
87LITERAL_YES = 2  # type: Final
88LITERAL_TYPE = 1  # type: Final
89LITERAL_NO = 0  # type: Final
90
91node_kinds = {
92    LDEF: 'Ldef',
93    GDEF: 'Gdef',
94    MDEF: 'Mdef',
95    UNBOUND_IMPORTED: 'UnboundImported',
96}  # type: Final
97inverse_node_kinds = {_kind: _name for _name, _kind in node_kinds.items()}  # type: Final
98
99
100implicit_module_attrs = {'__name__': '__builtins__.str',
101                         '__doc__': None,  # depends on Python version, see semanal.py
102                         '__file__': '__builtins__.str',
103                         '__package__': '__builtins__.str'}  # type: Final
104
105
106# These aliases exist because built-in class objects are not subscriptable.
107# For example `list[int]` fails at runtime. Instead List[int] should be used.
108type_aliases = {
109    'typing.List': 'builtins.list',
110    'typing.Dict': 'builtins.dict',
111    'typing.Set': 'builtins.set',
112    'typing.FrozenSet': 'builtins.frozenset',
113    'typing.ChainMap': 'collections.ChainMap',
114    'typing.Counter': 'collections.Counter',
115    'typing.DefaultDict': 'collections.defaultdict',
116    'typing.Deque': 'collections.deque',
117    'typing.OrderedDict': 'collections.OrderedDict',
118}  # type: Final
119
120# This keeps track of the oldest supported Python version where the corresponding
121# alias source is available.
122type_aliases_source_versions = {
123    'typing.List': (2, 7),
124    'typing.Dict': (2, 7),
125    'typing.Set': (2, 7),
126    'typing.FrozenSet': (2, 7),
127    'typing.ChainMap': (3, 3),
128    'typing.Counter': (2, 7),
129    'typing.DefaultDict': (2, 7),
130    'typing.Deque': (2, 7),
131    'typing.OrderedDict': (3, 7),
132}  # type: Final
133
134reverse_builtin_aliases = {
135    'builtins.list': 'typing.List',
136    'builtins.dict': 'typing.Dict',
137    'builtins.set': 'typing.Set',
138    'builtins.frozenset': 'typing.FrozenSet',
139}  # type: Final
140
141_nongen_builtins = {'builtins.tuple': 'typing.Tuple',
142                    'builtins.enumerate': ''}  # type: Final
143_nongen_builtins.update((name, alias) for alias, name in type_aliases.items())
144# Drop OrderedDict from this for backward compatibility
145del _nongen_builtins['collections.OrderedDict']
146
147
148def get_nongen_builtins(python_version: Tuple[int, int]) -> Dict[str, str]:
149    # After 3.9 with pep585 generic builtins are allowed.
150    return _nongen_builtins if python_version < (3, 9) else {}
151
152
153RUNTIME_PROTOCOL_DECOS = ('typing.runtime_checkable',
154                          'typing_extensions.runtime',
155                          'typing_extensions.runtime_checkable')  # type: Final
156
157
158class Node(Context):
159    """Common base class for all non-type parse tree nodes."""
160
161    __slots__ = ()
162
163    def __str__(self) -> str:
164        ans = self.accept(mypy.strconv.StrConv())
165        if ans is None:
166            return repr(self)
167        return ans
168
169    def accept(self, visitor: NodeVisitor[T]) -> T:
170        raise RuntimeError('Not implemented')
171
172
173@trait
174class Statement(Node):
175    """A statement node."""
176
177    __slots__ = ()
178
179    def accept(self, visitor: StatementVisitor[T]) -> T:
180        raise RuntimeError('Not implemented')
181
182
183@trait
184class Expression(Node):
185    """An expression node."""
186
187    __slots__ = ()
188
189    def accept(self, visitor: ExpressionVisitor[T]) -> T:
190        raise RuntimeError('Not implemented')
191
192
193class FakeExpression(Expression):
194    """A dummy expression.
195
196    We need a dummy expression in one place, and can't instantiate Expression
197    because it is a trait and mypyc barfs.
198    """
199    pass
200
201
202# TODO:
203# Lvalue = Union['NameExpr', 'MemberExpr', 'IndexExpr', 'SuperExpr', 'StarExpr'
204#                'TupleExpr']; see #1783.
205Lvalue = Expression
206
207
208@trait
209class SymbolNode(Node):
210    """Nodes that can be stored in a symbol table."""
211
212    __slots__ = ()
213
214    @property
215    @abstractmethod
216    def name(self) -> str: pass
217
218    # fullname can often be None even though the type system
219    # disagrees. We mark this with Bogus to let mypyc know not to
220    # worry about it.
221    @property
222    @abstractmethod
223    def fullname(self) -> Bogus[str]: pass
224
225    @abstractmethod
226    def serialize(self) -> JsonDict: pass
227
228    @classmethod
229    def deserialize(cls, data: JsonDict) -> 'SymbolNode':
230        classname = data['.class']
231        method = deserialize_map.get(classname)
232        if method is not None:
233            return method(data)
234        raise NotImplementedError('unexpected .class {}'.format(classname))
235
236
237# Items: fullname, related symbol table node, surrounding type (if any)
238Definition = Tuple[str, 'SymbolTableNode', Optional['TypeInfo']]
239
240
241class MypyFile(SymbolNode):
242    """The abstract syntax tree of a single source file."""
243
244    # Fully qualified module name
245    _fullname = None  # type: Bogus[str]
246    # Path to the file (empty string if not known)
247    path = ''
248    # Top-level definitions and statements
249    defs = None  # type: List[Statement]
250    # Type alias dependencies as mapping from target to set of alias full names
251    alias_deps = None  # type: DefaultDict[str, Set[str]]
252    # Is there a UTF-8 BOM at the start?
253    is_bom = False
254    names = None  # type: SymbolTable
255    # All import nodes within the file (also ones within functions etc.)
256    imports = None  # type: List[ImportBase]
257    # Lines on which to ignore certain errors when checking.
258    # If the value is empty, ignore all errors; otherwise, the list contains all
259    # error codes to ignore.
260    ignored_lines = None  # type: Dict[int, List[str]]
261    # Is this file represented by a stub file (.pyi)?
262    is_stub = False
263    # Is this loaded from the cache and thus missing the actual body of the file?
264    is_cache_skeleton = False
265    # Does this represent an __init__.pyi stub with a module __getattr__
266    # (i.e. a partial stub package), for such packages we suppress any missing
267    # module errors in addition to missing attribute errors.
268    is_partial_stub_package = False
269    # Plugin-created dependencies
270    plugin_deps = None  # type: Dict[str, Set[str]]
271
272    def __init__(self,
273                 defs: List[Statement],
274                 imports: List['ImportBase'],
275                 is_bom: bool = False,
276                 ignored_lines: Optional[Dict[int, List[str]]] = None) -> None:
277        super().__init__()
278        self.defs = defs
279        self.line = 1  # Dummy line number
280        self.imports = imports
281        self.is_bom = is_bom
282        self.alias_deps = defaultdict(set)
283        self.plugin_deps = {}
284        if ignored_lines:
285            self.ignored_lines = ignored_lines
286        else:
287            self.ignored_lines = {}
288
289    def local_definitions(self) -> Iterator[Definition]:
290        """Return all definitions within the module (including nested).
291
292        This doesn't include imported definitions.
293        """
294        return local_definitions(self.names, self.fullname)
295
296    @property
297    def name(self) -> str:
298        return '' if not self._fullname else self._fullname.split('.')[-1]
299
300    @property
301    def fullname(self) -> Bogus[str]:
302        return self._fullname
303
304    def accept(self, visitor: NodeVisitor[T]) -> T:
305        return visitor.visit_mypy_file(self)
306
307    def is_package_init_file(self) -> bool:
308        return len(self.path) != 0 and os.path.basename(self.path).startswith('__init__.')
309
310    def serialize(self) -> JsonDict:
311        return {'.class': 'MypyFile',
312                '_fullname': self._fullname,
313                'names': self.names.serialize(self._fullname),
314                'is_stub': self.is_stub,
315                'path': self.path,
316                'is_partial_stub_package': self.is_partial_stub_package,
317                }
318
319    @classmethod
320    def deserialize(cls, data: JsonDict) -> 'MypyFile':
321        assert data['.class'] == 'MypyFile', data
322        tree = MypyFile([], [])
323        tree._fullname = data['_fullname']
324        tree.names = SymbolTable.deserialize(data['names'])
325        tree.is_stub = data['is_stub']
326        tree.path = data['path']
327        tree.is_partial_stub_package = data['is_partial_stub_package']
328        tree.is_cache_skeleton = True
329        return tree
330
331
332class ImportBase(Statement):
333    """Base class for all import statements."""
334
335    is_unreachable = False  # Set by semanal.SemanticAnalyzerPass1 if inside `if False` etc.
336    is_top_level = False  # Ditto if outside any class or def
337    is_mypy_only = False  # Ditto if inside `if TYPE_CHECKING` or `if MYPY`
338
339    # If an import replaces existing definitions, we construct dummy assignment
340    # statements that assign the imported names to the names in the current scope,
341    # for type checking purposes. Example:
342    #
343    #     x = 1
344    #     from m import x   <-- add assignment representing "x = m.x"
345    assignments = None  # type: List[AssignmentStmt]
346
347    def __init__(self) -> None:
348        super().__init__()
349        self.assignments = []
350
351
352class Import(ImportBase):
353    """import m [as n]"""
354
355    ids = None  # type: List[Tuple[str, Optional[str]]]     # (module id, as id)
356
357    def __init__(self, ids: List[Tuple[str, Optional[str]]]) -> None:
358        super().__init__()
359        self.ids = ids
360
361    def accept(self, visitor: StatementVisitor[T]) -> T:
362        return visitor.visit_import(self)
363
364
365class ImportFrom(ImportBase):
366    """from m import x [as y], ..."""
367
368    id = None  # type: str
369    relative = None  # type: int
370    names = None  # type: List[Tuple[str, Optional[str]]]  # Tuples (name, as name)
371
372    def __init__(self, id: str, relative: int, names: List[Tuple[str, Optional[str]]]) -> None:
373        super().__init__()
374        self.id = id
375        self.names = names
376        self.relative = relative
377
378    def accept(self, visitor: StatementVisitor[T]) -> T:
379        return visitor.visit_import_from(self)
380
381
382class ImportAll(ImportBase):
383    """from m import *"""
384    id = None  # type: str
385    relative = None  # type: int
386    # NOTE: Only filled and used by old semantic analyzer.
387    imported_names = None  # type: List[str]
388
389    def __init__(self, id: str, relative: int) -> None:
390        super().__init__()
391        self.id = id
392        self.relative = relative
393        self.imported_names = []
394
395    def accept(self, visitor: StatementVisitor[T]) -> T:
396        return visitor.visit_import_all(self)
397
398
399class ImportedName(SymbolNode):
400    """Indirect reference to a fullname stored in symbol table.
401
402    This node is not present in the original program as such. This is
403    just a temporary artifact in binding imported names. After semantic
404    analysis pass 2, these references should be replaced with direct
405    reference to a real AST node.
406
407    Note that this is neither a Statement nor an Expression so this
408    can't be visited.
409    """
410
411    def __init__(self, target_fullname: str) -> None:
412        super().__init__()
413        self.target_fullname = target_fullname
414
415    @property
416    def name(self) -> str:
417        return self.target_fullname.split('.')[-1]
418
419    @property
420    def fullname(self) -> str:
421        return self.target_fullname
422
423    def serialize(self) -> JsonDict:
424        assert False, "ImportedName leaked from semantic analysis"
425
426    @classmethod
427    def deserialize(cls, data: JsonDict) -> 'ImportedName':
428        assert False, "ImportedName should never be serialized"
429
430    def __str__(self) -> str:
431        return 'ImportedName(%s)' % self.target_fullname
432
433
434FUNCBASE_FLAGS = [
435    'is_property', 'is_class', 'is_static', 'is_final'
436]  # type: Final
437
438
439class FuncBase(Node):
440    """Abstract base class for function-like nodes.
441
442    N.B: Although this has SymbolNode subclasses (FuncDef,
443    OverloadedFuncDef), avoid calling isinstance(..., FuncBase) on
444    something that is typed as SymbolNode.  This is to work around
445    mypy bug #3603, in which mypy doesn't understand multiple
446    inheritance very well, and will assume that a SymbolNode
447    cannot be a FuncBase.
448
449    Instead, test against SYMBOL_FUNCBASE_TYPES, which enumerates
450    SymbolNode subclasses that are also FuncBase subclasses.
451    """
452
453    __slots__ = ('type',
454                 'unanalyzed_type',
455                 'info',
456                 'is_property',
457                 'is_class',        # Uses "@classmethod" (explicit or implicit)
458                 'is_static',       # Uses "@staticmethod"
459                 'is_final',        # Uses "@final"
460                 '_fullname',
461                 )
462
463    def __init__(self) -> None:
464        super().__init__()
465        # Type signature. This is usually CallableType or Overloaded, but it can be
466        # something else for decorated functions.
467        self.type = None  # type: Optional[mypy.types.ProperType]
468        # Original, not semantically analyzed type (used for reprocessing)
469        self.unanalyzed_type = None  # type: Optional[mypy.types.ProperType]
470        # If method, reference to TypeInfo
471        # TODO: Type should be Optional[TypeInfo]
472        self.info = FUNC_NO_INFO
473        self.is_property = False
474        self.is_class = False
475        self.is_static = False
476        self.is_final = False
477        # Name with module prefix
478        # TODO: Type should be Optional[str]
479        self._fullname = cast(Bogus[str], None)
480
481    @property
482    @abstractmethod
483    def name(self) -> str: pass
484
485    @property
486    def fullname(self) -> Bogus[str]:
487        return self._fullname
488
489
490OverloadPart = Union['FuncDef', 'Decorator']
491
492
493class OverloadedFuncDef(FuncBase, SymbolNode, Statement):
494    """A logical node representing all the variants of a multi-declaration function.
495
496    A multi-declaration function is often an @overload, but can also be a
497    @property with a setter and a/or a deleter.
498
499    This node has no explicit representation in the source program.
500    Overloaded variants must be consecutive in the source file.
501    """
502
503    items = None  # type: List[OverloadPart]
504    unanalyzed_items = None  # type: List[OverloadPart]
505    impl = None  # type: Optional[OverloadPart]
506
507    def __init__(self, items: List['OverloadPart']) -> None:
508        super().__init__()
509        self.items = items
510        self.unanalyzed_items = items.copy()
511        self.impl = None
512        if len(items) > 0:
513            self.set_line(items[0].line, items[0].column)
514        self.is_final = False
515
516    @property
517    def name(self) -> str:
518        if self.items:
519            return self.items[0].name
520        else:
521            # This may happen for malformed overload
522            assert self.impl is not None
523            return self.impl.name
524
525    def accept(self, visitor: StatementVisitor[T]) -> T:
526        return visitor.visit_overloaded_func_def(self)
527
528    def serialize(self) -> JsonDict:
529        return {'.class': 'OverloadedFuncDef',
530                'items': [i.serialize() for i in self.items],
531                'type': None if self.type is None else self.type.serialize(),
532                'fullname': self._fullname,
533                'impl': None if self.impl is None else self.impl.serialize(),
534                'flags': get_flags(self, FUNCBASE_FLAGS),
535                }
536
537    @classmethod
538    def deserialize(cls, data: JsonDict) -> 'OverloadedFuncDef':
539        assert data['.class'] == 'OverloadedFuncDef'
540        res = OverloadedFuncDef([
541            cast(OverloadPart, SymbolNode.deserialize(d))
542            for d in data['items']])
543        if data.get('impl') is not None:
544            res.impl = cast(OverloadPart, SymbolNode.deserialize(data['impl']))
545            # set line for empty overload items, as not set in __init__
546            if len(res.items) > 0:
547                res.set_line(res.impl.line)
548        if data.get('type') is not None:
549            typ = mypy.types.deserialize_type(data['type'])
550            assert isinstance(typ, mypy.types.ProperType)
551            res.type = typ
552        res._fullname = data['fullname']
553        set_flags(res, data['flags'])
554        # NOTE: res.info will be set in the fixup phase.
555        return res
556
557
558class Argument(Node):
559    """A single argument in a FuncItem."""
560
561    __slots__ = ('variable', 'type_annotation', 'initializer', 'kind')
562
563    def __init__(self,
564                 variable: 'Var',
565                 type_annotation: 'Optional[mypy.types.Type]',
566                 initializer: Optional[Expression],
567                 kind: int) -> None:
568        super().__init__()
569        self.variable = variable
570        self.type_annotation = type_annotation
571        self.initializer = initializer
572        self.kind = kind  # must be an ARG_* constant
573
574    def set_line(self,
575                 target: Union[Context, int],
576                 column: Optional[int] = None,
577                 end_line: Optional[int] = None) -> None:
578        super().set_line(target, column, end_line)
579
580        if self.initializer and self.initializer.line < 0:
581            self.initializer.set_line(self.line, self.column, self.end_line)
582
583        self.variable.set_line(self.line, self.column, self.end_line)
584
585
586FUNCITEM_FLAGS = FUNCBASE_FLAGS + [
587    'is_overload', 'is_generator', 'is_coroutine', 'is_async_generator',
588    'is_awaitable_coroutine',
589]  # type: Final
590
591
592class FuncItem(FuncBase):
593    """Base class for nodes usable as overloaded function items."""
594
595    __slots__ = ('arguments',  # Note that can be None if deserialized (type is a lie!)
596                 'arg_names',  # Names of arguments
597                 'arg_kinds',  # Kinds of arguments
598                 'min_args',  # Minimum number of arguments
599                 'max_pos',  # Maximum number of positional arguments, -1 if no explicit
600                             # limit (*args not included)
601                 'body',  # Body of the function
602                 'is_overload',  # Is this an overload variant of function with more than
603                                 # one overload variant?
604                 'is_generator',  # Contains a yield statement?
605                 'is_coroutine',  # Defined using 'async def' syntax?
606                 'is_async_generator',  # Is an async def generator?
607                 'is_awaitable_coroutine',  # Decorated with '@{typing,asyncio}.coroutine'?
608                 'expanded',  # Variants of function with type variables with values expanded
609                 )
610
611    def __init__(self,
612                 arguments: List[Argument],
613                 body: 'Block',
614                 typ: 'Optional[mypy.types.FunctionLike]' = None) -> None:
615        super().__init__()
616        self.arguments = arguments
617        self.arg_names = [arg.variable.name for arg in self.arguments]
618        self.arg_kinds = [arg.kind for arg in self.arguments]  # type: List[int]
619        self.max_pos = self.arg_kinds.count(ARG_POS) + self.arg_kinds.count(ARG_OPT)
620        self.body = body
621        self.type = typ
622        self.unanalyzed_type = typ
623        self.is_overload = False
624        self.is_generator = False
625        self.is_coroutine = False
626        self.is_async_generator = False
627        self.is_awaitable_coroutine = False
628        self.expanded = []  # type: List[FuncItem]
629
630        self.min_args = 0
631        for i in range(len(self.arguments)):
632            if self.arguments[i] is None and i < self.max_fixed_argc():
633                self.min_args = i + 1
634
635    def max_fixed_argc(self) -> int:
636        return self.max_pos
637
638    def set_line(self,
639                 target: Union[Context, int],
640                 column: Optional[int] = None,
641                 end_line: Optional[int] = None) -> None:
642        super().set_line(target, column, end_line)
643        for arg in self.arguments:
644            arg.set_line(self.line, self.column, self.end_line)
645
646    def is_dynamic(self) -> bool:
647        return self.type is None
648
649
650FUNCDEF_FLAGS = FUNCITEM_FLAGS + [
651    'is_decorated', 'is_conditional', 'is_abstract',
652]  # type: Final
653
654
655class FuncDef(FuncItem, SymbolNode, Statement):
656    """Function definition.
657
658    This is a non-lambda function defined using 'def'.
659    """
660
661    __slots__ = ('_name',
662                 'is_decorated',
663                 'is_conditional',
664                 'is_abstract',
665                 'original_def',
666                 )
667
668    def __init__(self,
669                 name: str,              # Function name
670                 arguments: List[Argument],
671                 body: 'Block',
672                 typ: 'Optional[mypy.types.FunctionLike]' = None) -> None:
673        super().__init__(arguments, body, typ)
674        self._name = name
675        self.is_decorated = False
676        self.is_conditional = False  # Defined conditionally (within block)?
677        self.is_abstract = False
678        self.is_final = False
679        # Original conditional definition
680        self.original_def = None  # type: Union[None, FuncDef, Var, Decorator]
681
682    @property
683    def name(self) -> str:
684        return self._name
685
686    def accept(self, visitor: StatementVisitor[T]) -> T:
687        return visitor.visit_func_def(self)
688
689    def serialize(self) -> JsonDict:
690        # We're deliberating omitting arguments and storing only arg_names and
691        # arg_kinds for space-saving reasons (arguments is not used in later
692        # stages of mypy).
693        # TODO: After a FuncDef is deserialized, the only time we use `arg_names`
694        # and `arg_kinds` is when `type` is None and we need to infer a type. Can
695        # we store the inferred type ahead of time?
696        return {'.class': 'FuncDef',
697                'name': self._name,
698                'fullname': self._fullname,
699                'arg_names': self.arg_names,
700                'arg_kinds': self.arg_kinds,
701                'type': None if self.type is None else self.type.serialize(),
702                'flags': get_flags(self, FUNCDEF_FLAGS),
703                # TODO: Do we need expanded, original_def?
704                }
705
706    @classmethod
707    def deserialize(cls, data: JsonDict) -> 'FuncDef':
708        assert data['.class'] == 'FuncDef'
709        body = Block([])
710        ret = FuncDef(data['name'],
711                      [],
712                      body,
713                      (None if data['type'] is None
714                       else cast(mypy.types.FunctionLike,
715                                 mypy.types.deserialize_type(data['type']))))
716        ret._fullname = data['fullname']
717        set_flags(ret, data['flags'])
718        # NOTE: ret.info is set in the fixup phase.
719        ret.arg_names = data['arg_names']
720        ret.arg_kinds = data['arg_kinds']
721        # Leave these uninitialized so that future uses will trigger an error
722        del ret.arguments
723        del ret.max_pos
724        del ret.min_args
725        return ret
726
727
728# All types that are both SymbolNodes and FuncBases. See the FuncBase
729# docstring for the rationale.
730SYMBOL_FUNCBASE_TYPES = (OverloadedFuncDef, FuncDef)
731
732
733class Decorator(SymbolNode, Statement):
734    """A decorated function.
735
736    A single Decorator object can include any number of function decorators.
737    """
738
739    func = None  # type: FuncDef                # Decorated function
740    decorators = None  # type: List[Expression] # Decorators (may be empty)
741    # Some decorators are removed by semanal, keep the original here.
742    original_decorators = None  # type: List[Expression]
743    # TODO: This is mostly used for the type; consider replacing with a 'type' attribute
744    var = None  # type: Var                     # Represents the decorated function obj
745    is_overload = False
746
747    def __init__(self, func: FuncDef, decorators: List[Expression],
748                 var: 'Var') -> None:
749        super().__init__()
750        self.func = func
751        self.decorators = decorators
752        self.original_decorators = decorators.copy()
753        self.var = var
754        self.is_overload = False
755
756    @property
757    def name(self) -> str:
758        return self.func.name
759
760    @property
761    def fullname(self) -> Bogus[str]:
762        return self.func.fullname
763
764    @property
765    def is_final(self) -> bool:
766        return self.func.is_final
767
768    @property
769    def info(self) -> 'TypeInfo':
770        return self.func.info
771
772    @property
773    def type(self) -> 'Optional[mypy.types.Type]':
774        return self.var.type
775
776    def accept(self, visitor: StatementVisitor[T]) -> T:
777        return visitor.visit_decorator(self)
778
779    def serialize(self) -> JsonDict:
780        return {'.class': 'Decorator',
781                'func': self.func.serialize(),
782                'var': self.var.serialize(),
783                'is_overload': self.is_overload,
784                }
785
786    @classmethod
787    def deserialize(cls, data: JsonDict) -> 'Decorator':
788        assert data['.class'] == 'Decorator'
789        dec = Decorator(FuncDef.deserialize(data['func']),
790                        [],
791                        Var.deserialize(data['var']))
792        dec.is_overload = data['is_overload']
793        return dec
794
795
796VAR_FLAGS = [
797    'is_self', 'is_initialized_in_class', 'is_staticmethod',
798    'is_classmethod', 'is_property', 'is_settable_property', 'is_suppressed_import',
799    'is_classvar', 'is_abstract_var', 'is_final', 'final_unset_in_class', 'final_set_in_init',
800    'explicit_self_type', 'is_ready', 'from_module_getattr',
801]  # type: Final
802
803
804class Var(SymbolNode):
805    """A variable.
806
807    It can refer to global/local variable or a data attribute.
808    """
809
810    __slots__ = ('_name',
811                 '_fullname',
812                 'info',
813                 'type',
814                 'final_value',
815                 'is_self',
816                 'is_ready',
817                 'is_inferred',
818                 'is_initialized_in_class',
819                 'is_staticmethod',
820                 'is_classmethod',
821                 'is_property',
822                 'is_settable_property',
823                 'is_classvar',
824                 'is_abstract_var',
825                 'is_final',
826                 'final_unset_in_class',
827                 'final_set_in_init',
828                 'is_suppressed_import',
829                 'explicit_self_type',
830                 'from_module_getattr',
831                 )
832
833    def __init__(self, name: str, type: 'Optional[mypy.types.Type]' = None) -> None:
834        super().__init__()
835        self._name = name   # Name without module prefix
836        # TODO: Should be Optional[str]
837        self._fullname = cast('Bogus[str]', None)  # Name with module prefix
838        # TODO: Should be Optional[TypeInfo]
839        self.info = VAR_NO_INFO
840        self.type = type  # type: Optional[mypy.types.Type] # Declared or inferred type, or None
841        # Is this the first argument to an ordinary method (usually "self")?
842        self.is_self = False
843        self.is_ready = True  # If inferred, is the inferred type available?
844        self.is_inferred = (self.type is None)
845        # Is this initialized explicitly to a non-None value in class body?
846        self.is_initialized_in_class = False
847        self.is_staticmethod = False
848        self.is_classmethod = False
849        self.is_property = False
850        self.is_settable_property = False
851        self.is_classvar = False
852        self.is_abstract_var = False
853        # Set to true when this variable refers to a module we were unable to
854        # parse for some reason (eg a silenced module)
855        self.is_suppressed_import = False
856        # Was this "variable" (rather a constant) defined as Final[...]?
857        self.is_final = False
858        # If constant value is a simple literal,
859        # store the literal value (unboxed) for the benefit of
860        # tools like mypyc.
861        self.final_value = None  # type: Optional[Union[int, float, bool, str]]
862        # Where the value was set (only for class attributes)
863        self.final_unset_in_class = False
864        self.final_set_in_init = False
865        # This is True for a variable that was declared on self with an explicit type:
866        #     class C:
867        #         def __init__(self) -> None:
868        #             self.x: int
869        # This case is important because this defines a new Var, even if there is one
870        # present in a superclass (without explicit type this doesn't create a new Var).
871        # See SemanticAnalyzer.analyze_member_lvalue() for details.
872        self.explicit_self_type = False
873        # If True, this is an implicit Var created due to module-level __getattr__.
874        self.from_module_getattr = False
875
876    @property
877    def name(self) -> str:
878        return self._name
879
880    @property
881    def fullname(self) -> Bogus[str]:
882        return self._fullname
883
884    def accept(self, visitor: NodeVisitor[T]) -> T:
885        return visitor.visit_var(self)
886
887    def serialize(self) -> JsonDict:
888        # TODO: Leave default values out?
889        # NOTE: Sometimes self.is_ready is False here, but we don't care.
890        data = {'.class': 'Var',
891                'name': self._name,
892                'fullname': self._fullname,
893                'type': None if self.type is None else self.type.serialize(),
894                'flags': get_flags(self, VAR_FLAGS),
895                }  # type: JsonDict
896        if self.final_value is not None:
897            data['final_value'] = self.final_value
898        return data
899
900    @classmethod
901    def deserialize(cls, data: JsonDict) -> 'Var':
902        assert data['.class'] == 'Var'
903        name = data['name']
904        type = None if data['type'] is None else mypy.types.deserialize_type(data['type'])
905        v = Var(name, type)
906        v.is_ready = False  # Override True default set in __init__
907        v._fullname = data['fullname']
908        set_flags(v, data['flags'])
909        v.final_value = data.get('final_value')
910        return v
911
912
913class ClassDef(Statement):
914    """Class definition"""
915
916    name = None  # type: str       # Name of the class without module prefix
917    fullname = None  # type: Bogus[str]   # Fully qualified name of the class
918    defs = None  # type: Block
919    type_vars = None  # type: List[mypy.types.TypeVarDef]
920    # Base class expressions (not semantically analyzed -- can be arbitrary expressions)
921    base_type_exprs = None  # type: List[Expression]
922    # Special base classes like Generic[...] get moved here during semantic analysis
923    removed_base_type_exprs = None  # type: List[Expression]
924    info = None  # type: TypeInfo  # Related TypeInfo
925    metaclass = None  # type: Optional[Expression]
926    decorators = None  # type: List[Expression]
927    keywords = None  # type: OrderedDict[str, Expression]
928    analyzed = None  # type: Optional[Expression]
929    has_incompatible_baseclass = False
930
931    def __init__(self,
932                 name: str,
933                 defs: 'Block',
934                 type_vars: Optional[List['mypy.types.TypeVarDef']] = None,
935                 base_type_exprs: Optional[List[Expression]] = None,
936                 metaclass: Optional[Expression] = None,
937                 keywords: Optional[List[Tuple[str, Expression]]] = None) -> None:
938        super().__init__()
939        self.name = name
940        self.defs = defs
941        self.type_vars = type_vars or []
942        self.base_type_exprs = base_type_exprs or []
943        self.removed_base_type_exprs = []
944        self.info = CLASSDEF_NO_INFO
945        self.metaclass = metaclass
946        self.decorators = []
947        self.keywords = OrderedDict(keywords or [])
948
949    def accept(self, visitor: StatementVisitor[T]) -> T:
950        return visitor.visit_class_def(self)
951
952    def is_generic(self) -> bool:
953        return self.info.is_generic()
954
955    def serialize(self) -> JsonDict:
956        # Not serialized: defs, base_type_exprs, metaclass, decorators,
957        # analyzed (for named tuples etc.)
958        return {'.class': 'ClassDef',
959                'name': self.name,
960                'fullname': self.fullname,
961                'type_vars': [v.serialize() for v in self.type_vars],
962                }
963
964    @classmethod
965    def deserialize(self, data: JsonDict) -> 'ClassDef':
966        assert data['.class'] == 'ClassDef'
967        res = ClassDef(data['name'],
968                       Block([]),
969                       [mypy.types.TypeVarDef.deserialize(v) for v in data['type_vars']],
970                       )
971        res.fullname = data['fullname']
972        return res
973
974
975class GlobalDecl(Statement):
976    """Declaration global x, y, ..."""
977
978    names = None  # type: List[str]
979
980    def __init__(self, names: List[str]) -> None:
981        super().__init__()
982        self.names = names
983
984    def accept(self, visitor: StatementVisitor[T]) -> T:
985        return visitor.visit_global_decl(self)
986
987
988class NonlocalDecl(Statement):
989    """Declaration nonlocal x, y, ..."""
990
991    names = None  # type: List[str]
992
993    def __init__(self, names: List[str]) -> None:
994        super().__init__()
995        self.names = names
996
997    def accept(self, visitor: StatementVisitor[T]) -> T:
998        return visitor.visit_nonlocal_decl(self)
999
1000
1001class Block(Statement):
1002    __slots__ = ('body', 'is_unreachable')
1003
1004    def __init__(self, body: List[Statement]) -> None:
1005        super().__init__()
1006        self.body = body
1007        # True if we can determine that this block is not executed during semantic
1008        # analysis. For example, this applies to blocks that are protected by
1009        # something like "if PY3:" when using Python 2. However, some code is
1010        # only considered unreachable during type checking and this is not true
1011        # in those cases.
1012        self.is_unreachable = False
1013
1014    def accept(self, visitor: StatementVisitor[T]) -> T:
1015        return visitor.visit_block(self)
1016
1017
1018# Statements
1019
1020
1021class ExpressionStmt(Statement):
1022    """An expression as a statement, such as print(s)."""
1023    expr = None  # type: Expression
1024
1025    def __init__(self, expr: Expression) -> None:
1026        super().__init__()
1027        self.expr = expr
1028
1029    def accept(self, visitor: StatementVisitor[T]) -> T:
1030        return visitor.visit_expression_stmt(self)
1031
1032
1033class AssignmentStmt(Statement):
1034    """Assignment statement.
1035
1036    The same node class is used for single assignment, multiple assignment
1037    (e.g. x, y = z) and chained assignment (e.g. x = y = z), assignments
1038    that define new names, and assignments with explicit types ("# type: t"
1039    or "x: t [= ...]").
1040
1041    An lvalue can be NameExpr, TupleExpr, ListExpr, MemberExpr, or IndexExpr.
1042    """
1043
1044    lvalues = None  # type: List[Lvalue]
1045    # This is a TempNode if and only if no rvalue (x: t).
1046    rvalue = None  # type: Expression
1047    # Declared type in a comment, may be None.
1048    type = None  # type: Optional[mypy.types.Type]
1049    # Original, not semantically analyzed type in annotation (used for reprocessing)
1050    unanalyzed_type = None  # type: Optional[mypy.types.Type]
1051    # This indicates usage of PEP 526 type annotation syntax in assignment.
1052    new_syntax = False  # type: bool
1053    # Does this assignment define a type alias?
1054    is_alias_def = False
1055    # Is this a final definition?
1056    # Final attributes can't be re-assigned once set, and can't be overridden
1057    # in a subclass. This flag is not set if an attempted declaration was found to
1058    # be invalid during semantic analysis. It is still set to `True` if
1059    # a final declaration overrides another final declaration (this is checked
1060    # during type checking when MROs are known).
1061    is_final_def = False
1062
1063    def __init__(self, lvalues: List[Lvalue], rvalue: Expression,
1064                 type: 'Optional[mypy.types.Type]' = None, new_syntax: bool = False) -> None:
1065        super().__init__()
1066        self.lvalues = lvalues
1067        self.rvalue = rvalue
1068        self.type = type
1069        self.unanalyzed_type = type
1070        self.new_syntax = new_syntax
1071
1072    def accept(self, visitor: StatementVisitor[T]) -> T:
1073        return visitor.visit_assignment_stmt(self)
1074
1075
1076class OperatorAssignmentStmt(Statement):
1077    """Operator assignment statement such as x += 1"""
1078
1079    op = ''
1080    lvalue = None  # type: Lvalue
1081    rvalue = None  # type: Expression
1082
1083    def __init__(self, op: str, lvalue: Lvalue, rvalue: Expression) -> None:
1084        super().__init__()
1085        self.op = op
1086        self.lvalue = lvalue
1087        self.rvalue = rvalue
1088
1089    def accept(self, visitor: StatementVisitor[T]) -> T:
1090        return visitor.visit_operator_assignment_stmt(self)
1091
1092
1093class WhileStmt(Statement):
1094    expr = None  # type: Expression
1095    body = None  # type: Block
1096    else_body = None  # type: Optional[Block]
1097
1098    def __init__(self, expr: Expression, body: Block, else_body: Optional[Block]) -> None:
1099        super().__init__()
1100        self.expr = expr
1101        self.body = body
1102        self.else_body = else_body
1103
1104    def accept(self, visitor: StatementVisitor[T]) -> T:
1105        return visitor.visit_while_stmt(self)
1106
1107
1108class ForStmt(Statement):
1109    # Index variables
1110    index = None  # type: Lvalue
1111    # Type given by type comments for index, can be None
1112    index_type = None  # type: Optional[mypy.types.Type]
1113    # Original, not semantically analyzed type in annotation (used for reprocessing)
1114    unanalyzed_index_type = None  # type: Optional[mypy.types.Type]
1115    # Inferred iterable item type
1116    inferred_item_type = None  # type: Optional[mypy.types.Type]
1117    # Inferred iterator type
1118    inferred_iterator_type = None  # type: Optional[mypy.types.Type]
1119    # Expression to iterate
1120    expr = None  # type: Expression
1121    body = None  # type: Block
1122    else_body = None  # type: Optional[Block]
1123    is_async = False  # True if `async for ...` (PEP 492, Python 3.5)
1124
1125    def __init__(self,
1126                 index: Lvalue,
1127                 expr: Expression,
1128                 body: Block,
1129                 else_body: Optional[Block],
1130                 index_type: 'Optional[mypy.types.Type]' = None) -> None:
1131        super().__init__()
1132        self.index = index
1133        self.index_type = index_type
1134        self.unanalyzed_index_type = index_type
1135        self.expr = expr
1136        self.body = body
1137        self.else_body = else_body
1138
1139    def accept(self, visitor: StatementVisitor[T]) -> T:
1140        return visitor.visit_for_stmt(self)
1141
1142
1143class ReturnStmt(Statement):
1144    expr = None  # type: Optional[Expression]
1145
1146    def __init__(self, expr: Optional[Expression]) -> None:
1147        super().__init__()
1148        self.expr = expr
1149
1150    def accept(self, visitor: StatementVisitor[T]) -> T:
1151        return visitor.visit_return_stmt(self)
1152
1153
1154class AssertStmt(Statement):
1155    expr = None  # type: Expression
1156    msg = None  # type: Optional[Expression]
1157
1158    def __init__(self, expr: Expression, msg: Optional[Expression] = None) -> None:
1159        super().__init__()
1160        self.expr = expr
1161        self.msg = msg
1162
1163    def accept(self, visitor: StatementVisitor[T]) -> T:
1164        return visitor.visit_assert_stmt(self)
1165
1166
1167class DelStmt(Statement):
1168    expr = None  # type: Lvalue
1169
1170    def __init__(self, expr: Lvalue) -> None:
1171        super().__init__()
1172        self.expr = expr
1173
1174    def accept(self, visitor: StatementVisitor[T]) -> T:
1175        return visitor.visit_del_stmt(self)
1176
1177
1178class BreakStmt(Statement):
1179    def accept(self, visitor: StatementVisitor[T]) -> T:
1180        return visitor.visit_break_stmt(self)
1181
1182
1183class ContinueStmt(Statement):
1184    def accept(self, visitor: StatementVisitor[T]) -> T:
1185        return visitor.visit_continue_stmt(self)
1186
1187
1188class PassStmt(Statement):
1189    def accept(self, visitor: StatementVisitor[T]) -> T:
1190        return visitor.visit_pass_stmt(self)
1191
1192
1193class IfStmt(Statement):
1194    expr = None  # type: List[Expression]
1195    body = None  # type: List[Block]
1196    else_body = None  # type: Optional[Block]
1197
1198    def __init__(self, expr: List[Expression], body: List[Block],
1199                 else_body: Optional[Block]) -> None:
1200        super().__init__()
1201        self.expr = expr
1202        self.body = body
1203        self.else_body = else_body
1204
1205    def accept(self, visitor: StatementVisitor[T]) -> T:
1206        return visitor.visit_if_stmt(self)
1207
1208
1209class RaiseStmt(Statement):
1210    # Plain 'raise' is a valid statement.
1211    expr = None  # type: Optional[Expression]
1212    from_expr = None  # type: Optional[Expression]
1213
1214    def __init__(self, expr: Optional[Expression], from_expr: Optional[Expression]) -> None:
1215        super().__init__()
1216        self.expr = expr
1217        self.from_expr = from_expr
1218
1219    def accept(self, visitor: StatementVisitor[T]) -> T:
1220        return visitor.visit_raise_stmt(self)
1221
1222
1223class TryStmt(Statement):
1224    body = None  # type: Block                # Try body
1225    # Plain 'except:' also possible
1226    types = None  # type: List[Optional[Expression]]    # Except type expressions
1227    vars = None  # type: List[Optional[NameExpr]]     # Except variable names
1228    handlers = None  # type: List[Block]      # Except bodies
1229    else_body = None  # type: Optional[Block]
1230    finally_body = None  # type: Optional[Block]
1231
1232    def __init__(self, body: Block, vars: List['Optional[NameExpr]'],
1233                 types: List[Optional[Expression]],
1234                 handlers: List[Block], else_body: Optional[Block],
1235                 finally_body: Optional[Block]) -> None:
1236        super().__init__()
1237        self.body = body
1238        self.vars = vars
1239        self.types = types
1240        self.handlers = handlers
1241        self.else_body = else_body
1242        self.finally_body = finally_body
1243
1244    def accept(self, visitor: StatementVisitor[T]) -> T:
1245        return visitor.visit_try_stmt(self)
1246
1247
1248class WithStmt(Statement):
1249    expr = None  # type: List[Expression]
1250    target = None  # type: List[Optional[Lvalue]]
1251    # Type given by type comments for target, can be None
1252    unanalyzed_type = None  # type: Optional[mypy.types.Type]
1253    # Semantically analyzed types from type comment (TypeList type expanded)
1254    analyzed_types = None  # type: List[mypy.types.Type]
1255    body = None  # type: Block
1256    is_async = False  # True if `async with ...` (PEP 492, Python 3.5)
1257
1258    def __init__(self, expr: List[Expression], target: List[Optional[Lvalue]],
1259                 body: Block, target_type: 'Optional[mypy.types.Type]' = None) -> None:
1260        super().__init__()
1261        self.expr = expr
1262        self.target = target
1263        self.unanalyzed_type = target_type
1264        self.analyzed_types = []
1265        self.body = body
1266
1267    def accept(self, visitor: StatementVisitor[T]) -> T:
1268        return visitor.visit_with_stmt(self)
1269
1270
1271class PrintStmt(Statement):
1272    """Python 2 print statement"""
1273
1274    args = None  # type: List[Expression]
1275    newline = False
1276    # The file-like target object (given using >>).
1277    target = None  # type: Optional[Expression]
1278
1279    def __init__(self,
1280                 args: List[Expression],
1281                 newline: bool,
1282                 target: Optional[Expression] = None) -> None:
1283        super().__init__()
1284        self.args = args
1285        self.newline = newline
1286        self.target = target
1287
1288    def accept(self, visitor: StatementVisitor[T]) -> T:
1289        return visitor.visit_print_stmt(self)
1290
1291
1292class ExecStmt(Statement):
1293    """Python 2 exec statement"""
1294
1295    expr = None  # type: Expression
1296    globals = None  # type: Optional[Expression]
1297    locals = None  # type: Optional[Expression]
1298
1299    def __init__(self, expr: Expression,
1300                 globals: Optional[Expression],
1301                 locals: Optional[Expression]) -> None:
1302        super().__init__()
1303        self.expr = expr
1304        self.globals = globals
1305        self.locals = locals
1306
1307    def accept(self, visitor: StatementVisitor[T]) -> T:
1308        return visitor.visit_exec_stmt(self)
1309
1310
1311# Expressions
1312
1313
1314class IntExpr(Expression):
1315    """Integer literal"""
1316
1317    value = 0
1318
1319    def __init__(self, value: int) -> None:
1320        super().__init__()
1321        self.value = value
1322
1323    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1324        return visitor.visit_int_expr(self)
1325
1326
1327# How mypy uses StrExpr, BytesExpr, and UnicodeExpr:
1328# In Python 2 mode:
1329# b'x', 'x' -> StrExpr
1330# u'x' -> UnicodeExpr
1331# BytesExpr is unused
1332#
1333# In Python 3 mode:
1334# b'x' -> BytesExpr
1335# 'x', u'x' -> StrExpr
1336# UnicodeExpr is unused
1337
1338class StrExpr(Expression):
1339    """String literal"""
1340
1341    value = ''
1342
1343    # Keeps track of whether this string originated from Python 2 source code vs
1344    # Python 3 source code. We need to keep track of this information so we can
1345    # correctly handle types that have "nested strings". For example, consider this
1346    # type alias, where we have a forward reference to a literal type:
1347    #
1348    #     Alias = List["Literal['foo']"]
1349    #
1350    # When parsing this, we need to know whether the outer string and alias came from
1351    # Python 2 code vs Python 3 code so we can determine whether the inner `Literal['foo']`
1352    # is meant to be `Literal[u'foo']` or `Literal[b'foo']`.
1353    #
1354    # This field keeps track of that information.
1355    from_python_3 = True
1356
1357    def __init__(self, value: str, from_python_3: bool = False) -> None:
1358        super().__init__()
1359        self.value = value
1360        self.from_python_3 = from_python_3
1361
1362    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1363        return visitor.visit_str_expr(self)
1364
1365
1366class BytesExpr(Expression):
1367    """Bytes literal"""
1368
1369    # Note: we deliberately do NOT use bytes here because it ends up
1370    # unnecessarily complicating a lot of the result logic. For example,
1371    # we'd have to worry about converting the bytes into a format we can
1372    # easily serialize/deserialize to and from JSON, would have to worry
1373    # about turning the bytes into a human-readable representation in
1374    # error messages...
1375    #
1376    # It's more convenient to just store the human-readable representation
1377    # from the very start.
1378    value = ''
1379
1380    def __init__(self, value: str) -> None:
1381        super().__init__()
1382        self.value = value
1383
1384    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1385        return visitor.visit_bytes_expr(self)
1386
1387
1388class UnicodeExpr(Expression):
1389    """Unicode literal (Python 2.x)"""
1390
1391    value = ''
1392
1393    def __init__(self, value: str) -> None:
1394        super().__init__()
1395        self.value = value
1396
1397    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1398        return visitor.visit_unicode_expr(self)
1399
1400
1401class FloatExpr(Expression):
1402    """Float literal"""
1403
1404    value = 0.0
1405
1406    def __init__(self, value: float) -> None:
1407        super().__init__()
1408        self.value = value
1409
1410    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1411        return visitor.visit_float_expr(self)
1412
1413
1414class ComplexExpr(Expression):
1415    """Complex literal"""
1416
1417    def __init__(self, value: complex) -> None:
1418        super().__init__()
1419        self.value = value
1420
1421    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1422        return visitor.visit_complex_expr(self)
1423
1424
1425class EllipsisExpr(Expression):
1426    """Ellipsis (...)"""
1427
1428    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1429        return visitor.visit_ellipsis(self)
1430
1431
1432class StarExpr(Expression):
1433    """Star expression"""
1434
1435    expr = None  # type: Expression
1436
1437    def __init__(self, expr: Expression) -> None:
1438        super().__init__()
1439        self.expr = expr
1440
1441        # Whether this starred expression is used in a tuple/list and as lvalue
1442        self.valid = False
1443
1444    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1445        return visitor.visit_star_expr(self)
1446
1447
1448class RefExpr(Expression):
1449    """Abstract base class for name-like constructs"""
1450
1451    __slots__ = ('kind', 'node', 'fullname', 'is_new_def', 'is_inferred_def', 'is_alias_rvalue',
1452                 'type_guard')
1453
1454    def __init__(self) -> None:
1455        super().__init__()
1456        # LDEF/GDEF/MDEF/... (None if not available)
1457        self.kind = None  # type: Optional[int]
1458        # Var, FuncDef or TypeInfo that describes this
1459        self.node = None  # type: Optional[SymbolNode]
1460        # Fully qualified name (or name if not global)
1461        self.fullname = None  # type: Optional[str]
1462        # Does this define a new name?
1463        self.is_new_def = False
1464        # Does this define a new name with inferred type?
1465        #
1466        # For members, after semantic analysis, this does not take base
1467        # classes into consideration at all; the type checker deals with these.
1468        self.is_inferred_def = False
1469        # Is this expression appears as an rvalue of a valid type alias definition?
1470        self.is_alias_rvalue = False
1471        # Cache type guard from callable_type.type_guard
1472        self.type_guard = None  # type: Optional[mypy.types.Type]
1473
1474
1475class NameExpr(RefExpr):
1476    """Name expression
1477
1478    This refers to a local name, global name or a module.
1479    """
1480
1481    __slots__ = ('name', 'is_special_form')
1482
1483    def __init__(self, name: str) -> None:
1484        super().__init__()
1485        self.name = name  # Name referred to (may be qualified)
1486        # Is this a l.h.s. of a special form assignment like typed dict or type variable?
1487        self.is_special_form = False
1488
1489    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1490        return visitor.visit_name_expr(self)
1491
1492    def serialize(self) -> JsonDict:
1493        assert False, "Serializing NameExpr: %s" % (self,)
1494
1495
1496class MemberExpr(RefExpr):
1497    """Member access expression x.y"""
1498
1499    __slots__ = ('expr', 'name', 'def_var')
1500
1501    def __init__(self, expr: Expression, name: str) -> None:
1502        super().__init__()
1503        self.expr = expr
1504        self.name = name
1505        # The variable node related to a definition through 'self.x = <initializer>'.
1506        # The nodes of other kinds of member expressions are resolved during type checking.
1507        self.def_var = None  # type: Optional[Var]
1508
1509    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1510        return visitor.visit_member_expr(self)
1511
1512
1513# Kinds of arguments
1514
1515# Positional argument
1516ARG_POS = 0  # type: Final
1517# Positional, optional argument (functions only, not calls)
1518ARG_OPT = 1  # type: Final
1519# *arg argument
1520ARG_STAR = 2  # type: Final
1521# Keyword argument x=y in call, or keyword-only function arg
1522ARG_NAMED = 3  # type: Final
1523# **arg argument
1524ARG_STAR2 = 4  # type: Final
1525# In an argument list, keyword-only and also optional
1526ARG_NAMED_OPT = 5  # type: Final
1527
1528
1529class CallExpr(Expression):
1530    """Call expression.
1531
1532    This can also represent several special forms that are syntactically calls
1533    such as cast(...) and None  # type: ....
1534    """
1535
1536    __slots__ = ('callee', 'args', 'arg_kinds', 'arg_names', 'analyzed')
1537
1538    def __init__(self,
1539                 callee: Expression,
1540                 args: List[Expression],
1541                 arg_kinds: List[int],
1542                 arg_names: List[Optional[str]],
1543                 analyzed: Optional[Expression] = None) -> None:
1544        super().__init__()
1545        if not arg_names:
1546            arg_names = [None] * len(args)
1547
1548        self.callee = callee
1549        self.args = args
1550        self.arg_kinds = arg_kinds  # ARG_ constants
1551        # Each name can be None if not a keyword argument.
1552        self.arg_names = arg_names  # type: List[Optional[str]]
1553        # If not None, the node that represents the meaning of the CallExpr. For
1554        # cast(...) this is a CastExpr.
1555        self.analyzed = analyzed
1556
1557    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1558        return visitor.visit_call_expr(self)
1559
1560
1561class YieldFromExpr(Expression):
1562    expr = None  # type: Expression
1563
1564    def __init__(self, expr: Expression) -> None:
1565        super().__init__()
1566        self.expr = expr
1567
1568    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1569        return visitor.visit_yield_from_expr(self)
1570
1571
1572class YieldExpr(Expression):
1573    expr = None  # type: Optional[Expression]
1574
1575    def __init__(self, expr: Optional[Expression]) -> None:
1576        super().__init__()
1577        self.expr = expr
1578
1579    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1580        return visitor.visit_yield_expr(self)
1581
1582
1583class IndexExpr(Expression):
1584    """Index expression x[y].
1585
1586    Also wraps type application such as List[int] as a special form.
1587    """
1588
1589    base = None  # type: Expression
1590    index = None  # type: Expression
1591    # Inferred __getitem__ method type
1592    method_type = None  # type: Optional[mypy.types.Type]
1593    # If not None, this is actually semantically a type application
1594    # Class[type, ...] or a type alias initializer.
1595    analyzed = None  # type: Union[TypeApplication, TypeAliasExpr, None]
1596
1597    def __init__(self, base: Expression, index: Expression) -> None:
1598        super().__init__()
1599        self.base = base
1600        self.index = index
1601        self.analyzed = None
1602
1603    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1604        return visitor.visit_index_expr(self)
1605
1606
1607class UnaryExpr(Expression):
1608    """Unary operation"""
1609
1610    op = ''
1611    expr = None  # type: Expression
1612    # Inferred operator method type
1613    method_type = None  # type: Optional[mypy.types.Type]
1614
1615    def __init__(self, op: str, expr: Expression) -> None:
1616        super().__init__()
1617        self.op = op
1618        self.expr = expr
1619
1620    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1621        return visitor.visit_unary_expr(self)
1622
1623
1624class AssignmentExpr(Expression):
1625    """Assignment expressions in Python 3.8+, like "a := 2"."""
1626    def __init__(self, target: Expression, value: Expression) -> None:
1627        super().__init__()
1628        self.target = target
1629        self.value = value
1630
1631    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1632        return visitor.visit_assignment_expr(self)
1633
1634
1635# Map from binary operator id to related method name (in Python 3).
1636op_methods = {
1637    '+': '__add__',
1638    '-': '__sub__',
1639    '*': '__mul__',
1640    '/': '__truediv__',
1641    '%': '__mod__',
1642    'divmod': '__divmod__',
1643    '//': '__floordiv__',
1644    '**': '__pow__',
1645    '@': '__matmul__',
1646    '&': '__and__',
1647    '|': '__or__',
1648    '^': '__xor__',
1649    '<<': '__lshift__',
1650    '>>': '__rshift__',
1651    '==': '__eq__',
1652    '!=': '__ne__',
1653    '<': '__lt__',
1654    '>=': '__ge__',
1655    '>': '__gt__',
1656    '<=': '__le__',
1657    'in': '__contains__',
1658}  # type: Final
1659
1660op_methods_to_symbols = {v: k for (k, v) in op_methods.items()}  # type: Final
1661op_methods_to_symbols['__div__'] = '/'
1662
1663comparison_fallback_method = '__cmp__'  # type: Final
1664ops_falling_back_to_cmp = {'__ne__', '__eq__',
1665                           '__lt__', '__le__',
1666                           '__gt__', '__ge__'}  # type: Final
1667
1668
1669ops_with_inplace_method = {
1670    '+', '-', '*', '/', '%', '//', '**', '@', '&', '|', '^', '<<', '>>'}  # type: Final
1671
1672inplace_operator_methods = set(
1673    '__i' + op_methods[op][2:] for op in ops_with_inplace_method)  # type: Final
1674
1675reverse_op_methods = {
1676    '__add__': '__radd__',
1677    '__sub__': '__rsub__',
1678    '__mul__': '__rmul__',
1679    '__truediv__': '__rtruediv__',
1680    '__mod__': '__rmod__',
1681    '__divmod__': '__rdivmod__',
1682    '__floordiv__': '__rfloordiv__',
1683    '__pow__': '__rpow__',
1684    '__matmul__': '__rmatmul__',
1685    '__and__': '__rand__',
1686    '__or__': '__ror__',
1687    '__xor__': '__rxor__',
1688    '__lshift__': '__rlshift__',
1689    '__rshift__': '__rrshift__',
1690    '__eq__': '__eq__',
1691    '__ne__': '__ne__',
1692    '__lt__': '__gt__',
1693    '__ge__': '__le__',
1694    '__gt__': '__lt__',
1695    '__le__': '__ge__',
1696}  # type: Final
1697
1698# Suppose we have some class A. When we do A() + A(), Python will only check
1699# the output of A().__add__(A()) and skip calling the __radd__ method entirely.
1700# This shortcut is used only for the following methods:
1701op_methods_that_shortcut = {
1702    '__add__',
1703    '__sub__',
1704    '__mul__',
1705    '__div__',
1706    '__truediv__',
1707    '__mod__',
1708    '__divmod__',
1709    '__floordiv__',
1710    '__pow__',
1711    '__matmul__',
1712    '__and__',
1713    '__or__',
1714    '__xor__',
1715    '__lshift__',
1716    '__rshift__',
1717}  # type: Final
1718
1719normal_from_reverse_op = dict((m, n) for n, m in reverse_op_methods.items())  # type: Final
1720reverse_op_method_set = set(reverse_op_methods.values())  # type: Final
1721
1722unary_op_methods = {
1723    '-': '__neg__',
1724    '+': '__pos__',
1725    '~': '__invert__',
1726}  # type: Final
1727
1728
1729class OpExpr(Expression):
1730    """Binary operation (other than . or [] or comparison operators,
1731    which have specific nodes)."""
1732
1733    op = ''
1734    left = None  # type: Expression
1735    right = None  # type: Expression
1736    # Inferred type for the operator method type (when relevant).
1737    method_type = None  # type: Optional[mypy.types.Type]
1738    # Is the right side going to be evaluated every time?
1739    right_always = False
1740    # Is the right side unreachable?
1741    right_unreachable = False
1742
1743    def __init__(self, op: str, left: Expression, right: Expression) -> None:
1744        super().__init__()
1745        self.op = op
1746        self.left = left
1747        self.right = right
1748
1749    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1750        return visitor.visit_op_expr(self)
1751
1752
1753class ComparisonExpr(Expression):
1754    """Comparison expression (e.g. a < b > c < d)."""
1755
1756    operators = None  # type: List[str]
1757    operands = None  # type: List[Expression]
1758    # Inferred type for the operator methods (when relevant; None for 'is').
1759    method_types = None  # type: List[Optional[mypy.types.Type]]
1760
1761    def __init__(self, operators: List[str], operands: List[Expression]) -> None:
1762        super().__init__()
1763        self.operators = operators
1764        self.operands = operands
1765        self.method_types = []
1766
1767    def pairwise(self) -> Iterator[Tuple[str, Expression, Expression]]:
1768        """If this comparison expr is "a < b is c == d", yields the sequence
1769        ("<", a, b), ("is", b, c), ("==", c, d)
1770        """
1771        for i, operator in enumerate(self.operators):
1772            yield operator, self.operands[i], self.operands[i + 1]
1773
1774    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1775        return visitor.visit_comparison_expr(self)
1776
1777
1778class SliceExpr(Expression):
1779    """Slice expression (e.g. 'x:y', 'x:', '::2' or ':').
1780
1781    This is only valid as index in index expressions.
1782    """
1783
1784    begin_index = None  # type: Optional[Expression]
1785    end_index = None  # type: Optional[Expression]
1786    stride = None  # type: Optional[Expression]
1787
1788    def __init__(self, begin_index: Optional[Expression],
1789                 end_index: Optional[Expression],
1790                 stride: Optional[Expression]) -> None:
1791        super().__init__()
1792        self.begin_index = begin_index
1793        self.end_index = end_index
1794        self.stride = stride
1795
1796    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1797        return visitor.visit_slice_expr(self)
1798
1799
1800class CastExpr(Expression):
1801    """Cast expression cast(type, expr)."""
1802
1803    expr = None  # type: Expression
1804    type = None  # type: mypy.types.Type
1805
1806    def __init__(self, expr: Expression, typ: 'mypy.types.Type') -> None:
1807        super().__init__()
1808        self.expr = expr
1809        self.type = typ
1810
1811    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1812        return visitor.visit_cast_expr(self)
1813
1814
1815class RevealExpr(Expression):
1816    """Reveal type expression reveal_type(expr) or reveal_locals() expression."""
1817
1818    expr = None  # type: Optional[Expression]
1819    kind = 0  # type: int
1820    local_nodes = None  # type: Optional[List[Var]]
1821
1822    def __init__(
1823            self, kind: int,
1824            expr: Optional[Expression] = None,
1825            local_nodes: 'Optional[List[Var]]' = None) -> None:
1826        super().__init__()
1827        self.expr = expr
1828        self.kind = kind
1829        self.local_nodes = local_nodes
1830
1831    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1832        return visitor.visit_reveal_expr(self)
1833
1834
1835class SuperExpr(Expression):
1836    """Expression super().name"""
1837
1838    name = ''
1839    info = None  # type: Optional[TypeInfo]  # Type that contains this super expression
1840    call = None  # type: CallExpr  # The expression super(...)
1841
1842    def __init__(self, name: str, call: CallExpr) -> None:
1843        super().__init__()
1844        self.name = name
1845        self.call = call
1846
1847    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1848        return visitor.visit_super_expr(self)
1849
1850
1851class LambdaExpr(FuncItem, Expression):
1852    """Lambda expression"""
1853
1854    @property
1855    def name(self) -> str:
1856        return '<lambda>'
1857
1858    def expr(self) -> Expression:
1859        """Return the expression (the body) of the lambda."""
1860        ret = cast(ReturnStmt, self.body.body[-1])
1861        expr = ret.expr
1862        assert expr is not None  # lambda can't have empty body
1863        return expr
1864
1865    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1866        return visitor.visit_lambda_expr(self)
1867
1868    def is_dynamic(self) -> bool:
1869        return False
1870
1871
1872class ListExpr(Expression):
1873    """List literal expression [...]."""
1874
1875    items = None  # type: List[Expression]
1876
1877    def __init__(self, items: List[Expression]) -> None:
1878        super().__init__()
1879        self.items = items
1880
1881    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1882        return visitor.visit_list_expr(self)
1883
1884
1885class DictExpr(Expression):
1886    """Dictionary literal expression {key: value, ...}."""
1887
1888    items = None  # type: List[Tuple[Optional[Expression], Expression]]
1889
1890    def __init__(self, items: List[Tuple[Optional[Expression], Expression]]) -> None:
1891        super().__init__()
1892        self.items = items
1893
1894    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1895        return visitor.visit_dict_expr(self)
1896
1897
1898class TupleExpr(Expression):
1899    """Tuple literal expression (..., ...)
1900
1901    Also lvalue sequences (..., ...) and [..., ...]"""
1902
1903    items = None  # type: List[Expression]
1904
1905    def __init__(self, items: List[Expression]) -> None:
1906        super().__init__()
1907        self.items = items
1908
1909    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1910        return visitor.visit_tuple_expr(self)
1911
1912
1913class SetExpr(Expression):
1914    """Set literal expression {value, ...}."""
1915
1916    items = None  # type: List[Expression]
1917
1918    def __init__(self, items: List[Expression]) -> None:
1919        super().__init__()
1920        self.items = items
1921
1922    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1923        return visitor.visit_set_expr(self)
1924
1925
1926class GeneratorExpr(Expression):
1927    """Generator expression ... for ... in ... [ for ...  in ... ] [ if ... ]."""
1928
1929    left_expr = None  # type: Expression
1930    sequences = None  # type: List[Expression]
1931    condlists = None  # type: List[List[Expression]]
1932    is_async = None  # type: List[bool]
1933    indices = None  # type: List[Lvalue]
1934
1935    def __init__(self, left_expr: Expression, indices: List[Lvalue],
1936                 sequences: List[Expression], condlists: List[List[Expression]],
1937                 is_async: List[bool]) -> None:
1938        super().__init__()
1939        self.left_expr = left_expr
1940        self.sequences = sequences
1941        self.condlists = condlists
1942        self.indices = indices
1943        self.is_async = is_async
1944
1945    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1946        return visitor.visit_generator_expr(self)
1947
1948
1949class ListComprehension(Expression):
1950    """List comprehension (e.g. [x + 1 for x in a])"""
1951
1952    generator = None  # type: GeneratorExpr
1953
1954    def __init__(self, generator: GeneratorExpr) -> None:
1955        super().__init__()
1956        self.generator = generator
1957
1958    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1959        return visitor.visit_list_comprehension(self)
1960
1961
1962class SetComprehension(Expression):
1963    """Set comprehension (e.g. {x + 1 for x in a})"""
1964
1965    generator = None  # type: GeneratorExpr
1966
1967    def __init__(self, generator: GeneratorExpr) -> None:
1968        super().__init__()
1969        self.generator = generator
1970
1971    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1972        return visitor.visit_set_comprehension(self)
1973
1974
1975class DictionaryComprehension(Expression):
1976    """Dictionary comprehension (e.g. {k: v for k, v in a}"""
1977
1978    key = None  # type: Expression
1979    value = None  # type: Expression
1980    sequences = None  # type: List[Expression]
1981    condlists = None  # type: List[List[Expression]]
1982    is_async = None  # type: List[bool]
1983    indices = None  # type: List[Lvalue]
1984
1985    def __init__(self, key: Expression, value: Expression, indices: List[Lvalue],
1986                 sequences: List[Expression], condlists: List[List[Expression]],
1987                 is_async: List[bool]) -> None:
1988        super().__init__()
1989        self.key = key
1990        self.value = value
1991        self.sequences = sequences
1992        self.condlists = condlists
1993        self.indices = indices
1994        self.is_async = is_async
1995
1996    def accept(self, visitor: ExpressionVisitor[T]) -> T:
1997        return visitor.visit_dictionary_comprehension(self)
1998
1999
2000class ConditionalExpr(Expression):
2001    """Conditional expression (e.g. x if y else z)"""
2002
2003    cond = None  # type: Expression
2004    if_expr = None  # type: Expression
2005    else_expr = None  # type: Expression
2006
2007    def __init__(self, cond: Expression, if_expr: Expression, else_expr: Expression) -> None:
2008        super().__init__()
2009        self.cond = cond
2010        self.if_expr = if_expr
2011        self.else_expr = else_expr
2012
2013    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2014        return visitor.visit_conditional_expr(self)
2015
2016
2017class BackquoteExpr(Expression):
2018    """Python 2 expression `...`."""
2019
2020    expr = None  # type: Expression
2021
2022    def __init__(self, expr: Expression) -> None:
2023        super().__init__()
2024        self.expr = expr
2025
2026    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2027        return visitor.visit_backquote_expr(self)
2028
2029
2030class TypeApplication(Expression):
2031    """Type application expr[type, ...]"""
2032
2033    expr = None  # type: Expression
2034    types = None  # type: List[mypy.types.Type]
2035
2036    def __init__(self, expr: Expression, types: List['mypy.types.Type']) -> None:
2037        super().__init__()
2038        self.expr = expr
2039        self.types = types
2040
2041    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2042        return visitor.visit_type_application(self)
2043
2044
2045# Variance of a type variable. For example, T in the definition of
2046# List[T] is invariant, so List[int] is not a subtype of List[object],
2047# and also List[object] is not a subtype of List[int].
2048#
2049# The T in Iterable[T] is covariant, so Iterable[int] is a subtype of
2050# Iterable[object], but not vice versa.
2051#
2052# If T is contravariant in Foo[T], Foo[object] is a subtype of
2053# Foo[int], but not vice versa.
2054INVARIANT = 0  # type: Final
2055COVARIANT = 1  # type: Final
2056CONTRAVARIANT = 2  # type: Final
2057
2058
2059class TypeVarLikeExpr(SymbolNode, Expression):
2060    """Base class for TypeVarExpr and ParamSpecExpr."""
2061    _name = ''
2062    _fullname = ''
2063    # Upper bound: only subtypes of upper_bound are valid as values. By default
2064    # this is 'object', meaning no restriction.
2065    upper_bound = None  # type: mypy.types.Type
2066    # Variance of the type variable. Invariant is the default.
2067    # TypeVar(..., covariant=True) defines a covariant type variable.
2068    # TypeVar(..., contravariant=True) defines a contravariant type
2069    # variable.
2070    variance = INVARIANT
2071
2072    def __init__(
2073        self, name: str, fullname: str, upper_bound: 'mypy.types.Type', variance: int = INVARIANT
2074    ) -> None:
2075        super().__init__()
2076        self._name = name
2077        self._fullname = fullname
2078        self.upper_bound = upper_bound
2079        self.variance = variance
2080
2081    @property
2082    def name(self) -> str:
2083        return self._name
2084
2085    @property
2086    def fullname(self) -> str:
2087        return self._fullname
2088
2089
2090class TypeVarExpr(TypeVarLikeExpr):
2091    """Type variable expression TypeVar(...).
2092
2093    This is also used to represent type variables in symbol tables.
2094
2095    A type variable is not valid as a type unless bound in a TypeVarLikeScope.
2096    That happens within:
2097
2098     1. a generic class that uses the type variable as a type argument or
2099     2. a generic function that refers to the type variable in its signature.
2100    """
2101    # Value restriction: only types in the list are valid as values. If the
2102    # list is empty, there is no restriction.
2103    values = None  # type: List[mypy.types.Type]
2104
2105    def __init__(self, name: str, fullname: str,
2106                 values: List['mypy.types.Type'],
2107                 upper_bound: 'mypy.types.Type',
2108                 variance: int = INVARIANT) -> None:
2109        super().__init__(name, fullname, upper_bound, variance)
2110        self.values = values
2111
2112    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2113        return visitor.visit_type_var_expr(self)
2114
2115    def serialize(self) -> JsonDict:
2116        return {'.class': 'TypeVarExpr',
2117                'name': self._name,
2118                'fullname': self._fullname,
2119                'values': [t.serialize() for t in self.values],
2120                'upper_bound': self.upper_bound.serialize(),
2121                'variance': self.variance,
2122                }
2123
2124    @classmethod
2125    def deserialize(cls, data: JsonDict) -> 'TypeVarExpr':
2126        assert data['.class'] == 'TypeVarExpr'
2127        return TypeVarExpr(data['name'],
2128                           data['fullname'],
2129                           [mypy.types.deserialize_type(v) for v in data['values']],
2130                           mypy.types.deserialize_type(data['upper_bound']),
2131                           data['variance'])
2132
2133
2134class ParamSpecExpr(TypeVarLikeExpr):
2135    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2136        return visitor.visit_paramspec_expr(self)
2137
2138    def serialize(self) -> JsonDict:
2139        return {
2140            '.class': 'ParamSpecExpr',
2141            'name': self._name,
2142            'fullname': self._fullname,
2143            'upper_bound': self.upper_bound.serialize(),
2144            'variance': self.variance,
2145        }
2146
2147    @classmethod
2148    def deserialize(cls, data: JsonDict) -> 'ParamSpecExpr':
2149        assert data['.class'] == 'ParamSpecExpr'
2150        return ParamSpecExpr(
2151            data['name'],
2152            data['fullname'],
2153            mypy.types.deserialize_type(data['upper_bound']),
2154            data['variance']
2155        )
2156
2157
2158class TypeAliasExpr(Expression):
2159    """Type alias expression (rvalue)."""
2160
2161    # The target type.
2162    type = None  # type: mypy.types.Type
2163    # Names of unbound type variables used to define the alias
2164    tvars = None  # type: List[str]
2165    # Whether this alias was defined in bare form. Used to distinguish
2166    # between
2167    #     A = List
2168    # and
2169    #     A = List[Any]
2170    no_args = False  # type: bool
2171
2172    def __init__(self, node: 'TypeAlias') -> None:
2173        super().__init__()
2174        self.type = node.target
2175        self.tvars = node.alias_tvars
2176        self.no_args = node.no_args
2177        self.node = node
2178
2179    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2180        return visitor.visit_type_alias_expr(self)
2181
2182
2183class NamedTupleExpr(Expression):
2184    """Named tuple expression namedtuple(...) or NamedTuple(...)."""
2185
2186    # The class representation of this named tuple (its tuple_type attribute contains
2187    # the tuple item types)
2188    info = None  # type: TypeInfo
2189    is_typed = False  # whether this class was created with typing.NamedTuple
2190
2191    def __init__(self, info: 'TypeInfo', is_typed: bool = False) -> None:
2192        super().__init__()
2193        self.info = info
2194        self.is_typed = is_typed
2195
2196    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2197        return visitor.visit_namedtuple_expr(self)
2198
2199
2200class TypedDictExpr(Expression):
2201    """Typed dict expression TypedDict(...)."""
2202
2203    # The class representation of this typed dict
2204    info = None  # type: TypeInfo
2205
2206    def __init__(self, info: 'TypeInfo') -> None:
2207        super().__init__()
2208        self.info = info
2209
2210    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2211        return visitor.visit_typeddict_expr(self)
2212
2213
2214class EnumCallExpr(Expression):
2215    """Named tuple expression Enum('name', 'val1 val2 ...')."""
2216
2217    # The class representation of this enumerated type
2218    info = None  # type: TypeInfo
2219    # The item names (for debugging)
2220    items = None  # type: List[str]
2221    values = None  # type: List[Optional[Expression]]
2222
2223    def __init__(self, info: 'TypeInfo', items: List[str],
2224                 values: List[Optional[Expression]]) -> None:
2225        super().__init__()
2226        self.info = info
2227        self.items = items
2228        self.values = values
2229
2230    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2231        return visitor.visit_enum_call_expr(self)
2232
2233
2234class PromoteExpr(Expression):
2235    """Ducktype class decorator expression _promote(...)."""
2236
2237    type = None  # type: mypy.types.Type
2238
2239    def __init__(self, type: 'mypy.types.Type') -> None:
2240        super().__init__()
2241        self.type = type
2242
2243    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2244        return visitor.visit__promote_expr(self)
2245
2246
2247class NewTypeExpr(Expression):
2248    """NewType expression NewType(...)."""
2249    name = None  # type: str
2250    # The base type (the second argument to NewType)
2251    old_type = None  # type: Optional[mypy.types.Type]
2252    # The synthesized class representing the new type (inherits old_type)
2253    info = None  # type: Optional[TypeInfo]
2254
2255    def __init__(self, name: str, old_type: 'Optional[mypy.types.Type]', line: int,
2256                 column: int) -> None:
2257        super().__init__()
2258        self.name = name
2259        self.old_type = old_type
2260        self.line = line
2261        self.column = column
2262
2263    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2264        return visitor.visit_newtype_expr(self)
2265
2266
2267class AwaitExpr(Expression):
2268    """Await expression (await ...)."""
2269
2270    expr = None  # type: Expression
2271
2272    def __init__(self, expr: Expression) -> None:
2273        super().__init__()
2274        self.expr = expr
2275
2276    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2277        return visitor.visit_await_expr(self)
2278
2279
2280# Constants
2281
2282
2283class TempNode(Expression):
2284    """Temporary dummy node used during type checking.
2285
2286    This node is not present in the original program; it is just an artifact
2287    of the type checker implementation. It only represents an opaque node with
2288    some fixed type.
2289    """
2290
2291    type = None  # type: mypy.types.Type
2292    # Is this TempNode used to indicate absence of a right hand side in an annotated assignment?
2293    # (e.g. for 'x: int' the rvalue is TempNode(AnyType(TypeOfAny.special_form), no_rhs=True))
2294    no_rhs = False  # type: bool
2295
2296    def __init__(self,
2297                 typ: 'mypy.types.Type',
2298                 no_rhs: bool = False,
2299                 *,
2300                 context: Optional[Context] = None) -> None:
2301        """Construct a dummy node; optionally borrow line/column from context object."""
2302        super().__init__()
2303        self.type = typ
2304        self.no_rhs = no_rhs
2305        if context is not None:
2306            self.line = context.line
2307            self.column = context.column
2308
2309    def __repr__(self) -> str:
2310        return 'TempNode:%d(%s)' % (self.line, str(self.type))
2311
2312    def accept(self, visitor: ExpressionVisitor[T]) -> T:
2313        return visitor.visit_temp_node(self)
2314
2315
2316class TypeInfo(SymbolNode):
2317    """The type structure of a single class.
2318
2319    Each TypeInfo corresponds one-to-one to a ClassDef, which
2320    represents the AST of the class.
2321
2322    In type-theory terms, this is a "type constructor", and if the
2323    class is generic then it will be a type constructor of higher kind.
2324    Where the class is used in an actual type, it's in the form of an
2325    Instance, which amounts to a type application of the tycon to
2326    the appropriate number of arguments.
2327    """
2328
2329    _fullname = None  # type: Bogus[str]          # Fully qualified name
2330    # Fully qualified name for the module this type was defined in. This
2331    # information is also in the fullname, but is harder to extract in the
2332    # case of nested class definitions.
2333    module_name = None  # type: str
2334    defn = None  # type: ClassDef          # Corresponding ClassDef
2335    # Method Resolution Order: the order of looking up attributes. The first
2336    # value always to refers to this class.
2337    mro = None  # type: List[TypeInfo]
2338    # Used to stash the names of the mro classes temporarily between
2339    # deserialization and fixup. See deserialize() for why.
2340    _mro_refs = None  # type: Optional[List[str]]
2341    bad_mro = False  # Could not construct full MRO
2342
2343    declared_metaclass = None  # type: Optional[mypy.types.Instance]
2344    metaclass_type = None  # type: Optional[mypy.types.Instance]
2345
2346    names = None  # type: SymbolTable      # Names defined directly in this type
2347    is_abstract = False                    # Does the class have any abstract attributes?
2348    is_protocol = False                    # Is this a protocol class?
2349    runtime_protocol = False               # Does this protocol support isinstance checks?
2350    abstract_attributes = None  # type: List[str]
2351
2352    # The attributes 'assuming' and 'assuming_proper' represent structural subtype matrices.
2353    #
2354    # In languages with structural subtyping, one can keep a global subtype matrix like this:
2355    #   . A B C .
2356    #   A 1 0 0
2357    #   B 1 1 1
2358    #   C 1 0 1
2359    #   .
2360    # where 1 indicates that the type in corresponding row is a subtype of the type
2361    # in corresponding column. This matrix typically starts filled with all 1's and
2362    # a typechecker tries to "disprove" every subtyping relation using atomic (or nominal) types.
2363    # However, we don't want to keep this huge global state. Instead, we keep the subtype
2364    # information in the form of list of pairs (subtype, supertype) shared by all 'Instance's
2365    # with given supertype's TypeInfo. When we enter a subtype check we push a pair in this list
2366    # thus assuming that we started with 1 in corresponding matrix element. Such algorithm allows
2367    # to treat recursive and mutually recursive protocols and other kinds of complex situations.
2368    #
2369    # If concurrent/parallel type checking will be added in future,
2370    # then there should be one matrix per thread/process to avoid false negatives
2371    # during the type checking phase.
2372    assuming = None  # type: List[Tuple[mypy.types.Instance, mypy.types.Instance]]
2373    assuming_proper = None  # type: List[Tuple[mypy.types.Instance, mypy.types.Instance]]
2374    # Ditto for temporary 'inferring' stack of recursive constraint inference.
2375    # It contains Instance's of protocol types that appeared as an argument to
2376    # constraints.infer_constraints(). We need 'inferring' to avoid infinite recursion for
2377    # recursive and mutually recursive protocols.
2378    #
2379    # We make 'assuming' and 'inferring' attributes here instead of passing they as kwargs,
2380    # since this would require to pass them in many dozens of calls. In particular,
2381    # there is a dependency infer_constraint -> is_subtype -> is_callable_subtype ->
2382    # -> infer_constraints.
2383    inferring = None  # type: List[mypy.types.Instance]
2384    # 'inferring' and 'assuming' can't be made sets, since we need to use
2385    # is_same_type to correctly treat unions.
2386
2387    # Classes inheriting from Enum shadow their true members with a __getattr__, so we
2388    # have to treat them as a special case.
2389    is_enum = False
2390    # If true, any unknown attributes should have type 'Any' instead
2391    # of generating a type error.  This would be true if there is a
2392    # base class with type 'Any', but other use cases may be
2393    # possible. This is similar to having __getattr__ that returns Any
2394    # (and __setattr__), but without the __getattr__ method.
2395    fallback_to_any = False
2396
2397    # Information related to type annotations.
2398
2399    # Generic type variable names (full names)
2400    type_vars = None  # type: List[str]
2401
2402    # Direct base classes.
2403    bases = None  # type: List[mypy.types.Instance]
2404
2405    # Another type which this type will be treated as a subtype of,
2406    # even though it's not a subclass in Python.  The non-standard
2407    # `@_promote` decorator introduces this, and there are also
2408    # several builtin examples, in particular `int` -> `float`.
2409    _promote = None  # type: Optional[mypy.types.Type]
2410
2411    # Representation of a Tuple[...] base class, if the class has any
2412    # (e.g., for named tuples). If this is not None, the actual Type
2413    # object used for this class is not an Instance but a TupleType;
2414    # the corresponding Instance is set as the fallback type of the
2415    # tuple type.
2416    tuple_type = None  # type: Optional[mypy.types.TupleType]
2417
2418    # Is this a named tuple type?
2419    is_named_tuple = False
2420
2421    # If this class is defined by the TypedDict type constructor,
2422    # then this is not None.
2423    typeddict_type = None  # type: Optional[mypy.types.TypedDictType]
2424
2425    # Is this a newtype type?
2426    is_newtype = False
2427
2428    # Is this a synthesized intersection type?
2429    is_intersection = False
2430
2431    # This is a dictionary that will be serialized and un-serialized as is.
2432    # It is useful for plugins to add their data to save in the cache.
2433    metadata = None  # type: Dict[str, JsonDict]
2434
2435    FLAGS = [
2436        'is_abstract', 'is_enum', 'fallback_to_any', 'is_named_tuple',
2437        'is_newtype', 'is_protocol', 'runtime_protocol', 'is_final',
2438        'is_intersection',
2439    ]  # type: Final
2440
2441    def __init__(self, names: 'SymbolTable', defn: ClassDef, module_name: str) -> None:
2442        """Initialize a TypeInfo."""
2443        super().__init__()
2444        self.names = names
2445        self.defn = defn
2446        self.module_name = module_name
2447        self.type_vars = []
2448        self.bases = []
2449        self.mro = []
2450        self._fullname = defn.fullname
2451        self.is_abstract = False
2452        self.abstract_attributes = []
2453        self.assuming = []
2454        self.assuming_proper = []
2455        self.inferring = []
2456        self.add_type_vars()
2457        self.metadata = {}
2458        self.is_final = False
2459
2460    def add_type_vars(self) -> None:
2461        if self.defn.type_vars:
2462            for vd in self.defn.type_vars:
2463                self.type_vars.append(vd.fullname)
2464
2465    @property
2466    def name(self) -> str:
2467        """Short name."""
2468        return self.defn.name
2469
2470    @property
2471    def fullname(self) -> Bogus[str]:
2472        return self._fullname
2473
2474    def is_generic(self) -> bool:
2475        """Is the type generic (i.e. does it have type variables)?"""
2476        return len(self.type_vars) > 0
2477
2478    def get(self, name: str) -> 'Optional[SymbolTableNode]':
2479        for cls in self.mro:
2480            n = cls.names.get(name)
2481            if n:
2482                return n
2483        return None
2484
2485    def get_containing_type_info(self, name: str) -> 'Optional[TypeInfo]':
2486        for cls in self.mro:
2487            if name in cls.names:
2488                return cls
2489        return None
2490
2491    @property
2492    def protocol_members(self) -> List[str]:
2493        # Protocol members are names of all attributes/methods defined in a protocol
2494        # and in all its supertypes (except for 'object').
2495        members = set()  # type: Set[str]
2496        assert self.mro, "This property can be only accessed after MRO is (re-)calculated"
2497        for base in self.mro[:-1]:  # we skip "object" since everyone implements it
2498            if base.is_protocol:
2499                for name in base.names:
2500                    members.add(name)
2501        return sorted(list(members))
2502
2503    def __getitem__(self, name: str) -> 'SymbolTableNode':
2504        n = self.get(name)
2505        if n:
2506            return n
2507        else:
2508            raise KeyError(name)
2509
2510    def __repr__(self) -> str:
2511        return '<TypeInfo %s>' % self.fullname
2512
2513    def __bool__(self) -> bool:
2514        # We defined this here instead of just overriding it in
2515        # FakeInfo so that mypyc can generate a direct call instead of
2516        # using the generic bool handling.
2517        return not isinstance(self, FakeInfo)
2518
2519    def has_readable_member(self, name: str) -> bool:
2520        return self.get(name) is not None
2521
2522    def get_method(self, name: str) -> Optional[FuncBase]:
2523        for cls in self.mro:
2524            if name in cls.names:
2525                node = cls.names[name].node
2526                if isinstance(node, FuncBase):
2527                    return node
2528                else:
2529                    return None
2530        return None
2531
2532    def calculate_metaclass_type(self) -> 'Optional[mypy.types.Instance]':
2533        declared = self.declared_metaclass
2534        if declared is not None and not declared.type.has_base('builtins.type'):
2535            return declared
2536        if self._fullname == 'builtins.type':
2537            return mypy.types.Instance(self, [])
2538        candidates = [s.declared_metaclass
2539                      for s in self.mro
2540                      if s.declared_metaclass is not None
2541                      and s.declared_metaclass.type is not None]
2542        for c in candidates:
2543            if all(other.type in c.type.mro for other in candidates):
2544                return c
2545        return None
2546
2547    def is_metaclass(self) -> bool:
2548        return (self.has_base('builtins.type') or self.fullname == 'abc.ABCMeta' or
2549                self.fallback_to_any)
2550
2551    def has_base(self, fullname: str) -> bool:
2552        """Return True if type has a base type with the specified name.
2553
2554        This can be either via extension or via implementation.
2555        """
2556        for cls in self.mro:
2557            if cls.fullname == fullname:
2558                return True
2559        return False
2560
2561    def direct_base_classes(self) -> 'List[TypeInfo]':
2562        """Return a direct base classes.
2563
2564        Omit base classes of other base classes.
2565        """
2566        return [base.type for base in self.bases]
2567
2568    def __str__(self) -> str:
2569        """Return a string representation of the type.
2570
2571        This includes the most important information about the type.
2572        """
2573        return self.dump()
2574
2575    def dump(self,
2576             str_conv: 'Optional[mypy.strconv.StrConv]' = None,
2577             type_str_conv: 'Optional[mypy.types.TypeStrVisitor]' = None) -> str:
2578        """Return a string dump of the contents of the TypeInfo."""
2579        if not str_conv:
2580            str_conv = mypy.strconv.StrConv()
2581        base = ''  # type: str
2582
2583        def type_str(typ: 'mypy.types.Type') -> str:
2584            if type_str_conv:
2585                return typ.accept(type_str_conv)
2586            return str(typ)
2587
2588        head = 'TypeInfo' + str_conv.format_id(self)
2589        if self.bases:
2590            base = 'Bases({})'.format(', '.join(type_str(base)
2591                                                for base in self.bases))
2592        mro = 'Mro({})'.format(', '.join(item.fullname + str_conv.format_id(item)
2593                                         for item in self.mro))
2594        names = []
2595        for name in sorted(self.names):
2596            description = name + str_conv.format_id(self.names[name].node)
2597            node = self.names[name].node
2598            if isinstance(node, Var) and node.type:
2599                description += ' ({})'.format(type_str(node.type))
2600            names.append(description)
2601        items = [
2602            'Name({})'.format(self.fullname),
2603            base,
2604            mro,
2605            ('Names', names),
2606        ]
2607        if self.declared_metaclass:
2608            items.append('DeclaredMetaclass({})'.format(type_str(self.declared_metaclass)))
2609        if self.metaclass_type:
2610            items.append('MetaclassType({})'.format(type_str(self.metaclass_type)))
2611        return mypy.strconv.dump_tagged(
2612            items,
2613            head,
2614            str_conv=str_conv)
2615
2616    def serialize(self) -> JsonDict:
2617        # NOTE: This is where all ClassDefs originate, so there shouldn't be duplicates.
2618        data = {'.class': 'TypeInfo',
2619                'module_name': self.module_name,
2620                'fullname': self.fullname,
2621                'names': self.names.serialize(self.fullname),
2622                'defn': self.defn.serialize(),
2623                'abstract_attributes': self.abstract_attributes,
2624                'type_vars': self.type_vars,
2625                'bases': [b.serialize() for b in self.bases],
2626                'mro': [c.fullname for c in self.mro],
2627                '_promote': None if self._promote is None else self._promote.serialize(),
2628                'declared_metaclass': (None if self.declared_metaclass is None
2629                                       else self.declared_metaclass.serialize()),
2630                'metaclass_type':
2631                    None if self.metaclass_type is None else self.metaclass_type.serialize(),
2632                'tuple_type': None if self.tuple_type is None else self.tuple_type.serialize(),
2633                'typeddict_type':
2634                    None if self.typeddict_type is None else self.typeddict_type.serialize(),
2635                'flags': get_flags(self, TypeInfo.FLAGS),
2636                'metadata': self.metadata,
2637                }
2638        return data
2639
2640    @classmethod
2641    def deserialize(cls, data: JsonDict) -> 'TypeInfo':
2642        names = SymbolTable.deserialize(data['names'])
2643        defn = ClassDef.deserialize(data['defn'])
2644        module_name = data['module_name']
2645        ti = TypeInfo(names, defn, module_name)
2646        ti._fullname = data['fullname']
2647        # TODO: Is there a reason to reconstruct ti.subtypes?
2648        ti.abstract_attributes = data['abstract_attributes']
2649        ti.type_vars = data['type_vars']
2650        ti.bases = [mypy.types.Instance.deserialize(b) for b in data['bases']]
2651        ti._promote = (None if data['_promote'] is None
2652                       else mypy.types.deserialize_type(data['_promote']))
2653        ti.declared_metaclass = (None if data['declared_metaclass'] is None
2654                                 else mypy.types.Instance.deserialize(data['declared_metaclass']))
2655        ti.metaclass_type = (None if data['metaclass_type'] is None
2656                             else mypy.types.Instance.deserialize(data['metaclass_type']))
2657        # NOTE: ti.mro will be set in the fixup phase based on these
2658        # names.  The reason we need to store the mro instead of just
2659        # recomputing it from base classes has to do with a subtle
2660        # point about fine-grained incremental: the cache files might
2661        # not be loaded until after a class in the mro has changed its
2662        # bases, which causes the mro to change. If we recomputed our
2663        # mro, we would compute the *new* mro, which leaves us with no
2664        # way to detect that the mro has changed! Thus we need to make
2665        # sure to load the original mro so that once the class is
2666        # rechecked, it can tell that the mro has changed.
2667        ti._mro_refs = data['mro']
2668        ti.tuple_type = (None if data['tuple_type'] is None
2669                         else mypy.types.TupleType.deserialize(data['tuple_type']))
2670        ti.typeddict_type = (None if data['typeddict_type'] is None
2671                            else mypy.types.TypedDictType.deserialize(data['typeddict_type']))
2672        ti.metadata = data['metadata']
2673        set_flags(ti, data['flags'])
2674        return ti
2675
2676
2677class FakeInfo(TypeInfo):
2678    # types.py defines a single instance of this class, called types.NOT_READY.
2679    # This instance is used as a temporary placeholder in the process of de-serialization
2680    # of 'Instance' types. The de-serialization happens in two steps: In the first step,
2681    # Instance.type is set to NOT_READY. In the second step (in fixup.py) it is replaced by
2682    # an actual TypeInfo. If you see the assertion error below, then most probably something
2683    # went wrong during the second step and an 'Instance' that raised this error was not fixed.
2684    # Note:
2685    # 'None' is not used as a dummy value for two reasons:
2686    # 1. This will require around 80-100 asserts to make 'mypy --strict-optional mypy'
2687    #    pass cleanly.
2688    # 2. If NOT_READY value is accidentally used somewhere, it will be obvious where the value
2689    #    is from, whereas a 'None' value could come from anywhere.
2690    #
2691    # Additionally, this serves as a more general-purpose placeholder
2692    # for missing TypeInfos in a number of places where the excuses
2693    # for not being Optional are a little weaker.
2694    #
2695    # TypeInfo defines a __bool__ method that returns False for FakeInfo
2696    # so that it can be conveniently tested against in the same way that it
2697    # would be if things were properly optional.
2698    def __init__(self, msg: str) -> None:
2699        self.msg = msg
2700
2701    def __getattribute__(self, attr: str) -> None:
2702        # Handle __class__ so that isinstance still works...
2703        if attr == '__class__':
2704            return object.__getattribute__(self, attr)
2705        raise AssertionError(object.__getattribute__(self, 'msg'))
2706
2707
2708VAR_NO_INFO = FakeInfo('Var is lacking info')  # type: Final[TypeInfo]
2709CLASSDEF_NO_INFO = FakeInfo('ClassDef is lacking info')  # type: Final[TypeInfo]
2710FUNC_NO_INFO = FakeInfo('FuncBase for non-methods lack info')  # type: Final[TypeInfo]
2711
2712
2713class TypeAlias(SymbolNode):
2714    """
2715    A symbol node representing a type alias.
2716
2717    Type alias is a static concept, in contrast to variables with types
2718    like Type[...]. Namely:
2719        * type aliases
2720            - can be used in type context (annotations)
2721            - cannot be re-assigned
2722        * variables with type Type[...]
2723            - cannot be used in type context
2724            - but can be re-assigned
2725
2726    An alias can be defined only by an assignment to a name (not any other lvalues).
2727
2728    Such assignment defines an alias by default. To define a variable,
2729    an explicit Type[...] annotation is required. As an exception,
2730    at non-global scope non-subscripted rvalue creates a variable even without
2731    an annotation. This exception exists to accommodate the common use case of
2732    class-valued attributes. See SemanticAnalyzerPass2.check_and_set_up_type_alias
2733    for details.
2734
2735    Aliases can be generic. Currently, mypy uses unbound type variables for
2736    generic aliases and identifies them by name. Essentially, type aliases
2737    work as macros that expand textually. The definition and expansion rules are
2738    following:
2739
2740        1. An alias targeting a generic class without explicit variables act as
2741        the given class (this doesn't apply to Tuple and Callable, which are not proper
2742        classes but special type constructors):
2743
2744            A = List
2745            AA = List[Any]
2746
2747            x: A  # same as List[Any]
2748            x: A[int]  # same as List[int]
2749
2750            x: AA  # same as List[Any]
2751            x: AA[int]  # Error!
2752
2753            C = Callable  # Same as Callable[..., Any]
2754            T = Tuple  # Same as Tuple[Any, ...]
2755
2756        2. An alias using explicit type variables in its rvalue expects
2757        replacements (type arguments) for these variables. If missing, they
2758        are treated as Any, like for other generics:
2759
2760            B = List[Tuple[T, T]]
2761
2762            x: B  # same as List[Tuple[Any, Any]]
2763            x: B[int]  # same as List[Tuple[int, int]]
2764
2765            def f(x: B[T]) -> T: ...  # without T, Any would be used here
2766
2767        3. An alias can be defined using another aliases. In the definition
2768        rvalue the Any substitution doesn't happen for top level unsubscripted
2769        generic classes:
2770
2771            A = List
2772            B = A  # here A is expanded to List, _not_ List[Any],
2773                   # to match the Python runtime behaviour
2774            x: B[int]  # same as List[int]
2775            C = List[A]  # this expands to List[List[Any]]
2776
2777            AA = List[T]
2778            D = AA  # here AA expands to List[Any]
2779            x: D[int]  # Error!
2780
2781    Note: the fact that we support aliases like `A = List` means that the target
2782    type will be initially an instance type with wrong number of type arguments.
2783    Such instances are all fixed in the third pass of semantic analyzis.
2784    We therefore store the difference between `List` and `List[Any]` rvalues (targets)
2785    using the `no_args` flag. See also TypeAliasExpr.no_args.
2786
2787    Meaning of other fields:
2788
2789    target: The target type. For generic aliases contains unbound type variables
2790        as nested types.
2791    _fullname: Qualified name of this type alias. This is used in particular
2792        to track fine grained dependencies from aliases.
2793    alias_tvars: Names of unbound type variables used to define this alias.
2794    normalized: Used to distinguish between `A = List`, and `A = list`. Both
2795        are internally stored using `builtins.list` (because `typing.List` is
2796        itself an alias), while the second cannot be subscripted because of
2797        Python runtime limitation.
2798    line and column: Line an column on the original alias definition.
2799    eager: If True, immediately expand alias when referred to (useful for aliases
2800        within functions that can't be looked up from the symbol table)
2801    """
2802    __slots__ = ('target', '_fullname', 'alias_tvars', 'no_args', 'normalized',
2803                 'line', 'column', '_is_recursive', 'eager')
2804
2805    def __init__(self, target: 'mypy.types.Type', fullname: str, line: int, column: int,
2806                 *,
2807                 alias_tvars: Optional[List[str]] = None,
2808                 no_args: bool = False,
2809                 normalized: bool = False,
2810                 eager: bool = False) -> None:
2811        self._fullname = fullname
2812        self.target = target
2813        if alias_tvars is None:
2814            alias_tvars = []
2815        self.alias_tvars = alias_tvars
2816        self.no_args = no_args
2817        self.normalized = normalized
2818        # This attribute is manipulated by TypeAliasType. If non-None,
2819        # it is the cached value.
2820        self._is_recursive = None  # type: Optional[bool]
2821        self.eager = eager
2822        super().__init__(line, column)
2823
2824    @property
2825    def name(self) -> str:
2826        return self._fullname.split('.')[-1]
2827
2828    @property
2829    def fullname(self) -> str:
2830        return self._fullname
2831
2832    def serialize(self) -> JsonDict:
2833        data = {'.class': 'TypeAlias',
2834                'fullname': self._fullname,
2835                'target': self.target.serialize(),
2836                'alias_tvars': self.alias_tvars,
2837                'no_args': self.no_args,
2838                'normalized': self.normalized,
2839                'line': self.line,
2840                'column': self.column
2841                }  # type: JsonDict
2842        return data
2843
2844    def accept(self, visitor: NodeVisitor[T]) -> T:
2845        return visitor.visit_type_alias(self)
2846
2847    @classmethod
2848    def deserialize(cls, data: JsonDict) -> 'TypeAlias':
2849        assert data['.class'] == 'TypeAlias'
2850        fullname = data['fullname']
2851        alias_tvars = data['alias_tvars']
2852        target = mypy.types.deserialize_type(data['target'])
2853        no_args = data['no_args']
2854        normalized = data['normalized']
2855        line = data['line']
2856        column = data['column']
2857        return cls(target, fullname, line, column, alias_tvars=alias_tvars,
2858                   no_args=no_args, normalized=normalized)
2859
2860
2861class PlaceholderNode(SymbolNode):
2862    """Temporary symbol node that will later become a real SymbolNode.
2863
2864    These are only present during semantic analysis when using the new
2865    semantic analyzer. These are created if some essential dependencies
2866    of a definition are not yet complete.
2867
2868    A typical use is for names imported from a module which is still
2869    incomplete (within an import cycle):
2870
2871      from m import f  # Initially may create PlaceholderNode
2872
2873    This is particularly important if the imported shadows a name from
2874    an enclosing scope or builtins:
2875
2876      from m import int  # Placeholder avoids mixups with builtins.int
2877
2878    Another case where this is useful is when there is another definition
2879    or assignment:
2880
2881      from m import f
2882      def f() -> None: ...
2883
2884    In the above example, the presence of PlaceholderNode allows us to
2885    handle the second definition as a redefinition.
2886
2887    They are also used to create PlaceholderType instances for types
2888    that refer to incomplete types. Example:
2889
2890      class C(Sequence[C]): ...
2891
2892    We create a PlaceholderNode (with becomes_typeinfo=True) for C so
2893    that the type C in Sequence[C] can be bound.
2894
2895    Attributes:
2896
2897      fullname: Full name of of the PlaceholderNode.
2898      node: AST node that contains the definition that caused this to
2899          be created. This is useful for tracking order of incomplete definitions
2900          and for debugging.
2901      becomes_typeinfo: If True, this refers something that could later
2902          become a TypeInfo. It can't be used with type variables, in
2903          particular, as this would cause issues with class type variable
2904          detection.
2905
2906    The long-term purpose of placeholder nodes/types is to evolve into
2907    something that can support general recursive types.
2908    """
2909
2910    def __init__(self, fullname: str, node: Node, line: int, *,
2911                 becomes_typeinfo: bool = False) -> None:
2912        self._fullname = fullname
2913        self.node = node
2914        self.becomes_typeinfo = becomes_typeinfo
2915        self.line = line
2916
2917    @property
2918    def name(self) -> str:
2919        return self._fullname.split('.')[-1]
2920
2921    @property
2922    def fullname(self) -> str:
2923        return self._fullname
2924
2925    def serialize(self) -> JsonDict:
2926        assert False, "PlaceholderNode can't be serialized"
2927
2928    def accept(self, visitor: NodeVisitor[T]) -> T:
2929        return visitor.visit_placeholder_node(self)
2930
2931
2932class SymbolTableNode:
2933    """Description of a name binding in a symbol table.
2934
2935    These are only used as values in module (global), function (local)
2936    and class symbol tables (see SymbolTable). The name that is bound is
2937    the key in SymbolTable.
2938
2939    Symbol tables don't contain direct references to AST nodes primarily
2940    because there can be multiple symbol table references to a single
2941    AST node (due to imports and aliases), and different references can
2942    behave differently. This class describes the unique properties of
2943    each reference.
2944
2945    The most fundamental attribute is 'node', which is the AST node that
2946    the name refers to.
2947
2948    The kind is usually one of LDEF, GDEF or MDEF, depending on the scope
2949    of the definition. These three kinds can usually be used
2950    interchangeably and the difference between local, global and class
2951    scopes is mostly descriptive, with no semantic significance.
2952    However, some tools that consume mypy ASTs may care about these so
2953    they should be correct.
2954
2955    Attributes:
2956        node: AST node of definition. Among others, this can be one of
2957            FuncDef, Var, TypeInfo, TypeVarExpr or MypyFile -- or None
2958            for cross_ref that hasn't been fixed up yet.
2959        kind: Kind of node. Possible values:
2960               - LDEF: local definition
2961               - GDEF: global (module-level) definition
2962               - MDEF: class member definition
2963               - UNBOUND_IMPORTED: temporary kind for imported names (we
2964                 don't know the final kind yet)
2965        module_public: If False, this name won't be imported via
2966            'from <module> import *'. This has no effect on names within
2967            classes.
2968        module_hidden: If True, the name will be never exported (needed for
2969            stub files)
2970        cross_ref: For deserialized MypyFile nodes, the referenced module
2971            name; for other nodes, optionally the name of the referenced object.
2972        implicit: Was this defined by assignment to self attribute?
2973        plugin_generated: Was this symbol generated by a plugin?
2974            (And therefore needs to be removed in aststrip.)
2975        no_serialize: Do not serialize this node if True. This is used to prevent
2976            keys in the cache that refer to modules on which this file does not
2977            depend. Currently this can happen if there is a module not in build
2978            used e.g. like this:
2979                import a.b.c # type: ignore
2980            This will add a submodule symbol to parent module `a` symbol table,
2981            but `a.b` is _not_ added as its dependency. Therefore, we should
2982            not serialize these symbols as they may not be found during fixup
2983            phase, instead they will be re-added during subsequent patch parents
2984            phase.
2985            TODO: Refactor build.py to make dependency tracking more transparent
2986            and/or refactor look-up functions to not require parent patching.
2987
2988    NOTE: No other attributes should be added to this class unless they
2989    are shared by all node kinds.
2990    """
2991
2992    __slots__ = ('kind',
2993                 'node',
2994                 'module_public',
2995                 'module_hidden',
2996                 'cross_ref',
2997                 'implicit',
2998                 'plugin_generated',
2999                 'no_serialize',
3000                 )
3001
3002    def __init__(self,
3003                 kind: int,
3004                 node: Optional[SymbolNode],
3005                 module_public: bool = True,
3006                 implicit: bool = False,
3007                 module_hidden: bool = False,
3008                 *,
3009                 plugin_generated: bool = False,
3010                 no_serialize: bool = False) -> None:
3011        self.kind = kind
3012        self.node = node
3013        self.module_public = module_public
3014        self.implicit = implicit
3015        self.module_hidden = module_hidden
3016        self.cross_ref = None  # type: Optional[str]
3017        self.plugin_generated = plugin_generated
3018        self.no_serialize = no_serialize
3019
3020    @property
3021    def fullname(self) -> Optional[str]:
3022        if self.node is not None:
3023            return self.node.fullname
3024        else:
3025            return None
3026
3027    @property
3028    def type(self) -> 'Optional[mypy.types.Type]':
3029        node = self.node
3030        if isinstance(node, (Var, SYMBOL_FUNCBASE_TYPES)) and node.type is not None:
3031            return node.type
3032        elif isinstance(node, Decorator):
3033            return node.var.type
3034        else:
3035            return None
3036
3037    def copy(self) -> 'SymbolTableNode':
3038        new = SymbolTableNode(self.kind,
3039                              self.node,
3040                              self.module_public,
3041                              self.implicit,
3042                              self.module_hidden)
3043        new.cross_ref = self.cross_ref
3044        return new
3045
3046    def __str__(self) -> str:
3047        s = '{}/{}'.format(node_kinds[self.kind], short_type(self.node))
3048        if isinstance(self.node, SymbolNode):
3049            s += ' ({})'.format(self.node.fullname)
3050        # Include declared type of variables and functions.
3051        if self.type is not None:
3052            s += ' : {}'.format(self.type)
3053        return s
3054
3055    def serialize(self, prefix: str, name: str) -> JsonDict:
3056        """Serialize a SymbolTableNode.
3057
3058        Args:
3059          prefix: full name of the containing module or class; or None
3060          name: name of this object relative to the containing object
3061        """
3062        data = {'.class': 'SymbolTableNode',
3063                'kind': node_kinds[self.kind],
3064                }  # type: JsonDict
3065        if self.module_hidden:
3066            data['module_hidden'] = True
3067        if not self.module_public:
3068            data['module_public'] = False
3069        if self.implicit:
3070            data['implicit'] = True
3071        if self.plugin_generated:
3072            data['plugin_generated'] = True
3073        if isinstance(self.node, MypyFile):
3074            data['cross_ref'] = self.node.fullname
3075        else:
3076            assert self.node is not None, '%s:%s' % (prefix, name)
3077            if prefix is not None:
3078                fullname = self.node.fullname
3079                if (fullname is not None and '.' in fullname
3080                        and fullname != prefix + '.' + name
3081                        and not (isinstance(self.node, Var)
3082                                 and self.node.from_module_getattr)):
3083                    assert not isinstance(self.node, PlaceholderNode), (
3084                        'Definition of {} is unexpectedly incomplete'.format(fullname)
3085                    )
3086                    data['cross_ref'] = fullname
3087                    return data
3088            data['node'] = self.node.serialize()
3089        return data
3090
3091    @classmethod
3092    def deserialize(cls, data: JsonDict) -> 'SymbolTableNode':
3093        assert data['.class'] == 'SymbolTableNode'
3094        kind = inverse_node_kinds[data['kind']]
3095        if 'cross_ref' in data:
3096            # This will be fixed up later.
3097            stnode = SymbolTableNode(kind, None)
3098            stnode.cross_ref = data['cross_ref']
3099        else:
3100            assert 'node' in data, data
3101            node = SymbolNode.deserialize(data['node'])
3102            stnode = SymbolTableNode(kind, node)
3103        if 'module_hidden' in data:
3104            stnode.module_hidden = data['module_hidden']
3105        if 'module_public' in data:
3106            stnode.module_public = data['module_public']
3107        if 'implicit' in data:
3108            stnode.implicit = data['implicit']
3109        if 'plugin_generated' in data:
3110            stnode.plugin_generated = data['plugin_generated']
3111        return stnode
3112
3113
3114class SymbolTable(Dict[str, SymbolTableNode]):
3115    """Static representation of a namespace dictionary.
3116
3117    This is used for module, class and function namespaces.
3118    """
3119
3120    def __str__(self) -> str:
3121        a = []  # type: List[str]
3122        for key, value in self.items():
3123            # Filter out the implicit import of builtins.
3124            if isinstance(value, SymbolTableNode):
3125                if (value.fullname != 'builtins' and
3126                        (value.fullname or '').split('.')[-1] not in
3127                        implicit_module_attrs):
3128                    a.append('  ' + str(key) + ' : ' + str(value))
3129            else:
3130                a.append('  <invalid item>')
3131        a = sorted(a)
3132        a.insert(0, 'SymbolTable(')
3133        a[-1] += ')'
3134        return '\n'.join(a)
3135
3136    def copy(self) -> 'SymbolTable':
3137        return SymbolTable([(key, node.copy())
3138                            for key, node in self.items()])
3139
3140    def serialize(self, fullname: str) -> JsonDict:
3141        data = {'.class': 'SymbolTable'}  # type: JsonDict
3142        for key, value in self.items():
3143            # Skip __builtins__: it's a reference to the builtins
3144            # module that gets added to every module by
3145            # SemanticAnalyzerPass2.visit_file(), but it shouldn't be
3146            # accessed by users of the module.
3147            if key == '__builtins__' or value.no_serialize:
3148                continue
3149            data[key] = value.serialize(fullname, key)
3150        return data
3151
3152    @classmethod
3153    def deserialize(cls, data: JsonDict) -> 'SymbolTable':
3154        assert data['.class'] == 'SymbolTable'
3155        st = SymbolTable()
3156        for key, value in data.items():
3157            if key != '.class':
3158                st[key] = SymbolTableNode.deserialize(value)
3159        return st
3160
3161
3162def get_flags(node: Node, names: List[str]) -> List[str]:
3163    return [name for name in names if getattr(node, name)]
3164
3165
3166def set_flags(node: Node, flags: List[str]) -> None:
3167    for name in flags:
3168        setattr(node, name, True)
3169
3170
3171def get_member_expr_fullname(expr: MemberExpr) -> Optional[str]:
3172    """Return the qualified name representation of a member expression.
3173
3174    Return a string of form foo.bar, foo.bar.baz, or similar, or None if the
3175    argument cannot be represented in this form.
3176    """
3177    initial = None  # type: Optional[str]
3178    if isinstance(expr.expr, NameExpr):
3179        initial = expr.expr.name
3180    elif isinstance(expr.expr, MemberExpr):
3181        initial = get_member_expr_fullname(expr.expr)
3182    else:
3183        return None
3184    return '{}.{}'.format(initial, expr.name)
3185
3186
3187deserialize_map = {
3188    key: obj.deserialize
3189    for key, obj in globals().items()
3190    if type(obj) is not FakeInfo
3191    and isinstance(obj, type) and issubclass(obj, SymbolNode) and obj is not SymbolNode
3192}  # type: Final
3193
3194
3195def check_arg_kinds(arg_kinds: List[int], nodes: List[T], fail: Callable[[str, T], None]) -> None:
3196    is_var_arg = False
3197    is_kw_arg = False
3198    seen_named = False
3199    seen_opt = False
3200    for kind, node in zip(arg_kinds, nodes):
3201        if kind == ARG_POS:
3202            if is_var_arg or is_kw_arg or seen_named or seen_opt:
3203                fail("Required positional args may not appear "
3204                     "after default, named or var args",
3205                     node)
3206                break
3207        elif kind == ARG_OPT:
3208            if is_var_arg or is_kw_arg or seen_named:
3209                fail("Positional default args may not appear after named or var args", node)
3210                break
3211            seen_opt = True
3212        elif kind == ARG_STAR:
3213            if is_var_arg or is_kw_arg or seen_named:
3214                fail("Var args may not appear after named or var args", node)
3215                break
3216            is_var_arg = True
3217        elif kind == ARG_NAMED or kind == ARG_NAMED_OPT:
3218            seen_named = True
3219            if is_kw_arg:
3220                fail("A **kwargs argument must be the last argument", node)
3221                break
3222        elif kind == ARG_STAR2:
3223            if is_kw_arg:
3224                fail("You may only have one **kwargs argument", node)
3225                break
3226            is_kw_arg = True
3227
3228
3229def check_arg_names(names: Sequence[Optional[str]], nodes: List[T], fail: Callable[[str, T], None],
3230                    description: str = 'function definition') -> None:
3231    seen_names = set()  # type: Set[Optional[str]]
3232    for name, node in zip(names, nodes):
3233        if name is not None and name in seen_names:
3234            fail('Duplicate argument "{}" in {}'.format(name, description), node)
3235            break
3236        seen_names.add(name)
3237
3238
3239def is_class_var(expr: NameExpr) -> bool:
3240    """Return whether the expression is ClassVar[...]"""
3241    if isinstance(expr.node, Var):
3242        return expr.node.is_classvar
3243    return False
3244
3245
3246def is_final_node(node: Optional[SymbolNode]) -> bool:
3247    """Check whether `node` corresponds to a final attribute."""
3248    return isinstance(node, (Var, FuncDef, OverloadedFuncDef, Decorator)) and node.is_final
3249
3250
3251def local_definitions(names: SymbolTable,
3252                      name_prefix: str,
3253                      info: Optional[TypeInfo] = None) -> Iterator[Definition]:
3254    """Iterate over local definitions (not imported) in a symbol table.
3255
3256    Recursively iterate over class members and nested classes.
3257    """
3258    # TODO: What should the name be? Or maybe remove it?
3259    for name, symnode in names.items():
3260        shortname = name
3261        if '-redef' in name:
3262            # Restore original name from mangled name of multiply defined function
3263            shortname = name.split('-redef')[0]
3264        fullname = name_prefix + '.' + shortname
3265        node = symnode.node
3266        if node and node.fullname == fullname:
3267            yield fullname, symnode, info
3268            if isinstance(node, TypeInfo):
3269                yield from local_definitions(node.names, fullname, node)
3270