Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
inherit AbstractNodeVisitor
  • Loading branch information
elazarg committed Apr 5, 2017
commit 0604ee97fb01c5cc35cdadac6d18af7d2646cc4a
6 changes: 3 additions & 3 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)

import mypy.strconv
from mypy.visitor import NodeVisitor, StatementVisitor, ExpressionVisitor
from mypy.visitor import AbstractNodeVisitor, StatementVisitor, ExpressionVisitor
from mypy.util import dump_tagged, short_type


Expand Down Expand Up @@ -144,7 +144,7 @@ def get_column(self) -> int:
# TODO this should be just 'column'
return self.column

def accept(self, visitor: NodeVisitor[T]) -> T:
def accept(self, visitor: AbstractNodeVisitor[T]) -> T:
raise RuntimeError('Not implemented')


Expand Down Expand Up @@ -271,7 +271,7 @@ def name(self) -> str:
def fullname(self) -> str:
return self._fullname

def accept(self, visitor: NodeVisitor[T]) -> T:
def accept(self, visitor: AbstractNodeVisitor[T]) -> T:
return visitor.visit_mypy_file(self)

def is_package_init_file(self) -> bool:
Expand Down
61 changes: 54 additions & 7 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@
IntExpr, FloatExpr, UnicodeExpr, UNBOUND_IMPORTED, LITERAL_YES, nongen_builtins,
collections_type_aliases, get_member_expr_fullname,
)
import mypy.nodes
from mypy.typevars import has_no_typevars, fill_typevars
from mypy.visitor import NodeVisitor
from mypy.visitor import AbstractNodeVisitor, NodeVisitor
from mypy.traverser import TraverserVisitor
from mypy.errors import Errors, report_internal_error
from mypy.messages import CANNOT_ASSIGN_TO_TYPE
Expand Down Expand Up @@ -150,7 +151,7 @@
FUNCTION_SECOND_PHASE = 2 # Only analyze body


class SemanticAnalyzer(NodeVisitor):
class SemanticAnalyzer(AbstractNodeVisitor[None]):
"""Semantically analyze parsed mypy files.

The analyzer binds names and does various consistency checks for a
Expand Down Expand Up @@ -655,7 +656,7 @@ def visit_class_def(self, defn: ClassDef) -> None:
self.analyze_metaclass(defn)

for decorator in defn.decorators:
self.analyze_class_decorator(defn, decorator)
decorator.accept(self)

self.enter_class(defn)

Expand Down Expand Up @@ -702,9 +703,6 @@ def unbind_class_type_vars(self) -> None:
if self.bound_tvars:
enable_typevars(self.bound_tvars)

def analyze_class_decorator(self, defn: ClassDef, decorator: Expression) -> None:
decorator.accept(self)

def setup_type_promotion(self, defn: ClassDef) -> None:
"""Setup extra, ad-hoc subtyping relationships between classes (promotion).

Expand Down Expand Up @@ -1622,7 +1620,7 @@ def visit_if_stmt(self, s: IfStmt) -> None:
def visit_try_stmt(self, s: TryStmt) -> None:
self.analyze_try_stmt(s, self)

def analyze_try_stmt(self, s: TryStmt, visitor: NodeVisitor,
def analyze_try_stmt(self, s: TryStmt, visitor: AbstractNodeVisitor,
add_global: bool = False) -> None:
s.body.accept(visitor)
for type, var, handler in zip(s.types, s.vars, s.handlers):
Expand Down Expand Up @@ -1734,6 +1732,9 @@ def visit_exec_stmt(self, s: ExecStmt) -> None:
if s.variables2:
s.variables2.accept(self)

def visit_pass_stmt(self, o: 'mypy.nodes.PassStmt') -> T:
pass

#
# Expressions
#
Expand Down Expand Up @@ -2107,6 +2108,52 @@ def visit_await_expr(self, expr: AwaitExpr) -> None:
self.fail("'await' outside coroutine ('async def')", expr)
expr.expr.accept(self)

def visit_type_alias_expr(self, o: 'mypy.nodes.TypeAliasExpr') -> None:
pass

def visit_typeddict_expr(self, o: 'mypy.nodes.TypedDictExpr') -> None:
pass

def visit_int_expr(self, o: 'mypy.nodes.IntExpr') -> None:
pass

def visit_complex_expr(self, o: 'mypy.nodes.ComplexExpr') -> None:
pass

def visit_float_expr(self, o: 'mypy.nodes.FloatExpr') -> None:
pass

def visit_bytes_expr(self, o: 'mypy.nodes.BytesExpr') -> None:
pass

def visit_str_expr(self, o: 'mypy.nodes.StrExpr') -> None:
pass

def visit_unicode_expr(self, o: 'mypy.nodes.UnicodeExpr') -> None:
pass

def visit_enum_call_expr(self, o: 'mypy.nodes.EnumCallExpr') -> None:
pass

def visit_namedtuple_expr(self, o: 'mypy.nodes.NamedTupleExpr') -> None:
pass

def visit_newtype_expr(self, o: 'mypy.nodes.NewTypeExpr') -> None:
pass

def visit_type_var_expr(self, o: 'mypy.nodes.TypeVarExpr') -> None:
pass

def visit_ellipsis(self, o: 'mypy.nodes.EllipsisExpr') -> None:
pass

def visit_temp_node(self, o: 'mypy.nodes.TempNode') -> None:
# this method is actually visited
pass

def visit_var(self, o: 'mypy.nodes.Var') -> None:
assert False

#
# Helpers
#
Expand Down
5 changes: 5 additions & 0 deletions mypy/specialtype.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""Special case semantic analysis for type-expressions, such as namedtuple.

This module is used only by the SemanticAnalyzer, and is tightly coupled with it.
"""

from collections import OrderedDict

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add module docstring.


from typing import List, Dict, Tuple, cast, Optional, Callable, TYPE_CHECKING
Expand Down
13 changes: 7 additions & 6 deletions mypy/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ def visit_exec_stmt(self, o: 'mypy.nodes.ExecStmt') -> T:
pass


class NodeVisitor(Generic[T], ExpressionVisitor[T], StatementVisitor[T]):
class AbstractNodeVisitor(Generic[T], ExpressionVisitor[T], StatementVisitor[T]):
# Not in superclasses:
def visit_mypy_file(self, o: 'mypy.nodes.MypyFile') -> T:
pass


class NodeVisitor(Generic[T], AbstractNodeVisitor[T]):
"""Empty base class for parse tree node visitors.

The T type argument specifies the return type of the visit
Expand All @@ -311,11 +317,6 @@ class NodeVisitor(Generic[T], ExpressionVisitor[T], StatementVisitor[T]):
TODO make the default return value explicit
"""

# Not in superclasses:

def visit_mypy_file(self, o: 'mypy.nodes.MypyFile') -> T:
pass

# Module structure

def visit_import(self, o: 'mypy.nodes.Import') -> T:
Expand Down