From 07e8a3fb9dda73a028a877da909f55bd1ef0f5ed Mon Sep 17 00:00:00 2001 From: rechen Date: Wed, 11 Nov 2020 17:48:09 -0800 Subject: [PATCH] Support `# type: ignore[errorcode, ...]` in pyi files. Came up in https://github.com/python/typeshed/pull/4758 - mypy supports having error codes in brackets after a `# type: ignore`. From https://github.com/python/mypy/blob/master/mypy/errorcodes.py and https://hub.packtpub.com/mypy-0-730-releases-with-more-precise-error-locations-display-error-codes/, it seems error codes look like my-error-code, and you can include multiple by separating them with commas. This change will also allow `# type: ignore[]`, but I think that's fine. PiperOrigin-RevId: 341947562 --- pytype/pyi/parser.yy | 4 ++++ pytype/pyi/parser_test.py | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/pytype/pyi/parser.yy b/pytype/pyi/parser.yy index b4e910c96..62f860336 100644 --- a/pytype/pyi/parser.yy +++ b/pytype/pyi/parser.yy @@ -570,6 +570,10 @@ return typeignore : TYPECOMMENT NAME { Py_DecRef($2); } + | TYPECOMMENT NAME '[' maybe_type_list ']' { + Py_DecRef($2); + Py_DecRef($4); + } ; maybe_body diff --git a/pytype/pyi/parser_test.py b/pytype/pyi/parser_test.py index 511ba6eac..1e10c9df6 100644 --- a/pytype/pyi/parser_test.py +++ b/pytype/pyi/parser_test.py @@ -1075,6 +1075,17 @@ class Foo: __slots__ = ["a", "b"] """) + def test_typeignore_errorcode(self): + self.check(""" + def f() -> None: ... # type: ignore[override] + def g() -> None: ... # type: ignore[var-annotated] + def h() -> None: ... # type: ignore[abstract, no-untyped-def] + """, """ + def f() -> None: ... + def g() -> None: ... + def h() -> None: ... + """) + def test_decorators(self): # These tests are a bit questionable because most of the decorators only # make sense for methods of classes. But this at least gives us some