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

mypy 1.1.1 no longer respects # type: ignore[attr-defined] when importing from typing #14871

Closed
leycec opened this issue Mar 11, 2023 · 2 comments · Fixed by #14881
Closed

mypy 1.1.1 no longer respects # type: ignore[attr-defined] when importing from typing #14871

leycec opened this issue Mar 11, 2023 · 2 comments · Fixed by #14881
Labels
bug mypy got something wrong

Comments

@leycec
Copy link

leycec commented Mar 11, 2023

Springtime salutations, wonderful mypy devs! The Sun shines bright, the Canadian nights are still as cold as an icepick, and the bugs infest every corner of my codebase.

Bug Report

mypy ≤ 1.0.0 correctly respected # type: ignore[attr-defined] comments when importing typing attributes not available in the current version of Python.

mypy ≥ 1.1.1 no longer does so, instead emitting an erroneous warning referring to an irrelevant error code (i.e., "misc") as well as one warning for each such attribute. Examples follow or my middle name isn't "Beardy Bald Guy".

To Reproduce

from typing import (  # type: ignore[attr-defined]
    LiteralString as LiteralString,  # pyright: ignore[reportGeneralTypeIssues]
    Never as Never,  # pyright: ignore[reportGeneralTypeIssues]
    NotRequired as NotRequired,  # pyright: ignore[reportGeneralTypeIssues]
    Required as Required,  # pyright: ignore[reportGeneralTypeIssues]
    Self as Self,  # pyright: ignore[reportGeneralTypeIssues]
    TypeVarTuple as TypeVarTuple,  # pyright: ignore[reportGeneralTypeIssues]
    Unpack as Unpack,  # pyright: ignore[reportGeneralTypeIssues]
    assert_never as assert_never,  # pyright: ignore[reportGeneralTypeIssues]
    assert_type as assert_type,  # pyright: ignore[reportGeneralTypeIssues]
    clear_overloads as clear_overloads,  # pyright: ignore[reportGeneralTypeIssues]
    dataclass_transform as dataclass_transform,  # pyright: ignore[reportGeneralTypeIssues]
    reveal_type as reveal_type,  # pyright: ignore[reportGeneralTypeIssues]
    get_overloads as get_overloads,  # pyright: ignore[reportGeneralTypeIssues]
    reveal_type as reveal_type,  # pyright: ignore[reportGeneralTypeIssues]
)

The above code is copied verbatim from the @beartype codebase, which I maintain for the good of QA-kind. The above code used to pass muster under both mypy and pyright. Now, only pyright remains happy:

$ pyright
Loading configuration file at /home/leycec/py/beartype/pyrightconfig.json
Assuming Python version 3.11
Assuming Python platform Linux
Searching for source files
Found 223 source files
pyright 1.1.298
0 errors, 0 warnings, 0 informations 
Completed in 10.842sec

$ python3.11 -m mypy
Success: no issues found in 223 source files

$ python3.10 -m mypy
beartype/typing/__init__.py:192: note: Use `from typing_extensions import LiteralString` instead
beartype/typing/__init__.py:192: note: Error code "misc" not covered by "type: ignore" comment
beartype/typing/__init__.py:192: note: See https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-new-additions-to-the-typing-module
beartype/typing/__init__.py:192: note: Use `from typing_extensions import Never` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import NotRequired` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import Required` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import Self` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import TypeVarTuple` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import Unpack` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import assert_never` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import assert_type` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import clear_overloads` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import dataclass_transform` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import reveal_type` instead
beartype/typing/__init__.py:192: note: Use `from typing_extensions import get_overloads` instead
Success: no issues found in 223 source files

All is no longer well here. @beartype's userbase will now be assaulted with spurious warnings they can do absolutely nothing about. Even I can't do anything about these warnings! And I'm the @beartype guy.

Moreover, these warnings are arguably harmful with respect to runtime typing. The typing_extensions module may be an unconditional improvement over the typing module with respect to static typing – but that definitely doesn't extend to runtime typing. Why? Because the runtime API of type hints defined by the typing_extensions module substantially differs in non-standard and non-trivial ways from that defined by the typing module. It's effectively infeasible for runtime type-checkers to support both. Ergo, runtime type-checkers typically only support type hints defined by the typing module.

@beartype breaks this trend a bit. We actively support a subset of type hints defined by the typing_extensions module (e.g., typing_extensions.Literal[...]), despite those type hints providing a non-standard runtime API – but that's only a subset and that's only because Microsoft themselves vociferously complained on our issue tracker about this. What Microsoft wants, one gives to Microsoft.

Expected Behavior

Silence! Sweet, blessed silence!

Actual Behavior

Loudness! Horrid, accursed loudness! Please see above.

Your Environment

  • Mypy version used: mypy 1.1.1
  • Mypy command-line flags: None.
  • Mypy configuration options from mypy.ini (and other config files): None.
  • Python version used: Python 3.11, 3.10, 3.9, 3.8, and 3.7. Literally everything that has yet to hit EOL.

Thanks so much for all the tremendous volunteerism over these many, long years. You are dedication incarnate. Much ❤️ from the snow-encrusted Canadian wilderness.

@leycec, the @beartype guy

@leycec leycec added the bug mypy got something wrong label Mar 11, 2023
@AlexWaygood
Copy link
Member

Cc. @hauntsaninja -- the change in error code from attr-defined to misc seems to bisect to 0929773

hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Mar 11, 2023
JelleZijlstra pushed a commit that referenced this issue Mar 11, 2023
Fixes #14871

Note that this doesn't affect the exit code; it's just about what
non-fatal notes get printed during successful runs
@leycec
Copy link
Author

leycec commented Mar 12, 2023

Thanks so much. I cannot believe you've already resolved this in less than a day. You're an inspiration to us all, mypy! My jaw has officially dropped. 😮

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants