Skip to content

Type ignore comments erroneously marked as unused by dmypy #15043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,8 @@ def generate_unused_ignore_errors(self, file: str) -> None:
code=codes.UNUSED_IGNORE,
blocker=False,
only_once=False,
origin=(self.file, [line]),
target=self.target_module,
)
self._add_error_info(file, info)

Expand Down Expand Up @@ -837,6 +839,8 @@ def generate_ignore_without_code_errors(
code=codes.IGNORE_WITHOUT_CODE,
blocker=False,
only_once=False,
origin=(self.file, [line]),
target=self.target_module,
)
self._add_error_info(file, info)

Expand Down
2 changes: 2 additions & 0 deletions mypy/server/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,8 @@ def restore(ids: list[str]) -> None:
state.type_check_first_pass()
state.type_check_second_pass()
state.detect_possibly_undefined_vars()
state.generate_unused_ignore_notes()
state.generate_ignore_without_code_notes()
t2 = time.time()
state.finish_passes()
t3 = time.time()
Expand Down
137 changes: 137 additions & 0 deletions test-data/unit/daemon.test
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,143 @@ from demo.test import a
[file demo/test.py]
a: int

[case testUnusedTypeIgnorePreservedOnRerun]
-- Regression test for https://github.com/python/mypy/issues/9655
$ dmypy start -- --warn-unused-ignores --no-error-summary --hide-error-codes
Daemon started
$ dmypy check -- bar.py
bar.py:2: error: Unused "type: ignore" comment
== Return code: 1
$ dmypy check -- bar.py
bar.py:2: error: Unused "type: ignore" comment
== Return code: 1

[file foo/__init__.py]
[file foo/empty.py]
[file bar.py]
from foo.empty import *
a = 1 # type: ignore

[case testTypeIgnoreWithoutCodePreservedOnRerun]
-- Regression test for https://github.com/python/mypy/issues/9655
$ dmypy start -- --enable-error-code ignore-without-code --no-error-summary
Daemon started
$ dmypy check -- bar.py
bar.py:2: error: "type: ignore" comment without error code [ignore-without-code]
== Return code: 1
$ dmypy check -- bar.py
bar.py:2: error: "type: ignore" comment without error code [ignore-without-code]
== Return code: 1

[file foo/__init__.py]
[file foo/empty.py]
[file bar.py]
from foo.empty import *
a = 1 # type: ignore

[case testPossiblyUndefinedVarsPreservedAfterRerun]
-- Regression test for https://github.com/python/mypy/issues/9655
$ dmypy start -- --enable-error-code possibly-undefined --no-error-summary
Daemon started
$ dmypy check -- bar.py
bar.py:4: error: Name "a" may be undefined [possibly-undefined]
== Return code: 1
$ dmypy check -- bar.py
bar.py:4: error: Name "a" may be undefined [possibly-undefined]
== Return code: 1

[file foo/__init__.py]
[file foo/empty.py]
[file bar.py]
from foo.empty import *
if False:
a = 1
a

[case testUnusedTypeIgnorePreservedOnRerunWithIgnoredMissingImports]
$ dmypy start -- --no-error-summary --ignore-missing-imports --warn-unused-ignores
Daemon started
$ dmypy check foo
foo/main.py:3: error: Unused "type: ignore" comment [unused-ignore]
== Return code: 1
$ dmypy check foo
foo/main.py:3: error: Unused "type: ignore" comment [unused-ignore]
== Return code: 1

[file unused/__init__.py]
[file unused/submodule.py]
[file foo/empty.py]
[file foo/__init__.py]
from foo.main import *
from unused.submodule import *
[file foo/main.py]
from foo import empty
from foo.does_not_exist import *
a = 1 # type: ignore

[case testModuleDoesNotExistPreservedOnRerun]
$ dmypy start -- --no-error-summary --ignore-missing-imports
Daemon started
$ dmypy check foo
foo/main.py:1: error: Module "foo" has no attribute "does_not_exist" [attr-defined]
== Return code: 1
$ dmypy check foo
foo/main.py:1: error: Module "foo" has no attribute "does_not_exist" [attr-defined]
== Return code: 1

[file unused/__init__.py]
[file unused/submodule.py]
[file foo/__init__.py]
from foo.main import *
[file foo/main.py]
from foo import does_not_exist
from unused.submodule import *

[case testReturnTypeIgnoreAfterUnknownImport]
-- Return type ignores after unknown imports and unused modules are respected on the second pass.
$ dmypy start -- --warn-unused-ignores --no-error-summary
Daemon started
$ dmypy check -- foo.py
foo.py:2: error: Cannot find implementation or library stub for module named "a_module_which_does_not_exist" [import-not-found]
foo.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
== Return code: 1
$ dmypy check -- foo.py
foo.py:2: error: Cannot find implementation or library stub for module named "a_module_which_does_not_exist" [import-not-found]
foo.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
== Return code: 1

[file unused/__init__.py]
[file unused/empty.py]
[file foo.py]
from unused.empty import *
import a_module_which_does_not_exist
def is_foo() -> str:
return True # type: ignore

[case testAttrsTypeIgnoreAfterUnknownImport]
$ dmypy start -- --warn-unused-ignores --no-error-summary
Daemon started
$ dmypy check -- foo.py
foo.py:3: error: Cannot find implementation or library stub for module named "a_module_which_does_not_exist" [import-not-found]
foo.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
== Return code: 1
$ dmypy check -- foo.py
foo.py:3: error: Cannot find implementation or library stub for module named "a_module_which_does_not_exist" [import-not-found]
foo.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
== Return code: 1

[file unused/__init__.py]
[file unused/empty.py]
[file foo.py]
import attr
from unused.empty import *
import a_module_which_does_not_exist

@attr.frozen
class A:
def __init__(self) -> None:
self.__attrs_init__() # type: ignore[attr-defined]

[case testDaemonImportAncestors]
$ dmypy run test.py
Daemon started
Expand Down
24 changes: 24 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -10540,6 +10540,30 @@ from pkg.sub import modb
[out]
==

[case testUnusedTypeIgnorePreservedAfterChange]
# flags: --warn-unused-ignores --no-error-summary
[file main.py]
a = 1 # type: ignore
[file main.py.2]
a = 1 # type: ignore
# Comment to trigger reload.
[out]
main.py:1: error: Unused "type: ignore" comment
==
main.py:1: error: Unused "type: ignore" comment

[case testTypeIgnoreWithoutCodePreservedAfterChange]
# flags: --enable-error-code ignore-without-code --no-error-summary
[file main.py]
a = 1 # type: ignore
[file main.py.2]
a = 1 # type: ignore
# Comment to trigger reload.
[out]
main.py:1: error: "type: ignore" comment without error code
==
main.py:1: error: "type: ignore" comment without error code

[case testFineGrainedFunctoolsPartial]
import m

Expand Down