8000 Update the fast parser to work with Python 2.7 by ddfisher · Pull Request #1418 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Update the fast parser to work with Python 2.7 #1418

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 4 commits into from
Apr 22, 2016
Merged
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
Next Next commit
Update fast parser to work with the new typed_ast module
  • Loading branch information
ddfisher committed Apr 22, 2016
commit d78815965fd858b0e0cb18dc10c45bae48ba0e4e
86 changes: 43 additions & 43 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from mypy.errors import Errors

try:
import typed_ast # type: ignore
from typed_ast import ast35 # type: ignore
except ImportError:
print('You must install the typed_ast module before you can run mypy with `--fast-parser`.\n'
'The typed_ast module can be found at https://github.com/ddfisher/typed_ast',
Expand All @@ -43,7 +43,7 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None,
"""
is_stub_file = bool(fnam) and fnam.endswith('.pyi')
try:
ast = typed_ast.parse(source, fnam, 'exec')
ast = ast35.parse(source, fnam, 'exec')
except SyntaxError as e:
if errors:
errors.set_file('<input>' if fnam is None else fnam)
Expand All @@ -64,7 +64,7 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None,


def parse_type_comment(type_comment: str, line: int) -> Type:
typ = typed_ast.parse(type_comment, '<type_comment>', 'eval')
typ = ast35.parse(type_comment, '<type_comment>', 'eval')
return TypeConverter(line=line).visit(typ.body)


Expand All @@ -84,7 +84,7 @@ def find(f: Callable[[T], bool], seq: Sequence[T]) -> T:
return None


class ASTConverter(typed_ast.NodeTransformer):
class ASTConverter(ast35.NodeTransformer):
def __init__(self):
self.in_class = False
self.imports = []
Expand All @@ -99,19 +99,19 @@ def visit_list(self, l):
return [self.visit(e) for e in l]

op_map = {
typed_ast.Add: '+',
typed_ast.Sub: '-',
typed_ast.Mult: '*',
typed_ast.MatMult: '@',
typed_ast.Div: '/',
typed_ast.Mod: '%',
typed_ast.Pow: '**',
typed_ast.LShift: '<<',
typed_ast.RShift: '>>',
typed_ast.BitOr: '|',
typed_ast.BitXor: '^',
typed_ast.BitAnd: '&',
typed_ast.FloorDiv: '//'
ast35.Add: '+',
ast35.Sub: '-',
ast35.Mult: '*',
ast35.MatMult: '@',
ast35.Div: '/',
ast35.Mod: '%',
ast35.Pow: '**',
ast35.LShift: '<<',
ast35.RShift: '>>',
ast35.BitOr: '|',
ast35.BitXor: '^',
ast35.BitAnd: '&',
ast35.FloorDiv: '//'
}

def from_operator(self, op):
Expand All @@ -124,16 +124,16 @@ def from_operator(self, op):
return op_name

comp_op_map = {
typed_ast.Gt: '>',
typed_ast.Lt: '<',
typed_ast.Eq: '==',
typed_ast.GtE: '>=',
typed_ast.LtE: '<=',
typed_ast.NotEq: '!=',
typed_ast.Is: 'is',
typed_ast.IsNot: 'is not',
typed_ast.In: 'in',
typed_ast.NotIn: 'not in'
ast35.Gt: '>',
ast35.Lt: '<',
ast35.Eq: '==',
ast35.GtE: '>=',
ast35.LtE: '<=',
ast35.NotEq: '!=',
ast35.Is: 'is',
ast35.IsNot: 'is not',
ast35.In: 'in',
ast35.NotIn: 'not in'
}

def from_comp_operator(self, op):
Expand Down Expand Up @@ -199,7 +199,7 @@ def visit_FunctionDef(self, n):
arg_kinds = [arg.kind for arg in args]
arg_names = [arg.variable.name() for arg in args]
if n.type_comment is not None:
func_type_ast = typed_ast.parse(n.type_comment, '<func_type>', 'func_type')
func_type_ast = ast35.parse(n.type_comment, '<func_type>', 'func_type')
arg_types = [a if a is not None else AnyType() for
a in TypeConverter(line=n.lineno).visit(func_type_ast.argtypes)]
return_type = TypeConverter(line=n.lineno).visit(func_type_ast.returns)
Expand Down Expand Up @@ -276,9 +276,9 @@ def make_argument(arg, default, kind):
# stmt* body, expr* decorator_list, expr? returns, string? type_comment)

def stringify_name(self, n):
if isinstance(n, typed_ast.Name):
if isinstance(n, ast35.Name):
return n.id
elif isinstance(n, typed_ast.Attribute):
elif isinstance(n, ast35.Attribute):
return "{}.{}".format(self.stringify_name(n.value), n.attr)
else:
assert False, "can't stringify " + str(type(n))
Expand Down Expand Up @@ -451,9 +451,9 @@ def visit_BoolOp(self, n):
# mypy translates (1 and 2 and 3) as (1 and (2 and 3))
assert len(n.values) >= 2
op = None
if isinstance(n.op, typed_ast.And):
if isinstance(n.op, ast35.And):
op = 'and'
elif isinstance(n.op, typed_ast.Or):
elif isinstance(n.op, ast35.Or):
op = 'or'
else:
raise RuntimeError('unknown BoolOp ' + str(type(n)))
Expand Down Expand Up @@ -481,13 +481,13 @@ def visit_BinOp(self, n):
@with_line
def visit_UnaryOp(self, n):
op = None
if isinstance(n.op, typed_ast.Invert):
if isinstance(n.op, ast35.Invert):
op = '~'
elif isinstance(n.op, typed_ast.Not):
elif isinstance(n.op, ast35.Not):
op = 'not'
elif isinstance(n.op, typed_ast.UAdd):
elif isinstance(n.op, ast35.UAdd):
op = '+'
elif isinstance(n.op, typed_ast.USub):
elif isinstance(n.op, ast35.USub):
op = '-'

if op is None:
Expand All @@ -498,7 +498,7 @@ def visit_UnaryOp(self, n):
# Lambda(arguments args, expr body)
@with_line
def visit_Lambda(self, n):
body = typed_ast.Return(n.body)
body = ast35.Return(n.body)
body.lineno = n.lineno

return FuncExpr(self.transform_args(n.args, n.lineno),
Expand Down Expand Up @@ -578,7 +578,7 @@ def visit_Compare(self, n):
@with_line
def visit_Call(self, n):
def is_stararg(a):
return isinstance(a, typed_ast.Starred)
return isinstance(a, ast35.Starred)

def is_star2arg(k):
return k.arg is None
Expand Down Expand Up @@ -627,8 +627,8 @@ def visit_Ellipsis(self, n):
# Attribute(expr value, identifier attr, expr_context ctx)
@with_line
def visit_Attribute(self, n):
if (isinstance(n.value, typed_ast.Call) and
isinstance(n.value.func, typed_ast.Name) and
if (isinstance(n.value, ast35.Call) and
isinstance(n.value.func, ast35.Name) and
n.value.func.id == 'super'):
return SuperExpr(n.attr)

Expand Down Expand Up @@ -676,7 +676,7 @@ def visit_Index(self, n):
return self.visit(n.value)


class TypeConverter(typed_ast.NodeTransformer):
class TypeConverter(ast35.NodeTransformer):
def __init__(self, line=-1):
self.line = line

Expand All @@ -701,14 +701,14 @@ def visit_Str(self, n):

# Subscript(expr value, slice slice, expr_context ctx)
def visit_Subscript(self, n):
assert isinstance(n.slice, typed_ast.Index)
assert isinstance(n.slice, ast35.Index)

value = self.visit(n.value)

assert isinstance(value, UnboundType)
assert not value.args

if isinstance(n.slice.value, typed_ast.Tuple):
if isinstance(n.slice.value, ast35.Tuple):
params = self.visit(n.slice.value.elts)
else:
params = [self.visit(n.slice.value)]
Expand Down
0