diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index 17d42d5766ac..c67c6cb728d3 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -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 ------------------------------------------------------ diff --git a/mypy/fastparse.py b/mypy/fastparse.py index ec0556d598fc..788667105167 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -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) diff --git a/test-data/unit/check-38.test b/test-data/unit/check-38.test index 16f279545bc8..376fea612386 100644 --- a/test-data/unit/check-38.test +++ b/test-data/unit/check-38.test @@ -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