Skip to content

Commit

Permalink
Ensure wholly ignored files don't get errors about missing codes
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJCLaw committed Nov 28, 2021
1 parent 57311e0 commit a99fd9b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,10 @@ def __str__(self) -> str:
# Syntax errors are often blocking.
SYNTAX: Final = ErrorCode("syntax", "Report syntax errors", "General")

# This is an internal marker code for a whole-file ignore. It is not intended to
# be user-visible.
FILE: Final = ErrorCode("file", "Internal marker for a whole file being ignored", "General")
del error_codes[FILE.code]

# This is a catch-all for remaining uncategorized errors.
MISC: Final = ErrorCode("misc", "Miscellaneous other checks", "General")
7 changes: 7 additions & 0 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,13 @@ def generate_ignore_without_code_errors(self,
return

used_ignored_lines = self.used_ignored_lines[file]

# If the whole file is ignored, ignore it.
if used_ignored_lines:
_, used_codes = min(used_ignored_lines.items())
if codes.FILE.code in used_codes:
return

for line, ignored_codes in self.ignored_lines[file].items():
if ignored_codes:
continue
Expand Down
2 changes: 1 addition & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def translate_stmt_list(self,
if (ismodule and stmts and self.type_ignores
and min(self.type_ignores) < self.get_lineno(stmts[0])):
self.errors.used_ignored_lines[self.errors.file][min(self.type_ignores)].append(
codes.MISC.code)
codes.FILE.code)
block = Block(self.fix_function_overloads(self.translate_stmt_list(stmts)))
mark_block_unreachable(block)
return [block]
Expand Down
2 changes: 1 addition & 1 deletion mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def translate_stmt_list(self,
if (module and stmts and self.type_ignores
and min(self.type_ignores) < self.get_lineno(stmts[0])):
self.errors.used_ignored_lines[self.errors.file][min(self.type_ignores)].append(
codes.MISC.code)
codes.FILE.code)
block = Block(self.fix_function_overloads(self.translate_stmt_list(stmts)))
mark_block_unreachable(block)
return [block]
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ y # type: ignore # E: "type: ignore" comment without error code
# flags: --disallow-ignore-without-code --warn-unused-ignores
"x" # type: ignore # E: Unused "type: ignore" comment

[case testErrorCodeMissingWholeFileIgnores]
# flags: --disallow-ignore-without-code
# type: ignore # whole file ignore
x
y # type: ignore # ignore the lack of error code since we're ignore the whole file

[case testErrorCodeIgnoreWithExtraSpace]
x # type: ignore [name-defined]
x2 # type: ignore [ name-defined ]
Expand Down

0 comments on commit a99fd9b

Please sign in to comment.