Skip to content

PEP 604 unions don't work with generic type aliases #12211

Closed
@Eldar1205

Description

@Eldar1205

Bug Report

Python 3.10 new Union syntax doesn't work in a scenario old syntax did when involved with generic type aliases.

To Reproduce

Create the following definitions:

_ResponseType = TypeVar("_ResponseType")


SyncFunc: TypeAlias = Callable[..., _ResponseType]
Coro: TypeAlias = Callable[..., Coroutine[Any, Any, _ResponseType]]

SyncFuncOrCoro: TypeAlias = Coro[_ResponseType] | SyncFunc[_ResponseType]

Expected Behavior

Should work without errors, like it does when using old Union syntax:

_ResponseType = TypeVar("_ResponseType")


SyncFunc: TypeAlias = Callable[..., _ResponseType]
Coro: TypeAlias = Callable[..., Coroutine[Any, Any, _ResponseType]]

SyncFuncOrCoro: TypeAlias = Union[Coro[_ResponseType], SyncFunc[_ResponseType]]

Actual Behavior

I receive mypy error "Type application is only supported for generic classes".

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: N/A
  • Python version used: 3.10.2
  • Operating system and version: Windows 11
  • Mypy configuration options from mypy.ini (and other config files):

I'm using pretty strict mypy settings:

[mypy]
follow_imports = normal
ignore_errors = false
implicit_reexport = false
warn_redundant_casts = True
warn_unused_ignores = True
disallow_any_generics = True
disallow_untyped_defs = True
check_untyped_defs = True
allow_redefinition = false
local_partial_types = True
strict_optional = true
strict_equality = true
warn_unused_configs = true
warn_unreachable = true
warn_no_return = true
no_implicit_optional = true

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