Thanks to visit codestin.com
Credit goes to github.com

Skip to content

isinstance can take typing.Union as second arg in 3.10 and newer #12137

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
hauntsaninja opened this issue Jun 13, 2024 · 3 comments
Closed

isinstance can take typing.Union as second arg in 3.10 and newer #12137

hauntsaninja opened this issue Jun 13, 2024 · 3 comments

Comments

@hauntsaninja
Copy link
Collaborator

Currently it allows for types.UnionType but not typing.Union

if sys.version_info >= (3, 10):
    _ClassInfo: TypeAlias = type | types.UnionType | tuple[_ClassInfo, ...]
else:
    _ClassInfo: TypeAlias = type | tuple[_ClassInfo, ...]

def isinstance(obj: object, class_or_tuple: _ClassInfo, /) -> bool: ...

I think mypy currently assumes that whatever typeshed says for isinstance has no false positives

@JelleZijlstra
Copy link
Member

I'm not sure we could express this in typeshed. Union[int, str] is not an instance of typing.Union.

@AlexWaygood
Copy link
Member

(I don't object to revisiting this, but this is a duplicate of #7505)

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Jun 13, 2024

Thanks, looks like most of the relevant discussion is on #7508. Sounds like most people are surprised that isinstance works at all with old Union / think that type checkers should special case this even more than they currently do

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

No branches or pull requests

3 participants