Skip to content

Tighter types for nodes.py #2224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2016
Merged
Changes from all commits
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
38 changes: 20 additions & 18 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Node(Context):
line = -1
column = -1

# TODO: Move to Expression
literal = LITERAL_NO
literal_hash = None # type: Any

Expand Down Expand Up @@ -139,8 +140,9 @@ class Statement(Node):
class Expression(Node):
"""An expression node."""


# TODO: Union['NameExpr', 'TupleExpr', 'ListExpr', 'MemberExpr', 'IndexExpr']; see #1783.
# TODO:
# Lvalue = Union['NameExpr', 'MemberExpr', 'IndexExpr', 'SuperExpr', 'StarExpr'
# 'TupleExpr', 'ListExpr']; see #1783.
Lvalue = Expression


Expand All @@ -157,7 +159,7 @@ def fullname(self) -> str: pass

# NOTE: Can't use @abstractmethod, since many subclasses of Node
# don't implement serialize().
def serialize(self) -> Any:
def serialize(self) -> JsonDict:
raise NotImplementedError('Cannot serialize {} instance'.format(self.__class__.__name__))

@classmethod
Expand All @@ -171,7 +173,7 @@ def deserialize(cls, data: JsonDict) -> 'SymbolNode':
raise NotImplementedError('unexpected .class {}'.format(classname))


class MypyFile(SymbolNode, Statement):
class MypyFile(SymbolNode):
"""The abstract syntax tree of a single source file."""

# Module name ('__main__' for initial file)
Expand Down Expand Up @@ -591,7 +593,7 @@ def deserialize(cls, data: JsonDict) -> 'Decorator':
return dec


class Var(SymbolNode, Statement):
class Var(SymbolNode):
"""A variable.

It can refer to global/local variable or a data attribute.
Expand Down Expand Up @@ -798,10 +800,10 @@ class OperatorAssignmentStmt(Statement):
"""Operator assignment statement such as x += 1"""

op = ''
lvalue = None # type: Expression
lvalue = None # type: Lvalue
rvalue = None # type: Expression

def __init__(self, op: str, lvalue: Expression, rvalue: Expression) -> None:
def __init__(self, op: str, lvalue: Lvalue, rvalue: Expression) -> None:
self.op = op
self.lvalue = lvalue
self.rvalue = rvalue
Expand All @@ -826,14 +828,14 @@ def accept(self, visitor: NodeVisitor[T]) -> T:

class ForStmt(Statement):
# Index variables
index = None # type: Expression
index = None # type: Lvalue
# Expression to iterate
expr = None # type: Expression
body = None # type: Block
else_body = None # type: Block
is_async = False # True if `async for ...` (PEP 492, Python 3.5)

def __init__(self, index: Expression, expr: Expression, body: Block,
def __init__(self, index: Lvalue, expr: Expression, body: Block,
else_body: Block) -> None:
self.index = index
self.expr = expr
Expand Down Expand Up @@ -865,9 +867,9 @@ def accept(self, visitor: NodeVisitor[T]) -> T:


class DelStmt(Statement):
expr = None # type: Expression
expr = None # type: Lvalue

def __init__(self, expr: Expression) -> None:
def __init__(self, expr: Lvalue) -> None:
self.expr = expr

def accept(self, visitor: NodeVisitor[T]) -> T:
Expand Down Expand Up @@ -940,11 +942,11 @@ def accept(self, visitor: NodeVisitor[T]) -> T:

class WithStmt(Statement):
expr = None # type: List[Expression]
target = None # type: List[Expression]
target = None # type: List[Lvalue]
body = None # type: Block
is_async = False # True if `async with ...` (PEP 492, Python 3.5)

def __init__(self, expr: List[Expression], target: List[Expression],
def __init__(self, expr: List[Expression], target: List[Lvalue],
body: Block) -> None:
self.expr = expr
self.target = target
Expand Down Expand Up @@ -1540,9 +1542,9 @@ class GeneratorExpr(Expression):
left_expr = None # type: Expression
sequences = None # type: List[Expression]
condlists = None # type: List[List[Expression]]
indices = None # type: List[Expression]
indices = None # type: List[Lvalue]

def __init__(self, left_expr: Expression, indices: List[Expression],
def __init__(self, left_expr: Expression, indices: List[Lvalue],
sequences: List[Expression], condlists: List[List[Expression]]) -> None:
self.left_expr = left_expr
self.sequences = sequences
Expand Down Expand Up @@ -1584,9 +1586,9 @@ class DictionaryComprehension(Expression):
value = None # type: Expression
sequences = None # type: List[Expression]
condlists = None # type: List[List[Expression]]
indices = None # type: List[Expression]
indices = None # type: List[Lvalue]

def __init__(self, key: Expression, value: Expression, indices: List[Expression],
def __init__(self, key: Expression, value: Expression, indices: List[Lvalue],
sequences: List[Expression], condlists: List[List[Expression]]) -> None:
self.key = key
self.value = value
Expand Down Expand Up @@ -2026,7 +2028,7 @@ def __str__(self) -> str:
('Names', sorted(self.names.keys()))],
'TypeInfo')

def serialize(self) -> Union[str, JsonDict]:
def serialize(self) -> JsonDict:
# NOTE: This is where all ClassDefs originate, so there shouldn't be duplicates.
data = {'.class': 'TypeInfo',
'module_name': self.module_name,
Expand Down