Skip to content

Commit d8f09b2

Browse files
committed
Update fastparse for new typed_ast version
1 parent 5aecb26 commit d8f09b2

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

mypy/fastparse.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None,
6565
is_stub_file = bool(fnam) and fnam.endswith('.pyi')
6666
try:
6767
assert pyversion[0] >= 3 or is_stub_file
68-
ast = ast3.parse(source, fnam, 'exec')
68+
ast = ast3.parse(source, fnam, 'exec', feature_version=pyversion[1])
6969

7070
tree = ASTConverter(pyversion=pyversion,
7171
is_stub=is_stub_file,
@@ -120,7 +120,7 @@ def is_no_type_check_decorator(expr: ast3.expr) -> bool:
120120
return False
121121

122122

123-
class ASTConverter(ast3.NodeTransformer):
123+
class ASTConverter(ast3.NodeTransformer): # type: ignore
124124
def __init__(self,
125125
pyversion: Tuple[int, int],
126126
is_stub: bool,
@@ -392,7 +392,13 @@ def make_argument(arg: ast3.arg, default: Optional[ast3.expr], kind: int) -> Arg
392392
if no_type_check:
393393
arg_type = None
394394
else:
395-
arg_type = TypeConverter(self.errors, line=line).visit(arg.annotation)
395+
if arg.annotation is not None and arg.type_comment is not None:
396+
self.fail(messages.DUPLICATE_TYPE_SIGNATURES, arg.lineno, arg.col_offset)
397+
arg_type = None
398+
if arg.annotation is not None:
399+
arg_type = TypeConverter(self.errors, line=line).visit(arg.annotation)
400+
elif arg.type_comment is not None:
401+
arg_type = parse_type_comment(arg.type_comment, arg.lineno, arg.col_offset)
396402
return Argument(Var(arg.arg), arg_type, self.visit(default), kind)
397403

398404
new_args = []
@@ -487,28 +493,24 @@ def visit_Delete(self, n: ast3.Delete) -> DelStmt:
487493
# Assign(expr* targets, expr? value, string? type_comment, expr? annotation)
488494
@with_line
489495
def visit_Assign(self, n: ast3.Assign) -> AssignmentStmt:
490-
typ = None
491-
if hasattr(n, 'annotation') and n.annotation is not None: # type: ignore
492-
new_syntax = True
493-
else:
494-
new_syntax = False
495-
if new_syntax and self.pyversion < (3, 6):
496-
self.fail('Variable annotation syntax is only supported in Python 3.6, '
497-
'use type comment instead', n.lineno, n.col_offset)
498-
# typed_ast prevents having both type_comment and annotation.
496+
lvalues = self.translate_expr_list(n.targets)
497+
rvalue = self.visit(n.value)
499498
if n.type_comment is not None:
500499
typ = parse_type_comment(n.type_comment, n.lineno, self.errors)
501-
elif new_syntax:
502-
typ = TypeConverter(self.errors, line=n.lineno).visit(n.annotation) # type: ignore
503-
typ.column = n.annotation.col_offset
500+
else:
501+
typ = None
502+
return AssignmentStmt(lvalues, rvalue, type=typ, new_syntax=False)
503+
504+
# AnnAssign(expr target, expr annotation, expr? value, int simple)
505+
@with_line
506+
def visit_AnnAssign(self, n: ast3.AnnAssign) -> AssignmentStmt:
504507
if n.value is None: # always allow 'x: int'
505508
rvalue = TempNode(AnyType()) # type: Expression
506509
else:
507510
rvalue = self.visit(n.value)
508-
lvalues = self.translate_expr_list(n.targets)
509-
return AssignmentStmt(lvalues,
510-
rvalue,
511-
type=typ, new_syntax=new_syntax)
511+
typ = TypeConverter(self.errors, line=n.lineno).visit(n.annotation)
512+
typ.column = n.annotation.col_offset
513+
return AssignmentStmt([self.visit(n.target)], rvalue, type=typ, new_syntax=True)
512514

513515
# AugAssign(expr target, operator op, expr value)
514516
@with_line
@@ -815,9 +817,6 @@ def is_star2arg(k: ast3.keyword) -> bool:
815817
# Num(object n) -- a number as a PyObject.
816818
@with_line
817819
def visit_Num(self, n: ast3.Num) -> Union[IntExpr, FloatExpr, ComplexExpr]:
818-
if getattr(n, 'contains_underscores', None) and self.pyversion < (3, 6):
819-
self.fail('Underscores in numeric literals are only supported in Python 3.6',
820-
n.lineno, n.col_offset)
821820
if isinstance(n.n, int):
822821
return IntExpr(n.n)
823822
elif isinstance(n.n, float):
@@ -914,7 +913,7 @@ def visit_Index(self, n: ast3.Index) -> Node:
914913
return self.visit(n.value)
915914

916915

917-
class TypeConverter(ast3.NodeTransformer):
916+
class TypeConverter(ast3.NodeTransformer): # type: ignore
918917
def __init__(self, errors: Errors, line: int = -1) -> None:
919918
self.errors = errors
920919
self.line = line

mypy/fastparse2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
try:
4646
from typed_ast import ast27
47-
from typed_ast import ast3
47+
from typed_ast import ast3 # type: ignore
4848
except ImportError:
4949
if sys.version_info.minor > 2:
5050
print('You must install the typed_ast package before you can run mypy'

test-data/unit/check-newsyntax.test

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[case testNewSyntaxRequire36]
22
# flags: --fast-parser --python-version 3.5
3-
x: int = 5 # E: Variable annotation syntax is only supported in Python 3.6, use type comment instead
3+
x: int = 5 # E: Variable annotation syntax is only supported in Python 3.6 and greater
44
[out]
55

66
[case testNewSyntaxSyntaxError]
@@ -98,3 +98,12 @@ main:4: error: Unexpected type declaration
9898
main:4: error: Unsupported target for indexed assignment
9999
main:5: error: Type cannot be declared in assignment to non-self attribute
100100
main:5: error: "str" has no attribute "x"
101+
102+
[case testNewSyntaxFstringError]
103+
# flags: --fast-parser --python-version 3.5
104+
f'' # E: Format strings are only supported in Python 3.6 and greater
105+
106+
[case testNewSyntaxAsyncComprehensionError]
107+
# flags: --fast-parser --python-version 3.5
108+
async def f():
109+
results = [i async for i in aiter() if i % 2] # E: Async comprehensions are only supported in Python 3.6 and greater

test-data/unit/check-underscores.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[case testUnderscoresRequire36]
22
# flags: --fast-parser --python-version 3.5
3-
x = 1000_000 # E: Underscores in numeric literals are only supported in Python 3.6
3+
x = 1000_000 # E: Underscores in numeric literals are only supported in Python 3.6 and greater
44
[out]
55

66
[case testUnderscoresSyntaxError]

0 commit comments

Comments
 (0)