Skip to content

Somewhat better support for isinstance on old-style unions #19714

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

Merged
merged 1 commit into from
Aug 23, 2025

Conversation

hauntsaninja
Copy link
Collaborator

Partially fixes #17680 , remainder of the issue should probably be fixed in the isinstance stub

Partially fixes python#17680 , remainder of the issue should probably be fixed
in the isinstance stub
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

SimpleAlias = Union[int, str]

def foo(x: Union[int, str, tuple]):
# TODO: fix the typeshed stub for isinstance
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fixable in typeshed? I don't think the correct type is expressible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the correct type isn't expressible, but if we change typeshed to include _SpecialForm here https://github.com/python/typeshed/blob/b6e21d05aec7ff1444ffc109c7d31d2f5bb28dbd/stdlib/builtins.pyi#L1537 or attempt to type more of the union runtime impl or just give up and make it use object, I think that will make the remaining error here go away

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was tried in python/typeshed#7508 and the consensus seemed to be that it wasn't worth it?

I wouldn't mind the stubs reflecting the runtime a bit more closely w.r.t. the various special forms though; it would mean less special casing in ty too, which would be nice

@hauntsaninja hauntsaninja merged commit ac4cacb into python:master Aug 23, 2025
20 checks passed
@hauntsaninja hauntsaninja deleted the mypybehav branch August 23, 2025 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support typing.Union/typing.Optional in isinstance check (/support old-style unions)
3 participants