Skip to content

Commit c245e91

Browse files
authored
Add test and docs for top level mypy: disable-error-code comment (#14810)
Closes #14750
1 parent e712634 commit c245e91

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

docs/source/common_issues.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Ignoring a whole file
186186

187187
* To only ignore errors, use a top-level ``# mypy: ignore-errors`` comment instead.
188188
* To only ignore errors with a specific error code, use a top-level
189-
``# mypy: disable-error-code=...`` comment.
189+
``# mypy: disable-error-code="..."`` comment. Example: ``# mypy: disable-error-code="truthy-bool, ignore-without-code"``
190190
* To replace the contents of a module with ``Any``, use a per-module ``follow_imports = skip``.
191191
See :ref:`Following imports <follow-imports>` for details.
192192

mypy/fastparse.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,13 @@ def translate_stmt_list(
487487
and self.type_ignores
488488
and min(self.type_ignores) < self.get_lineno(stmts[0])
489489
):
490-
if self.type_ignores[min(self.type_ignores)]:
490+
ignores = self.type_ignores[min(self.type_ignores)]
491+
if ignores:
492+
joined_ignores = ", ".join(ignores)
491493
self.fail(
492494
(
493495
"type ignore with error code is not supported for modules; "
494-
"use `# mypy: disable-error-code=...`"
496+
f'use `# mypy: disable-error-code="{joined_ignores}"`'
495497
),
496498
line=min(self.type_ignores),
497499
column=0,

test-data/unit/check-errorcodes.test

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,11 @@ def f(d: D, s: str) -> None:
960960
[typing fixtures/typing-typeddict.pyi]
961961

962962
[case testRecommendErrorCode]
963-
# type: ignore[whatever] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code=...` [syntax]
963+
# type: ignore[whatever] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code="whatever"` [syntax]
964+
1 + "asdf"
965+
966+
[case testRecommendErrorCode2]
967+
# type: ignore[whatever, other] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code="whatever, other"` [syntax]
964968
1 + "asdf"
965969

966970
[case testShowErrorCodesInConfig]

test-data/unit/check-inline-config.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,12 @@ enable_error_code = ignore-without-code, truthy-bool
210210

211211
\[mypy-tests.*]
212212
disable_error_code = ignore-without-code
213+
214+
[case testInlineErrorCodesMultipleCodes]
215+
# mypy: disable-error-code="truthy-bool, ignore-without-code"
216+
class Foo:
217+
pass
218+
219+
foo = Foo()
220+
if foo: ...
221+
42 + "no" # type: ignore

0 commit comments

Comments
 (0)