-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Bug Report
The type narrowing seems to fail in some situations, from what i can tell its caused when:
The type is narrowed to a union of subclasses, then narrowed further by one of the branches of that subclass using isinstance.
in the example, i define Base and to mixins (FooMixin and BarMixin), then a function that takes Base and checks for the presence of the mixins.
I use isinstance(item, (FooMixin, BarMixin) as a guard which narrows the type of item to Union[__main__.<subclass of "Base" and "FooMixin">, __main__.<subclass of "Base" and "BarMixin">] as expected.
then if i use isinstance(item, FooMixin) instead of getting FooMixin or __main__.<subclass of "Base" and "FooMixin"> for the type of item i get <nothing> which has been hard to google for but i'm assuming is an internal type in mypy for "i have no idea what this is"?
the actual code this happens in is complex, but this seems to be the simplest case I've found to recreate it.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=f64cd0c65509ac3d6b6498fed4ced174
Expected Behavior
i would had expected the type to be narrowed to either <subclass of "Base" and "FooMixin"> or FooMixin and for there to be no error.
Actual Behavior
main.py:22: note: Revealed type is "Union[__main__.<subclass of "Base" and "FooMixin">, __main__.<subclass of "Base" and "BarMixin">]"
main.py:25: note: Revealed type is "<nothing>"
main.py:26: error: <nothing> has no attribute "foo" [attr-defined]
main.py:28: note: Revealed type is "__main__.<subclass of "Base" and "BarMixin">"
Your Environment
- Mypy version used: 1.6.1
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): defaults - Python version used: 3.11.2