Skip to content

<nothing> when narrowing types on union of subtypes #16413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danielknell opened this issue Nov 6, 2023 · 3 comments
Closed

<nothing> when narrowing types on union of subtypes #16413

danielknell opened this issue Nov 6, 2023 · 3 comments
Labels
bug mypy got something wrong

Comments

@danielknell
Copy link

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
@danielknell danielknell added the bug mypy got something wrong label Nov 6, 2023
@danielknell 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
@bzoracler
Copy link
Contributor

This error no longer appears in the latest version of mypy

@hauntsaninja
Copy link
Collaborator

Fixed by #18972

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jun 10, 2025

Added a regression test for this specific issue in #19266

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants