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
{{ message }}
Repository navigation
<nothing> when narrowing types on union of subtypes #16413
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
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[/+]on Nov 6, 2023
Activity
[-]error: <nothing> has no attribute "foo" when narrowing types on union of subtypes with mixins[/-][+] <nothing> when narrowing types on union of subtypes[/+]bzoracler commentedon Jun 10, 2025
This error no longer appears in the latest version of mypy
hauntsaninja commentedon Jun 10, 2025
Fixed by #18972
hauntsaninja commentedon Jun 10, 2025
Added a regression test for this specific issue in #19266
Add regression test for narrowing union of mixins (#19266)