Incompatible type "Type[ImportError]"; expected "Type[Exception]"
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
That's pretty weird! I am not sure it's specific to ImportError; I also get an error for y = [AttributeError, ImportError].