Skip to content

TypeAlias does not work with Union types #11665

Closed
@realharry

Description

@realharry

Bug Report

Union types are valid types, just like any other types. That's at least my expectation. However, you cannot currently type alias union types.

For example,

X: TypeAlias = int | str  #   line 3

def f(b: bool) -> X:     # line 6
    if b:
        return 3
    else:
        return "three"

Mypy will issue the following errors:

typealias_test.py:3: error: Variable "typing.TypeAlias" is not valid as a type
typealias_test.py:6: error: Variable "typealias_test.X" is not valid as a type

To Reproduce

  1. Create a type alias for a union type, using either typing.Union or the 3.10 vert bar syntax.
  2. You can explicitly declare the type alias implicitly or explicitly with typing.TypeAlias.
  3. Run mypy check. Union type aliases are not recognized as type by mypy.

Expected Behavior

Union types should be treated as just any other types.

Actual Behavior

Union types cannot be type-aliased in the current impl of mypy.

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.0
  • Operating system and version: Ubuntu 20.04.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions