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

Unexpected unused type ignore alt #16680

Closed

Conversation

seddonym
Copy link

(Explain how this PR changes mypy.)

This test currently fails with this error:

    AssertionError: Command 3 (dmypy check -- bar.py) did not give expected output
    --- Captured stderr call ---
    Expected:
      bar.py:2: error: Unused "type: ignore" comment (diff)
      == Return code: 1                              (diff)
    Actual:
      (empty)

It demonstrates a bug that when an module is removed using
`FineGrainedBuildManager.update` because it is not "seen" by
`fine_grained_increment_follow_imports`, then "unused type: ignore"
warnings disappear from subsequent checks.

Ref: python#9655
This test fails with the error:

    AssertionError: Command 3 (dmypy check -- bar.py) did not give expected output
    --- Captured stderr call ---
    Expected:
      bar.py:2: error: "type: ignore" comment without error code  [ignore-without-code] (diff)
      == Return code: 1                             (diff)
    Actual:
      (empty)

This test illustrates that '"type: ignore" comment without error code'
errors currently disappear in the same way that 'Unused "type: ignore"'
errors do as described in python#9655.

Ref: python#9655
This test fails with the error:

    AssertionError: Command 3 (dmypy check -- bar.py) did not give expected output
    --- Captured stderr call ---
    Expected:
      bar.py:4: error: Name "a" may be undefined  [possibly-undefined] (diff)
      == Return code: 1                             (diff)
    Actual:
      (empty)

This test illustrates that possibly-undefined errors currently disappear
in the same way that 'Unused "type: ignore"' errors do as described in python#9655.

Ref: python#9655
These tests show how some errors disappear on a re-run of dmypy after a
file is altered.
This which fixes issue
python#9655 wherein some types of error
would be lost when a file was re-processed by dmypy. Regression tests
are also included.

This also fixes another error where sometimes files would not be
re-processed by dmypy if the only error in the file was either "unused
type ignore" or "ignore without code".
This catches a regression caused by the previous commits where "type:
ignore" comments are erroneously marked as unused in re-runs of dmypy.

As far as I can tell, this only happens in modules which contain an
import that we don't know how to type (such as a module which does not
exist), and a submodule which is unused.
Before this change, fine-grained builds could spuriously mark ignored
legitimate errors as "unused ignores".

By keeping track of these ignored errors we ensure that enough analysis
is done to know that the ignored lines are actually useful.

We have to change is_errors_for_file so that we don't consider files as
faulty when all of their errors were hidden.
@@ -3078,7 +3078,7 @@ def load_graph(
path=bs.path,
source=bs.text,
manager=manager,
root_source=not bs.followed,
root_source=not (bs.followed or bs.module == "builtins"),
Copy link
Author

Choose a reason for hiding this comment

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

Prior to this, root_source was True for builtins, which seemed incorrect? But I'm not sure of the intent.

The reason for adding this here is because of this check when constructing the State:

https://github.com/seddonym/mypy/blob/7e61402902f14fae93c0327d146172657f43c4c4/mypy/build.py#L1941

This comment has been minimized.

@seddonym seddonym force-pushed the unexpected-unused-type-ignore-alt branch from 43b4256 to e9f5c1a Compare December 19, 2023 15:13

This comment has been minimized.

Prior to this commit, messages would sometimes not be present in
only_once_messages, causing an error. This uses the discard method which
won't error if the message isn't present.
@seddonym seddonym force-pushed the unexpected-unused-type-ignore-alt branch from 8f825d7 to cbf1ed2 Compare December 19, 2023 15:35

This comment has been minimized.

@seddonym seddonym force-pushed the unexpected-unused-type-ignore-alt branch from 51a89ea to 0bb21c6 Compare December 19, 2023 17:14
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@seddonym seddonym closed this Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants