8000 Make Expression and Statement separable by elazarg · Pull Request #2209 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Make Expression and Statement separable #2209

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 3 commits into from
Oct 3, 2016
Merged
Show file tree
Hide file tree
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
20 changes: 10 additions & 10 deletions mypy/binder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import (Any, Dict, List, Set, Iterator)
from typing import (Any, Dict, List, Set, Iterator, Union)
from contextlib import contextmanager

from mypy.types import Type, AnyType, PartialType
from mypy.nodes import (Expression, Var, RefExpr, SymbolTableNode)
from mypy.nodes import (Node, Expression, Var, RefExpr, SymbolTableNode)

from mypy.subtypes import is_subtype
from mypy.join import join_simple
Expand Down Expand Up @@ -96,16 +96,16 @@ def _get(self, key: Key, index: int=-1) -> Type:
return self.frames[i][key]
return None

def push(self, expr: Expression, typ: Type) -> None:
if not expr.literal:
def push(self, node: Node, typ: Type) -> None:
if not node.literal:
return
key = expr.literal_hash
key = node.literal_hash
if key not in self.declarations:
self.declarations[key] = self.get_declaration(expr)
self.declarations[key] = self.get_declaration(node)
self._add_dependencies(key)
self._push(key, typ)

def get(self, expr: Expression) -> Type:
def get(self, expr: Union[Expression, Var]) -> Type:
return self._get(expr.literal_hash)

def cleanse(self, expr: Expression) -> None:
Expand Down Expand Up @@ -165,9 +165,9 @@ def pop_frame(self, fall_through: int = 0) -> Frame:

return result

def get_declaration(self, expr: Expression) -> Type:
if isinstance(expr, (RefExpr, SymbolTableNode)) and isinstance(expr.node, Var):
type = expr.node.type
def get_declaration(self, node: Node) -> Type:
if isinstance(node, (RefExpr, SymbolTableNode)) and isinstance(node.node, Var):
type = node.node.type
if isinstance(type, PartialType):
return None
return type
Expand Down
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2218,7 +2218,7 @@ def check_type_equivalency(self, t1: Type, t2: Type, node: Context,
if not is_equivalent(t1, t2):
self.fail(msg, node)

def store_type(self, node: Expression, typ: Type) -> None:
def store_type(self, node: Node, typ: Type) -> None:
"""Store the type of a node in the type map."""
self.type_map[node] = typ
if typ is not None:
Expand Down
7 changes: 4 additions & 3 deletions mypy/exprtotype.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Translate an expression (Node) to a Type value."""
"""Translate an Expression to a Type value."""

from mypy.nodes import (
Node, NameExpr, MemberExpr, IndexExpr, TupleExpr, ListExpr, StrExpr, BytesExpr, EllipsisExpr
Expression, NameExpr, MemberExpr, IndexExpr, TupleExpr,
ListExpr, StrExpr, BytesExpr, EllipsisExpr
)
from mypy.parsetype import parse_str_as_type, TypeParseError
from mypy.types import Type, UnboundType, TypeList, EllipsisType
Expand All @@ -11,7 +12,7 @@ class TypeTranslationError(Exception):
"""Exception raised when an expression is not valid as a type."""


def expr_to_unanalyzed_type(expr: Node) -> Type:
def expr_to_unanalyzed_type(expr: Expression) -> Type:
"""Translate an expression to the corresponding type.

The result is not semantically analyzed. It can be UnboundType or TypeList.
Expand Down
62 changes: 39 additions & 23 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,21 @@ def generic_visit(self, node: ast35.AST) -> None:
def visit_NoneType(self, n: Any) -> Optional[Node]:
return None

def visit_list(self, l: Sequence[ast35.AST]) -> List[Expression]:
return [self.visit(e) for e in l]
def translate_expr_list(self, l: Sequence[ast35.AST]) -> List[Expression]:
res = [] # type: List[Expression]
for e in l:
exp = self.visit(e)
assert exp is None or isinstance(exp, Expression)
res.append(exp)
return res

def translate_stmt_list(self, l: Sequence[ast35.AST]) -> List[Statement]:
res = [] # type: List[Statement]
for e in l:
stmt = self.visit(e)
assert stmt is None or isinstance(stmt, Statement)
res.append(stmt)
return res

op_map = {
ast35.Add: '+',
Expand Down Expand Up @@ -176,7 +189,7 @@ def from_comp_operator(self, op: ast35.cmpop) -> str:
def as_block(self, stmts: List[ast35.stmt], lineno: int) -> Block:
b = None
if stmts:
b = Block(self.fix_function_overloads(self.visit_list(stmts)))
b = Block(self.fix_function_overloads(self.translate_stmt_list(stmts)))
b.set_line(lineno)
return b

Expand Down Expand Up @@ -225,7 +238,7 @@ def translate_module_id(self, id: str) -> str:
return id

def visit_Module(self, mod: ast35.Module) -> MypyFile:
body = self.fix_function_overloads(self.visit_list(mod.body))
body = self.fix_function_overloads(self.translate_stmt_list(mod.body))

return MypyFile(body,
self.imports,
Expand Down Expand Up @@ -268,8 +281,10 @@ def do_func_def(self, n: Union[ast35.FunctionDef, ast35.AsyncFunctionDef],
arg_types = [a.type_annotation if a.type_annotation is not None else AnyType()
for a in args]
else:
arg_types = [a if a is not None else AnyType() for
a in TypeConverter(line=n.lineno).visit_list(func_type_ast.argtypes)]
translated_args = (TypeConverter(line=n.lineno)
.translate_expr_list(func_type_ast.argtypes))
arg_types = [a if a is not None else AnyType()
for a in translated_args]
return_type = TypeConverter(line=n.lineno).visit(func_type_ast.returns)

# add implicit self type
Expand Down Expand Up @@ -312,7 +327,7 @@ def do_func_def(self, n: Union[ast35.FunctionDef, ast35.AsyncFunctionDef],
func_def.is_decorated = True
func_def.set_line(n.lineno + len(n.decorator_list))
func_def.body.set_line(func_def.get_line())
return Decorator(func_def, self.visit_list(n.decorator_list), var)
return Decorator(func_def, self.translate_expr_list(n.decorator_list), var)
else:
return func_def

Expand Down Expand Up @@ -382,9 +397,9 @@ def visit_ClassDef(self, n: ast35.ClassDef) -> ClassDef:
cdef = ClassDef(n.name,
self.as_block(n.body, n.lineno),
None,
self.visit_list(n. 67F4 bases),
self.translate_expr_list(n.bases),
metaclass=metaclass)
cdef.decorators = self.visit_list(n.decorator_list)
cdef.decorators = self.translate_expr_list(n.decorator_list)
self.class_nesting -= 1
return cdef

Expand All @@ -397,7 +412,7 @@ def visit_Return(self, n: ast35.Return) -> ReturnStmt:
@with_line
def visit_Delete(self, n: ast35.Delete) -> DelStmt:
if len(n.targets) > 1:
tup = TupleExpr(self.visit_list(n.targets))
tup = TupleExpr(self.translate_expr_list(n.targets))
tup.set_line(n.lineno)
return DelStmt(tup)
else:
Expand All @@ -424,7 +439,7 @@ def visit_Assign(self, n: ast35.Assign) -> AssignmentStmt:
rvalue = TempNode(AnyType()) # type: Expression
else:
rvalue = self.visit(n.value)
lvalues = self.visit_list(n.targets)
lvalues = self.translate_expr_list(n.targets)
return AssignmentStmt(lvalues,
rvalue,
type=typ, new_syntax=new_syntax)
Expand Down Expand Up @@ -590,7 +605,7 @@ def group(vals: List[Expression]) -> OpExpr:
else:
return OpExpr(op, vals[0], group(vals[1:]))

return group(self.visit_list(n.values))
return group(self.translate_expr_list(n.values))

# BinOp(expr left, operator op, expr right)
@with_line
Expand Down Expand Up @@ -640,12 +655,13 @@ def visit_IfExp(self, n: ast35.IfExp) -> ConditionalExpr:
# Dict(expr* keys, expr* values)
@with_line
def visit_Dict(self, n: ast35.Dict) -> DictExpr:
return DictExpr(list(zip(self.visit_list(n.keys), self.visit_list(n.values))))
return DictExpr(list(zip(self.translate_expr_list(n.keys),
self.translate_expr_list(n.values))))

# Set(expr* elts)
@with_line
def visit_Set(self, n: ast35.Set) -> SetExpr:
return SetExpr(self.visit_list(n.elts))
return SetExpr(self.translate_expr_list(n.elts))

# ListComp(expr elt, comprehension* generators)
@with_line
Expand All @@ -662,7 +678,7 @@ def visit_SetComp(self, n: ast35.SetComp) -> SetComprehension:
def visit_DictComp(self, n: ast35.DictComp) -> DictionaryComprehension:
targets = [self.visit(c.target) for c in n.generators]
iters = [self.visit(c.iter) for c in n.generators]
ifs_list = [self.visit_list(c.ifs) for c in n.generators]
ifs_list = [self.translate_expr_list(c.ifs) for c in n.generators]
return DictionaryComprehension(self.visit(n.key),
self.visit(n.value),
targets,
Expand All @@ -674,7 +690,7 @@ def visit_DictComp(self, n: ast35.DictComp) -> DictionaryComprehension:
def visit_GeneratorExp(self, n: ast35.GeneratorExp) -> GeneratorExpr:
targets = [self.visit(c.target) for c in n.generators]
iters = [self.visit(c.iter) for c in n.generators]
ifs_list = [self.visit_list(c.ifs) for c in n.generators]
ifs_list = [self.translate_expr_list(c.ifs) for c in n.generators]
return GeneratorExpr(self.visit(n.elt),
targets,
iters,
Expand All @@ -700,7 +716,7 @@ def visit_YieldFrom(self, n: ast35.YieldFrom) -> YieldFromExpr:
@with_line
def visit_Compare(self, n: ast35.Compare) -> ComparisonExpr:
operators = [self.from_comp_operator(o) for o in n.ops]
operands = self.visit_list([n.left] + n.comparators)
operands = self.translate_expr_list([n.left] + n.comparators)
return ComparisonExpr(operators, operands)

# Call(expr func, expr* args, keyword* keywords)
Expand All @@ -710,7 +726,7 @@ def visit_Call(self, n: ast35.Call) -> CallExpr:
def is_star2arg(k: ast35.keyword) -> bool:
return k.arg is None

arg_types = self.visit_list(
arg_types = self.translate_expr_list(
[a.value if isinstance(a, ast35.Starred) else a for a in n.args] +
[k.value for k in n.keywords])
arg_kinds = ([ARG_STAR if isinstance(a, ast35.Starred) else ARG_POS for a in n.args] +
Expand Down Expand Up @@ -812,7 +828,7 @@ def visit_Slice(self, n: ast35.Slice) -> SliceExpr:

# ExtSlice(slice* dims)
def visit_ExtSlice(self, n: ast35.ExtSlice) -> TupleExpr:
return TupleExpr(self.visit_list(n.dims))
return TupleExpr(self.translate_expr_list(n.dims))

# Index(expr value)
def visit_Index(self, n: ast35.Index) -> Node:
Expand All @@ -836,7 +852,7 @@ def generic_visit(self, node: ast35.AST) -> None:
def visit_NoneType(self, n: Any) -> Type:
return None

def visit_list(self, l: Sequence[ast35.AST]) -> List[Type]:
def translate_expr_list(self, l: Sequence[ast35.AST]) -> List[Type]:
return [self.visit(e) for e in l]

def visit_Name(self, n: ast35.Name) -> Type:
Expand All @@ -860,7 +876,7 @@ def visit_Subscript(self, n: ast35.Subscript) -> Type:

empty_tuple_index = False
if isinstance(n.slice.value, ast35.Tuple):
params = self.visit_list(n.slice.value.elts)
params = self.translate_expr_list(n.slice.value.elts)
if len(n.slice.value.elts) == 0:
empty_tuple_index = True
else:
Expand All @@ -869,7 +885,7 @@ def visit_Subscript(self, n: ast35.Subscript) -> Type:
return UnboundType(value.name, params, line=self.line, empty_tuple_index=empty_tuple_index)

def visit_Tuple(self, n: ast35.Tuple) -> Type:
return TupleType(self.visit_list(n.elts), None, implicit=True, line=self.line)
return TupleType(self.translate_expr_list(n.elts), None, implicit=True, line=self.line)

# Attribute(expr value, identifier attr, expr_context ctx)
def visit_Attribute(self, n: ast35.Attribute) -> Type:
Expand All @@ -886,7 +902,7 @@ def visit_Ellipsis(self, n: ast35.Ellipsis) -> Type:

# List(expr* elts, expr_context ctx)
def visit_List(self, n: ast35.List) -> Type:
return TypeList(self.visit_list(n.elts), line=self.line)
return TypeList(self.translate_expr_list(n.elts), line=self.line)


class TypeCommentParseError(Exception):
Expand Down
Loading
0