Skip to content

Errors when using union type aliases with isinstance #14242

@jrab89

Description

@jrab89

Bug Report

I'm getting errors when using an alias of a union type as the second argument to isinstance

To Reproduce

IntOrStr = int | str

foo = 0
bar = ""

if isinstance(foo, IntOrStr):
    print("foo is an IntOrStr")

if isinstance(bar, int | str):
    print("bar is an IntOrStr")

Expected Behavior

mypy shouldn't output any errors

Actual Behavior

mypy_isinstance_with_union_alias_issue.py:6: error: Parameterized generics cannot be used with class or instance checks  [misc]
mypy_isinstance_with_union_alias_issue.py:6: error: Argument 2 to "isinstance" has incompatible type "object"; expected "Union[type, UnionType, Tuple[Union[type, UnionType, Tuple[Any, ...]], ...]]"  [arg-type]

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11.0

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-604PEP 604 (union | operator)topic-type-aliasTypeAlias and other type alias issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions