Skip to content

Commit a99fd9b

Browse files
committed
Ensure wholly ignored files don't get errors about missing codes
1 parent 57311e0 commit a99fd9b

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

mypy/errorcodes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,10 @@ def __str__(self) -> str:
138138
# Syntax errors are often blocking.
139139
SYNTAX: Final = ErrorCode("syntax", "Report syntax errors", "General")
140140

141+
# This is an internal marker code for a whole-file ignore. It is not intended to
142+
# be user-visible.
143+
FILE: Final = ErrorCode("file", "Internal marker for a whole file being ignored", "General")
144+
del error_codes[FILE.code]
145+
141146
# This is a catch-all for remaining uncategorized errors.
142147
MISC: Final = ErrorCode("misc", "Miscellaneous other checks", "General")

mypy/errors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,13 @@ def generate_ignore_without_code_errors(self,
492492
return
493493

494494
used_ignored_lines = self.used_ignored_lines[file]
495+
496+
# If the whole file is ignored, ignore it.
497+
if used_ignored_lines:
498+
_, used_codes = min(used_ignored_lines.items())
499+
if codes.FILE.code in used_codes:
500+
return
501+
495502
for line, ignored_codes in self.ignored_lines[file].items():
496503
if ignored_codes:
497504
continue

mypy/fastparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def translate_stmt_list(self,
360360
if (ismodule and stmts and self.type_ignores
361361
and min(self.type_ignores) < self.get_lineno(stmts[0])):
362362
self.errors.used_ignored_lines[self.errors.file][min(self.type_ignores)].append(
363-
codes.MISC.code)
363+
codes.FILE.code)
364364
block = Block(self.fix_function_overloads(self.translate_stmt_list(stmts)))
365365
mark_block_unreachable(block)
366366
return [block]

mypy/fastparse2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def translate_stmt_list(self,
216216
if (module and stmts and self.type_ignores
217217
and min(self.type_ignores) < self.get_lineno(stmts[0])):
218218
self.errors.used_ignored_lines[self.errors.file][min(self.type_ignores)].append(
219-
codes.MISC.code)
219+
codes.FILE.code)
220220
block = Block(self.fix_function_overloads(self.translate_stmt_list(stmts)))
221221
mark_block_unreachable(block)
222222
return [block]

test-data/unit/check-errorcodes.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ y # type: ignore # E: "type: ignore" comment without error code
144144
# flags: --disallow-ignore-without-code --warn-unused-ignores
145145
"x" # type: ignore # E: Unused "type: ignore" comment
146146

147+
[case testErrorCodeMissingWholeFileIgnores]
148+
# flags: --disallow-ignore-without-code
149+
# type: ignore # whole file ignore
150+
x
151+
y # type: ignore # ignore the lack of error code since we're ignore the whole file
152+
147153
[case testErrorCodeIgnoreWithExtraSpace]
148154
x # type: ignore [name-defined]
149155
x2 # type: ignore [ name-defined ]

0 commit comments

Comments
 (0)