Skip to content

Union of unions doesn't work using Python 3.10 Type Union Operator #12005

Closed
@Garrett-R

Description

@Garrett-R

Bug Report

It seems you can't take a union of unions using the new Type Union Operator.

To Reproduce

  1. Run Mypy on this file:
from typing import TypeAlias, Union


class Dog:
    pass

class Cat:
    pass

class Salmon:
    pass

class Trout:
    pass


Mammal: TypeAlias = Dog | Cat

Fish: TypeAlias = Salmon | Trout

Animal: TypeAlias = Mammal | Fish  # type error!!

Animal2: TypeAlias = Union[Mammal, Fish]  # no type error, as expected

Expected Behavior

In PEP-0604, it says "The existing typing.Union and | syntax should be equivalent.". Therefore, I assumed the last 2 lines above would both behave similarly.

Actual Behavior

Mypy throws type error:

error: Unsupported left operand type for | ("object")

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: <no flags>
  • Mypy configuration options from mypy.ini (and other config files): <no config>
  • Python version used: 3.10.2
  • Operating system and version: Ubuntu 20.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-604PEP 604 (union | operator)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions