Skip to content

cached runs of dmypy stop reporting "unused type: ignore" warnings #9655

Open

Description

@rggjan

Using mypy 0.782, I run
dmypy run .

on my codebase. The first time it runs, it correctly reports a few

error: unused 'type: ignore' comment

same as mypy. However, when rerunning dmypy run ., these errors disappear, while all other errors remain.

For some reason, this type of warning (warn_unused_ignores) seems to be missed sometimes when using the cache.

Unfortunately, it doesn't seem trivial to reproduce, so I can't give a reproducible project right now...

Activity

  1. jgillard commented on Mar 30, 2022

    @jgillard

    I noticed this today with mypy 0.942, so this is still an issue.

    > dmypy run
    Success: no issues found in 604 source files
    
    > mypy --cache-dir=/dev/null
    apps/matchmaking/services.py:260: error: Unused "type: ignore" comment
    Found 1 error in 1 file (checked 604 source files)
    
    > mypy --version
    mypy 0.942
  2. peterbygrave commented on Nov 30, 2022

    @peterbygrave

    This is still a problem in 0.991

  3. AlexWaygood commented on Nov 30, 2022

    @AlexWaygood
    Member

    Has anybody managed to find a reproducible example yet?

  4. meshy commented on Feb 20, 2023

    @meshy
    Contributor

    I have recently encountered this on 1.0.1. I have not yet produced a minimal example.

    In my case the bulk of the unused type: ignore warnings which disappear are on lines that have been typed by the django-stubs project. I wonder if it may have something to do with that project, or with plugins in general.

  5. meshy commented on Feb 27, 2023

    @meshy
  6. meshy commented on Mar 3, 2023

    @meshy
    Contributor

    I have a new example that doesn't have any external requirements.

    unused/__init__.py
    (empty file)

    unused/empty.py
    (empty file)

    example.py

    from unused.empty import *
    a = 1  # type: ignore

    Instructions (in a clean virtualenv)

    $ dmypy stop
    Daemon stopped
    
    $ dmypy run -- --package example --warn-unused-ignores
    Daemon started
    example.py:2: error: Unused "type: ignore" comment
    Found 1 error in 1 file (checked 46 source files)
    
    $ dmypy run -- --package example --warn-unused-ignores
    Success: no issues found in 1 source file

    From what I can tell this issue happens when we call fine_grained_manager.update([], to_delete) in dmypy_server.Server.fine_grained_increment_follow_imports.

    In this case, to_delete is [('unused', '/.../mypy-9655/unused/__init__.py')].

    For some reason, when this update is made, the Unused "type: ignore" comment also goes missing.

    Next up: dig into mypy, and work out what's going on.

    EDIT: I have created a failing test based on this example, and continue to investigate a fix.

  7. added a commit that references this issue on Mar 4, 2023
  8. 46 remaining items

  9. meshy commented on Jan 2, 2024

    @meshy
    Contributor

    If anyone following this thread is able to check if #15043 works for them, I'd appreciate the feedback.

  10. m-honel-ywh2bt commented on Jan 2, 2024

    @m-honel-ywh2bt

    @meshy Using #15043, I can confirm that Unused "type: ignore" comment errors seem to appear everytime dmypy is ran, without having to restart the daemon. 👌🏻

  11. JukkaL commented on Feb 5, 2024

    @JukkaL
    Collaborator

    I think there is another case where type ignore comments still aren't being reported. Here are a few failing test cases:

    [case testUnusedTypeIgnorePreservedAfterChangeInOtherModule1]
    # flags: --warn-unused-ignores
    [file main.py]
    import a
    
    def f() -> None:
        1()
        a = 1  # type: ignore
    
    [file a.py]
    [file a.py.2]
    x = 1
    
    [out]
    main.py:4: error: "int" not callable
    main.py:5: error: Unused "type: ignore" comment
    ==
    main.py:4: error: "int" not callable
    main.py:5: error: Unused "type: ignore" comment
    
    [case testUnusedTypeIgnorePreservedAfterChangeInOtherModule2]
    # flags: --warn-unused-ignores
    [file main.py]
    import a
    
    def f() -> None:
        1()
        a = 1  # type: ignore
    
    def g() -> None:
        1()  # type: ignore
    
    [file a.py]
    [file a.py.2]
    x = 1
    
    [out]
    main.py:4: error: "int" not callable
    main.py:5: error: Unused "type: ignore" comment
    ==
    main.py:4: error: "int" not callable
    main.py:5: error: Unused "type: ignore" comment
    
    

    The error on line 5 isn't reported on the second step.

    The root cause is that we don't report unused type ignores in nodes only processed using reprocess_nodes, i.e. when following fine-grained dependencies or reprocessing nodes that previously generated errors.

    I think this is a potential way to fix this (but I haven't thought about this very carefully yet):

    • Collect a set of all the triggered/processed nodes in propagate_changes_using_dependencies.
    • At the end of a daemon update, perform the equivalent of generate_unused_ignore_notes, but only report errors within the previously triggered nodes. The filtering step seems necessary, as some parts of modules are often not processed during a fine-grained increment.
  12. meshy commented on Feb 5, 2024

    @meshy
    Contributor

    @JukkaL good catch.

    Do you think this blocks #15043, or could we tackle that case separately?

  13. JukkaL commented on Feb 5, 2024

    @JukkaL
    Collaborator

    Ah, I forgot to mention that the test cases I mentioned above aren't specifically covered by #15043 yet. It's okay to leave fixing these for another PR, but we shouldn't close this issue until all known cases are fixed.

  14. meshy commented on Feb 5, 2024

    @meshy
    Contributor

    OK great, thank you. I've removed the "fixes" comment from that PR so we avoid closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions