Skip to content

Commit

Permalink
Don't fail on mypy-style '# type: ignore[code, ...]' comments
Browse files Browse the repository at this point in the history
Fixes #485

PiperOrigin-RevId: 289758806
  • Loading branch information
Pytype Team authored and rchen152 committed Jan 23, 2020
1 parent f777035 commit fd7ddc8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pytype/directors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from six import moves

_DIRECTIVE_RE = re.compile(r"#\s*(pytype|type)\s*:\s?([^#]*)")
_IGNORE_RE = re.compile(r"^ignore(\[.+\])?$")
_CLOSING_BRACKETS_RE = re.compile(r"^(\s*[]})]\s*)+(#.*)?$")
_WHITESPACE_RE = re.compile(r"^\s*(#.*)?$")
_CLASS_OR_FUNC_RE = re.compile(r"^(def|class)\s")
Expand Down Expand Up @@ -229,7 +230,9 @@ def _process_type(self, lineno, code, data, is_nested):
self._errorlog.invalid_directive(
self._filename, lineno,
"Multiple type comments on the same line.")
if data == "ignore":
# Also supports mypy-style ignore[code, ...] syntax, treated as regular
# ignores.
if _IGNORE_RE.match(data):
if not code:
self._ignore.start_range(lineno, True)
else:
Expand Down
10 changes: 10 additions & 0 deletions pytype/directors_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ def test_ignore_one_line(self):
self._should_report(False, 3)
self._should_report(True, 4)

def test_ignore_one_line_mypy_style(self):
self._create("""
# line 2
x = 123 # type: ignore[arg-type]
# line 4
""")
self._should_report(True, 2)
self._should_report(False, 3)
self._should_report(True, 4)

def test_utf8(self):
self._create("""
x = u"abc□def\n"
Expand Down

0 comments on commit fd7ddc8

Please sign in to comment.