Skip to content

Cannot silence "unused 'type: ignore'" in version specific code #8823

Description

@serhiy-storchaka

I need to execute some code only on specific Python version (it is the use of TextIOWrapper.reconfigure() added in Python 3.7, but in following example I will use more abstract code).

import sys

if sys.version_info >= (3, 7):
    x: int = 'a'

Since MyPy on Python 3.7 raises an error for it (actually because of the error in typeshed) I need to add # type: ignore:

import sys

if sys.version_info >= (3, 7):
    x: int = 'a'  # type: ignore

But now I will get an error unused 'type: ignore' comment when run MyPy with warn_unused_ignores = True on Python 3.6.

So I cannot use variant 1, because it is an error on 3.7, and cannot use variant 2, because it is an error on 3.6.

MyPy should not complain about unused 'type: ignore' comment in the code which it does not analyze.

MyPy version is 0.770.

Activity

  1. hauntsaninja commented on May 14, 2020

    @hauntsaninja
    Collaborator

    As a workaround for this issue, you can use cast

  2. serhiy-storchaka commented on May 18, 2020

    @serhiy-storchaka
    MemberAuthor

    It does not help in my particular issue.

    The problem is that MyPy skips some parts of the code and complains about unused type: ignore in this code (which would be not ignored if this code be not skipped). It is not consistent and does not have a workaround.

  3. hauntsaninja commented on May 18, 2020

    @hauntsaninja
    Collaborator

    Agree that there is an issue, but I think it can be worked around using cast, since that lets you remove the type ignore altogether:

    ~/delete λ cat test84.py
    import sys
    from typing import cast, Any
    
    if sys.version_info >= (3, 7):
        x: int = cast(Any, 'a')
    ~/delete λ mypy --python-version 3.6 --warn-unused-ignores --warn-redundant-casts test84.py
    Success: no issues found in 1 source file
    ~/delete λ mypy --python-version 3.7 --warn-unused-ignores --warn-redundant-casts test84.py
    Success: no issues found in 1 source file
    

    Looking at the issue on typeshed, you'd need to do something like cast(Any, sys.stdout).reconfigure(errors="replace"). It's not pretty, but it should work.

  4. serhiy-storchaka commented on May 19, 2020

    @serhiy-storchaka
    MemberAuthor

    x: int = 'a' was just an example. It is the simplest way that I know to trigger an error. In real code I use

    sys.stdout.reconfigure(errors="replace")

    and I do not know how to silence an error in this case.

  5. serhiy-storchaka commented on May 19, 2020

    @serhiy-storchaka
    MemberAuthor

    Oh, and thank you for your suggestion. I tried similar workaround, but made an error in the code, so it did not work. Now I see my mistake and fixed it thank to you.

  6. msullivan commented on May 20, 2020

    @msullivan
    Collaborator

    I think we need a way to silence unused type ignore messages for individual type ignores short of disabling the warning.

  7. changed the title [-]Cannot silence type checking in version specific code[/-] [+]Cannot silence "unused 'type: ignore'" in version specific code[/+] on May 20, 2020
  8. wsanchez commented on Oct 1, 2020

    @wsanchez

    I'm running into a by-platform equivalent of this. If I try to import epoll from the select module on macOS, I get an [attr-defined] error, but on Linux, that's fine. Conversely, on Linux, one can't import KQ_FILTER_READ from select.

    So, depending on which platform you run mypy, you get a different error, and I don't know how to suppress that class of error message.

    That might be a different ticket, but it seems similar enough that maybe mentioning it in this one will do…?

  9. wsanchez commented on Oct 1, 2020

    @wsanchez

    Note the select issue I mention above appears to be new between Mypy 0.770 and 0.782.

  10. gvanrossum commented on Oct 1, 2020

    @gvanrossum
    Member

    @wsanchez That is different. Are you using the right syntax for the platform check? Please ask on Gitter.

  11. wsanchez commented on Oct 1, 2020

    @wsanchez

    @gvanrossum Oh, OK, I didn't realize that Mypy could be told to do platform-specific things. I'll read more. twisted/twisted#1416 (comment) describes the issue in a bit more detail.

  12. 21 remaining items

  13. added a commit that references this issue on Sep 8, 2022
  14. added a commit that references this issue on Sep 8, 2022
  15. senarvi commented on Mar 17, 2023

    @senarvi

    A possible workaround for the @stinovlas's import use case is to use getattr instead:

    try:
        TestCase: Any = getattr(unittest, 'IsolatedAsyncioTestCase')
    except AttributeError:  # pragma: no cover
        TestCase = getattr(asynctest, 'TestCase')

    I've been struggling with this issue with try/except imports. I thought I had tried everything already, but this worked!

  16. matteosantama commented on Mar 26, 2023

    @matteosantama

    I know "me too!" comments aren't especially helpful, but here's an example where the suggested workaround (using cast) does not work.

    import sys
    
    from typing import cast, Callable
    
    if sys.version_info >= (3, 8):
        from importlib.metadata import version
    else:
        from importlib_metadata import version
    
    cast(Callable[[str], str], version)
    
    __version__ = version("my-lib")
    ❯ mypy --python-version 3.10
    Success: no issues found in 37 source files
    
    ❯ mypy --python-version 3.7
    my-lib/__init__.py:12: error: Call to untyped function "version" in typed context  [no-untyped-call]
    Found 1 error in 1 file (checked 37 source files)
    
    ❯ mypy --version
    mypy 1.1.1 (compiled: yes)
    
  17. hauntsaninja commented on Mar 27, 2023

    @hauntsaninja
    Collaborator

    Try version: Callable[[str], str] before your if, or to cast in the same expression that you call e.g. cast(Callable[[str], str], version)("my-lib")

  18. added a commit that references this issue on May 2, 2023
  19. added a commit that references this issue on May 2, 2023
    61f6358
  20. hauntsaninja commented on May 21, 2023

    @hauntsaninja
    Collaborator

    Thanks to ilevkivskyi, in the next release of mypy # type: ignore[unused-ignore] should work.

  21. added a commit that references this issue on Nov 15, 2023
  22. added a commit that references this issue on Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions