From 261132e74c36051057c159cb1f6a181443d9c658 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Fri, 22 May 2020 17:07:55 +0300 Subject: [PATCH] Add support for 'from __future__ import annotations' PEP 563 brought a new `__future__` import for post-poning evaluation of annotations that introduced in 3.7. This patch adds support for that future feature, and removes 'all_feature_names' from that list since it is not valid a syntax (`from __future__ import all_feature_names`). Also it fixes a bug related usage of `ALLOWED_FUTURES` (global and version independant flags) instead of `allowed_futures` (extended version of the previ ous flag that has some version specific flags, probably unnoticed) --- parso/python/errors.py | 9 +++++---- test/test_python_errors.py | 7 +++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/parso/python/errors.py b/parso/python/errors.py index eba4181..d47411a 100644 --- a/parso/python/errors.py +++ b/parso/python/errors.py @@ -13,8 +13,8 @@ _STAR_EXPR_PARENTS = ('testlist_star_expr', 'testlist_comp', 'exprlist') _MAX_BLOCK_SIZE = 20 _MAX_INDENT_COUNT = 100 ALLOWED_FUTURES = ( - 'all_feature_names', 'nested_scopes', 'generators', 'division', - 'absolute_import', 'with_statement', 'print_function', 'unicode_literals', + 'nested_scopes', 'generators', 'division', 'absolute_import', + 'with_statement', 'print_function', 'unicode_literals', ) _COMP_FOR_TYPES = ('comp_for', 'sync_comp_for') @@ -622,13 +622,14 @@ class _FutureImportRule(SyntaxRule): allowed_futures = list(ALLOWED_FUTURES) if self._normalizer.version >= (3, 5): allowed_futures.append('generator_stop') - + if self._normalizer.version >= (3, 7): + allowed_futures.append('annotations') if name == 'braces': self.add_issue(node, message="not a chance") elif name == 'barry_as_FLUFL': m = "Seriously I'm not implementing this :) ~ Dave" self.add_issue(node, message=m) - elif name not in ALLOWED_FUTURES: + elif name not in allowed_futures: message = "future feature %s is not defined" % name self.add_issue(node, message=message) diff --git a/test/test_python_errors.py b/test/test_python_errors.py index 31b4c2e..3f6dde7 100644 --- a/test/test_python_errors.py +++ b/test/test_python_errors.py @@ -185,12 +185,13 @@ def test_statically_nested_blocks(): def test_future_import_first(): - def is_issue(code, *args): + def is_issue(code, *args, **kwargs): code = code % args - return bool(_get_error_list(code)) + return bool(_get_error_list(code, **kwargs)) i1 = 'from __future__ import division' i2 = 'from __future__ import absolute_import' + i3 = 'from __future__ import annotations' assert not is_issue(i1) assert not is_issue(i1 + ';' + i2) assert not is_issue(i1 + '\n' + i2) @@ -201,6 +202,8 @@ def test_future_import_first(): assert not is_issue('""\n%s;%s', i1, i2) assert not is_issue('"";%s;%s ', i1, i2) assert not is_issue('"";%s\n%s ', i1, i2) + assert not is_issue(i3, version="3.7") + assert is_issue(i3, version="3.6") assert is_issue('1;' + i1) assert is_issue('1\n' + i1) assert is_issue('"";1\n' + i1)