8000 Update to typed-ast 1.0.0 by ddfisher · Pull Request #2857 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Update to typed-ast 1.0.0 #2857

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
Feb 13, 2017
Merged
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
Update fastparse for new typed_ast version
  • Loading branch information
ddfisher committed Feb 13, 2017
commit 8c2312281b2f2adb4ea271709040b571cac2e29a
45 changes: 22 additions & 23 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None,
is_stub_file = bool(fnam) and fnam.endswith('.pyi')
try:
assert pyversion[0] >= 3 or is_stub_file
ast = ast3.parse(source, fnam, 'exec')
ast = ast3.parse(source, fnam, 'exec', feature_version=pyversion[1])

tree = ASTConverter(pyversion=pyversion,
is_stub=is_stub_file,
Expand Down Expand Up @@ -120,7 +120,7 @@ def is_no_type_check_decorator(expr: ast3.expr) -> bool:
return False


class ASTConverter(ast3.NodeTransformer):
class ASTConverter(ast3.NodeTransformer): # type: ignore
Copy link
Member

Choose a reason for hiding this comment

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

Can you open a typeshed issue to upgrade the typed_ast stubs, and link this (and similer ignores later) to it?

def __init__(self,
pyversion: Tuple[int, int],
is_stub: bool,
Expand Down Expand Up @@ -392,7 +392,13 @@ def make_argument(arg: ast3.arg, default: Optional[ast3.expr], kind: int) -> Arg
if no_type_check:
arg_type = None
else:
arg_type = TypeConverter(self.errors, line=line).visit(arg.annotation)
if arg.annotation is not None and arg.type_comment is not None:
self.fail(messages.DUPLICATE_TYPE_SIGNATURES, arg.lineno, arg.col_offset)
arg_type = None
if arg.annotation is not None:
arg_type = TypeConverter(self.errors, line=line).visit(arg.annotation)
elif arg.type_comment is not None:
arg_type = parse_type_comment(arg.type_comment, arg.lineno, arg.col_offset)
return Argument(Var(arg.arg), arg_type, self.visit(default), kind)
8000
new_args = []
Expand Down Expand Up @@ -487,28 +493,24 @@ def visit_Delete(self, n: ast3.Delete) -> DelStmt:
# Assign(expr* targets, expr? value, string? type_comment, expr? annotation)
@with_line
def visit_Assign(self, n: ast3.Assign) -> AssignmentStmt:
typ = None
if hasattr(n, 'annotation') and n.annotation is not None: # type: ignore
new_syntax = True
else:
new_syntax = False
if new_syntax and self.pyversion < (3, 6):
self.fail('Variable annotation syntax is only supported in Python 3.6, '
'use type comment instead', n.lineno, n.col_offset)
# typed_ast prevents having both type_comment and annotation.
lvalues = self.translate_expr_list(n.targets)
rvalue = self.visit(n.value)
if n.type_comment is not None:
typ = parse_type_comment(n.type_comment, n.lineno, self.errors)
elif new_syntax:
typ = TypeConverter(self.errors, line=n.lineno).visit(n.annotation) # type: ignore
typ.column = n.annotation.col_offset
else:
typ = None
return AssignmentStmt(lvalues, rvalue, type=typ, new_syntax=False)

# AnnAssign(expr target, expr annotation, expr? value, int simple)
@with_line
def visit_AnnAssign(self, n: ast3.AnnAssign) -> AssignmentStmt:
if n.value is None: # always allow 'x: int'
rvalue = TempNode(AnyType()) # type: Expression
else:
rvalue = self.visit(n.value)
lvalues = self.translate_expr_list(n.targets)
return AssignmentStmt(lvalues,
rvalue,
type=typ, new_syntax=new_syntax)
typ = TypeConverter(self.errors, line=n.lineno).visit(n.annotation)
typ.column = n.annotation.col_offset
return AssignmentStmt([self.visit(n.target)], rvalue, type=typ, new_syntax=True)

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


class TypeConverter(ast3.NodeTransformer):
class TypeConverter(ast3.NodeTransformer): # type: ignore
def __init__(self, errors: Errors, line: int = -1) -> None:
self.errors = errors
self.line = line
Expand Down
2 changes: 1 addition & 1 deletion mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

try:
from typed_ast import ast27
from typed_ast import ast3
from typed_ast import ast3 # type: ignore
except ImportError:
if sys.version_info.minor > 2:
print('You must install the typed_ast package before you can run mypy'
Expand Down
11 changes: 10 additions & 1 deletion test-data/unit/check-newsyntax.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[case testNewSyntaxRequire36]
# flags: --fast-parser --python-version 3.5
x: int = 5 # E: Variable annotation syntax is only supported in Python 3.6, use type comment instead
x: int = 5 # E: Variable annotation syntax is only supported in Python 3.6 and greater
[out]

[case testNewSyntaxSyntaxError]
Expand Down Expand Up @@ -98,3 +98,12 @@ main:4: error: Unexpected type declaration
main:4: error: Unsupported target for indexed assignment
main:5: error: Type cannot be declared in assignment to non-self attribute
main:5: error: "str" has no attribute "x"

[case testNewSyntaxFstringError]
# flags: --fast-parser --python-version 3.5
f'' # E: Format strings are only supported in Python 3.6 and greater

[case testNewSyntaxAsyncComprehension 84E2 Error]
# flags: --fast-parser --python-version 3.5
async def f():
results = [i async for i in aiter() if i % 2] # E: Async comprehensions are only supported in Python 3.6 and greater
2 changes: 1 addition & 1 deletion test-data/unit/check-underscores.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[case testUnderscoresRequire36]
# flags: --fast-parser --python-version 3.5
x = 1000_000 # E: Underscores in numeric literals are only supported in Python 3.6
x = 1000_000 # E: Underscores in numeric literals are only supported in Python 3.6 and greater
[out]

[case testUnderscoresSyntaxError]
Expand Down
0