Skip to content

PEP 604 union type alias not supported inside isinstance checks #16358

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
michaeloliverx opened this issue Oct 28, 2023 · 1 comment
Closed
Labels
bug mypy got something wrong topic-pep-604 PEP 604 (union | operator)

Comments

@michaeloliverx
Copy link

Bug Report

PEP 604 introduced union type syntax X | Y which included support for the new syntax within calls to isinstance and issubclass.

mypy complains about code which runs correctly at runtime.

To Reproduce

class ANDNode:
    pass


class ORNode:
    pass


class NOTNode:
    pass


NodeType = ANDNode | ORNode | NOTNode

# mypy error
isinstance(NOTNode(), NodeType)

# okay
isinstance(NOTNode(), ANDNode | ORNode | NOTNode)

Gist URL: https://gist.github.com/mypy-play/4be2e08fa1da185af6e821705ea85744
Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=4be2e08fa1da185af6e821705ea85744

Expected Behavior

I would expect mypy to support a union type alias within an isinstance call to avoid duplication.

Actual Behavior

pep-604.py:16: error: Parameterized generics cannot be used with class or instance checks  [misc]
pep-604.py:16: error: Argument 2 to "isinstance" has incompatible type "<typing special form>"; expected "_ClassInfo"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.6.1 (compiled: yes)
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11.0
@michaeloliverx michaeloliverx added the bug mypy got something wrong label Oct 28, 2023
@AlexWaygood AlexWaygood added the topic-pep-604 PEP 604 (union | operator) label Oct 28, 2023
@ngnpope
Copy link
Contributor

ngnpope commented Mar 6, 2024

This looks like a duplicate of #11673.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-604 PEP 604 (union | operator)
Projects
None yet
Development

No branches or pull requests

4 participants