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

Incompatible type "Type[ImportError]"; expected "Type[Exception]" #14443

Open
PeterJCLaw-mns opened this issue Jan 13, 2023 · 1 comment
Open
Labels
bug mypy got something wrong topic-inference When to infer types or require explicit annotations

Comments

@PeterJCLaw-mns
Copy link

Bug Report

Mypy appears not to consider ImportError to be an exception type in some cases.

To Reproduce

y = [
    # ValueError,
    ImportError,  # error: List item 0 has incompatible type "Type[ImportError]"; expected "Type[Exception]"  [list-item]
    AttributeError,
]
reveal_type(y)  # note: Revealed type is "builtins.list[def (*args: builtins.object, *, name: Union[builtins.str, None] =, obj: Union[builtins.str, None] =) -> builtins.Exception]"

The ordering of the list elements doesn't seem to impact the behaviour.

If you un-comment the ValueError however:

y = [
    ValueError,
    ImportError,
    AttributeError,
]
reveal_type(y)  # note: Revealed type is "builtins.list[builtins.type]"

While this is perhaps less precise than I'd hope (I'd hop to get list[type[Exception]]) it feels more right to me.

Adding an annotation to the variable (y: list[type[Exception]]) does help mypy see that this is ok, however in my original use-case the list being constructed was passed to a decorator function, so there was no easy opportunity to annotate it.

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11
@PeterJCLaw-mns PeterJCLaw-mns added the bug mypy got something wrong label Jan 13, 2023
@AlexWaygood AlexWaygood added the topic-inference When to infer types or require explicit annotations label Jan 13, 2023
@JelleZijlstra
Copy link
Member

That's pretty weird! I am not sure it's specific to ImportError; I also get an error for y = [AttributeError, ImportError].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-inference When to infer types or require explicit annotations
Projects
None yet
Development

No branches or pull requests

3 participants