Skip to content

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

Closed as not planned

Description

@hauntsaninja

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

Activity

  1. JelleZijlstra commented on Jun 13, 2024

    @JelleZijlstra
    Member

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

  2. AlexWaygood commented on Jun 13, 2024

    @AlexWaygood
    Member

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

  3. hauntsaninja commented on Jun 13, 2024

    @hauntsaninja
    CollaboratorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions