Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Ignoring a whole file

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

Expand Down
5 changes: 3 additions & 2 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,12 @@ def translate_stmt_list(
and self.type_ignores
and min(self.type_ignores) < self.get_lineno(stmts[0])
):
if self.type_ignores[min(self.type_ignores)]:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use walrus just yet; we still support 3.7

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first time I've used walrus in real code :)
Probably, the last time.

if ignores := self.type_ignores[min(self.type_ignores)]:
joined_ignores = ", ".join(ignores)
self.fail(
(
"type ignore with error code is not supported for modules; "
"use `# mypy: disable-error-code=...`"
f'use `# mypy: disable-error-code="{joined_ignores}"`'
),
line=min(self.type_ignores),
column=0,
Expand Down
6 changes: 5 additions & 1 deletion test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,11 @@ def f(d: D, s: str) -> None:
[typing fixtures/typing-typeddict.pyi]

[case testRecommendErrorCode]
# type: ignore[whatever] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code=...` [syntax]
# type: ignore[whatever] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code="whatever"` [syntax]
1 + "asdf"

[case testRecommendErrorCode2]
# type: ignore[whatever, other] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code="whatever, other"` [syntax]
1 + "asdf"

[case testShowErrorCodesInConfig]
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-inline-config.test
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,12 @@ enable_error_code = ignore-without-code, truthy-bool

\[mypy-tests.*]
disable_error_code = ignore-without-code

[case testInlineErrorCodesMultipleCodes]
# mypy: disable-error-code="truthy-bool, ignore-without-code"
class Foo:
pass

foo = Foo()
if foo: ...
42 + "no" # type: ignore