python / python/mypy

Generic type alias with `Annotated` of a union: Type application is only supported for generic classes

Open
#19,343 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-type-alias
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

from typing import Annotated, TypeVar

T = TypeVar('T')
A = Annotated[T | object, '']

A[int]  # error: Type application is only supported for generic classes  [misc]

https://mypy-play.net/?mypy=latest&python=3.12&gist=ed5366ef19a62c9620df139b05adcce9

I expect this to work the same as:

from typing import TypeVar

T = TypeVar('T')
A = list[T | object, '']

A[int]  # ok

Looks like Annotated annotation makes mypy to mess with finding type var definition.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked mypy-play reproducer using Python 3.12 and the latest mypy. Compare the Annotated union alias with the stated working generic alias case, then confirm that applying A[int] no longer reports the type-application error while preserving the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.