You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
The text was updated successfully, but these errors were encountered:
danielknell
changed the title
error: <nothing> has no attribute "foo" when narrowing types on union of subtypes with mixins
<nothing> when narrowing types on union of subtypes
Nov 6, 2023
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
andBarMixin
), then a function that takesBase
and checks for the presence of the mixins.I use
isinstance(item, (FooMixin, BarMixin)
as a guard which narrows the type ofitem
toUnion[__main__.<subclass of "Base" and "FooMixin">, __main__.<subclass of "Base" and "BarMixin">]
as expected.then if i use
isinstance(item, FooMixin)
instead of gettingFooMixin
or__main__.<subclass of "Base" and "FooMixin">
for the type ofitem
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">
orFooMixin
and for there to be no error.Actual Behavior
Your Environment
mypy.ini
(and other config files): defaultsThe text was updated successfully, but these errors were encountered: