Skip to content
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

Type ignore comments erroneously marked as unused by dmypy #15043

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
26 changes: 25 additions & 1 deletion mypy/dmypy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,10 +684,11 @@ def refresh_file(module: str, path: str) -> list[str]:

# Find all original modules in graph that were not reached -- they are deleted.
to_delete = []
seen_and_ancestors = self._seen_and_ancestors(seen)
for module_id in orig_modules:
if module_id not in graph:
continue
if module_id not in seen:
if module_id not in seen_and_ancestors:
module_path = graph[module_id].path
assert module_path is not None
to_delete.append((module_id, module_path))
Expand Down Expand Up @@ -715,6 +716,29 @@ def refresh_file(module: str, path: str) -> list[str]:

return messages

def _seen_and_ancestors(self, seen: set[str]) -> set[str]:
"""Return the set of seen modules along with any ancestors not already in the set.
Copy link

Choose a reason for hiding this comment

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

Great docstring, thanks.


For example, given this set:

{"foo", "foo.bar", "a.b.c"}

... we would expect this set to be returned:

{"foo", "foo.bar", "a.b.c", "a.b", "a"}

This is used to stop us from deleting ancestor modules from the graph
when their descendants have been seen.
"""
seen_paths = seen.copy()
Copy link

Choose a reason for hiding this comment

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

No big deal, but we could do this without any conditionals like this, if you prefer:

    seen_and_ancestors: set[str] = set()
    for module in seen:
        module_parts = module.split(".")
        for i in range(len(module_parts)):
            seen_and_ancestors.add(".".join(module_parts[:i + 1]))
    return seen_and_ancestors

Copy link
Contributor Author

@meshy meshy Jan 2, 2024

Choose a reason for hiding this comment

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

I assumed that in some cases with many sub-modules that my way would be faster so my preference would be to keep it as-is, but I'm not strongly opposed to changing this if there's a strong will to do so?

for module_path in seen:
while module_path := module_path.rpartition(".")[0]:
if module_path in seen_paths:
Copy link

Choose a reason for hiding this comment

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

I think we might be able to drop this conditional and just always add the module_path.

Copy link
Contributor Author

@meshy meshy Jan 2, 2024

Choose a reason for hiding this comment

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

It feels like it'd be quicker to skip this if we have a module with many submodules. I haven't measured this though.

break
else:
seen_paths.add(module_path)
return seen_paths

def find_reachable_changed_modules(
self,
roots: list[BuildSource],
Expand Down
4 changes: 4 additions & 0 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,8 @@ def generate_unused_ignore_errors(self, file: str) -> None:
blocker=False,
only_once=False,
allow_dups=False,
origin=(self.file, [line]),
target=self.target_module,
)
self._add_error_info(file, info)

Expand Down Expand Up @@ -777,6 +779,8 @@ def generate_ignore_without_code_errors(
blocker=False,
only_once=False,
allow_dups=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 @@ -667,6 +667,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 @@ -615,3 +615,140 @@ b: str
from demo.test import a
[file demo/test.py]
a: int

[case testUnusedTypeIgnorePreservedOnRerun]
-- Regression test for https:/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:/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:/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]
24 changes: 24 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -10505,3 +10505,27 @@ 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