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

assert_never() throws error on Python 3.10 #12613

Closed
johnthagen opened this issue Apr 18, 2022 · 2 comments
Closed

assert_never() throws error on Python 3.10 #12613

johnthagen opened this issue Apr 18, 2022 · 2 comments
Labels
bug mypy got something wrong

Comments

@johnthagen
Copy link
Contributor

Bug Report

assert_never() does not seem to work correctly on Python 3.10 with the latest mypy and typing-extensions.

To Reproduce

from typing_extensions import assert_never


def int_or_str(arg: int | str) -> None:
    match arg:
        case int():
            print("It's an int")
        case str():
            print("It's a str")
        case _ as unreachable:
            assert_never(unreachable)

But get an unexpected error:

$ mypy main.py
main.py:1: error: Module "typing_extensions" has no attribute "assert_never"
Found 1 error in 1 file (checked 1 source file)

The definition I see within typing-extensions has a conditional, but is none-the-less there:

if hasattr(typing, "assert_never"):
    assert_never = typing.assert_never
else:
    def assert_never(__arg: Never) -> Never:
        ...

Expected Behavior

No error and for type checking to be successful.

Your Environment

  • Python 3.10.3
  • Windows 10
mypy              0.942
mypy-extensions   0.4.3
typing_extensions 4.2.0
[tool.mypy]
ignore_missing_imports = true
strict = true
@JelleZijlstra
Copy link
Member

Mypy doesn't look at the runtime code in typing-extensions, but at the stubs in https://github.com/python/typeshed/blob/master/stdlib/typing_extensions.pyi. The stubs update adding assert_never() must not have made it into the last mypy release yet. It will be in the next one.

For assert_never specifically, you can just define it in your own code directly with the same signature as in typing-extensions, since there's no magic around it.

@johnthagen
Copy link
Contributor Author

I can confirm this is fixed in Mypy 0.950.

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

No branches or pull requests

2 participants