Incompatible type "Type[ImportError]"; expected "Type[Exception]" #14443
Copy link
Copy link
Closed
Labels
bugmypy got something wrongmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotationsWhen to infer types or require explicit annotations
Description
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 hope 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotationsWhen to infer types or require explicit annotations
Activity
JelleZijlstra commentedon Jan 13, 2023
That's pretty weird! I am not sure it's specific to ImportError; I also get an error for
y = [AttributeError, ImportError].Make join of callables a bit more principled (python#22296)