Skip to content
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

Ignoring the rest of a file with a single # type: ignore comment on Python 3.8+. #6841

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ including imports or docstrings) has the effect of ignoring the *entire* module.

foo.bar()

When running mypy with Python 3.8 or later, a ``# type: ignore`` comment
anywhere at the top indentation level of a module will skip type checking for
all remaining lines in the file.

.. code-block:: python

"""Docstring."""

import spam
import eggs
import foo

eggs.fry() # This code is still checked!

# type: ignore

foo.bar() # Mypy skips over everything here.
foo.baz()

Unexpected errors about 'None' and/or 'Optional' types
------------------------------------------------------

Expand Down
22 changes: 21 additions & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,27 @@ def translate_stmt_list(self,
return [block]

res = [] # type: List[Statement]
for stmt in stmts:
line = 0

for i, stmt in enumerate(stmts):

if ismodule: # This line needs to be split for mypy to branch on version:
if sys.version_info >= (3, 8):
# In Python 3.8+ (we need end_lineno), a "# type: ignore" comment
# between statements at the top level of a module skips checking
# for everything else:
ignores = set(range(line + 1, self.get_lineno(stmt))) & self.type_ignores

if ignores:
self.errors.used_ignored_lines[self.errors.file].add(min(ignores))
rest = self.fix_function_overloads(self.translate_stmt_list(stmts[i:]))
block = Block(rest)
block.is_unreachable = True
res.append(block)
return res

line = stmt.end_lineno if stmt.end_lineno is not None else stmt.lineno

node = self.visit(stmt)
res.append(node)

Expand Down
5 changes: 5 additions & 0 deletions test-data/unit/check-38.test
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,8 @@ def g(x: int): ...
/
0 # type: ignore
) # type: ignore # E: unused 'type: ignore' comment

[case testIgnoreRestOfModule]
ERROR # E: Name 'ERROR' is not defined
# type: ignore
IGNORE