diff options
author | Michael J. Sullivan <sully@msully.net> | 2019-08-12 06:00:46 -0700 |
---|---|---|
committer | Ian Stapleton Cordasco <graffatcolmingov@gmail.com> | 2019-08-12 08:00:46 -0500 |
commit | fa92556ca04214fbe292b3d007b34059e8490b5e (patch) | |
tree | 4fefa65f1352cc7d97ff2100cd0fe78a4967d993 | |
parent | cc2977ff8cbb0e2b668413a73a0c3a6a4b4f02e8 (diff) | |
download | pyflakes-fa92556ca04214fbe292b3d007b34059e8490b5e.tar.gz |
In PEP 484 type comments, allow text after "# type: ignore" (#455)
* In PEP 484 type comments, allow text after "# type: ignore"
This is to support allowing typecheckers to implement ignores for
specific errors, using syntax like `# type: ignore=E1000` or
`# type: ignore[type-mismatch` or some such.
mypy is about to add support for ignoring specific errors following
this design: https://github.com/python/mypy/issues/7239
Support for extra text in type comments was implemented
in CPython as https://bugs.python.org/issue36878
and in typed_ast as https://github.com/python/typed_ast/pull/116.
* add test back
-rw-r--r-- | pyflakes/checker.py | 10 | ||||
-rw-r--r-- | pyflakes/test/test_checker.py | 2 | ||||
-rw-r--r-- | pyflakes/test/test_type_annotations.py | 21 |
3 files changed, 28 insertions, 5 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py index f9ae0aa..f2e579f 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -76,11 +76,13 @@ else: FOR_TYPES = (ast.For,) LOOP_TYPES = (ast.While, ast.For) -# https://github.com/python/typed_ast/blob/55420396/ast27/Parser/tokenizer.c#L102-L104 +# https://github.com/python/typed_ast/blob/1.4.0/ast27/Parser/tokenizer.c#L102-L104 TYPE_COMMENT_RE = re.compile(r'^#\s*type:\s*') -# https://github.com/python/typed_ast/blob/55420396/ast27/Parser/tokenizer.c#L1400 -TYPE_IGNORE_RE = re.compile(TYPE_COMMENT_RE.pattern + r'ignore\s*(#|$)') -# https://github.com/python/typed_ast/blob/55420396/ast27/Grammar/Grammar#L147 +# https://github.com/python/typed_ast/blob/1.4.0/ast27/Parser/tokenizer.c#L1408-L1413 +ASCII_NON_ALNUM = ''.join([chr(i) for i in range(128) if not chr(i).isalnum()]) +TYPE_IGNORE_RE = re.compile( + TYPE_COMMENT_RE.pattern + r'ignore([{}]|$)'.format(ASCII_NON_ALNUM)) +# https://github.com/python/typed_ast/blob/1.4.0/ast27/Grammar/Grammar#L147 TYPE_FUNC_RE = re.compile(r'^(\(.*?\))\s*->\s*(.*)$') diff --git a/pyflakes/test/test_checker.py b/pyflakes/test/test_checker.py index f47588d..b527572 100644 --- a/pyflakes/test/test_checker.py +++ b/pyflakes/test/test_checker.py @@ -154,7 +154,7 @@ class CollectTypeCommentsTests(TestCase): def test_type_comment_starts_with_word_ignore(self): ret = self._collect('x = 1 # type: ignore[T]') - self.assertSetEqual(ret, {(ast.Assign, ('# type: ignore[T]',))}) + self.assertSetEqual(ret, set()) def test_last_node_wins(self): """ diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py index 9c34dcf..bebdef7 100644 --- a/pyflakes/test/test_type_annotations.py +++ b/pyflakes/test/test_type_annotations.py @@ -343,6 +343,27 @@ class TestTypeAnnotations(TestCase): # type: F """) + def test_typeIgnore(self): + self.flakes(""" + a = 0 # type: ignore + b = 0 # type: ignore[excuse] + c = 0 # type: ignore=excuse + d = 0 # type: ignore [excuse] + e = 0 # type: ignore whatever + """) + + def test_typeIgnoreBogus(self): + self.flakes(""" + x = 1 # type: ignored + """, m.UndefinedName) + + def test_typeIgnoreBogusUnicode(self): + error = (m.CommentAnnotationSyntaxError if version_info < (3,) + else m.UndefinedName) + self.flakes(""" + x = 2 # type: ignore\xc3 + """, error) + @skipIf(version_info < (3,), 'new in Python 3') def test_return_annotation_is_class_scope_variable(self): self.flakes(""" |