python / python/mypy

Type expression arguments aren't recognized as `TypeForm` when the parameter type is a union containing a `TypeForm`

Open
#21,964 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

A type expression passed as a call argument is only recognized as a TypeForm when the parameter type is exactly TypeForm[...]. With a union such as TypeForm[T] | None, the argument is evaluated as a value expression instead.

from typing import Annotated, TypeVar
from typing_extensions import TypeForm

T = TypeVar('T')

def f(t: TypeForm[T]) -> T: ...
def g(t: TypeForm[T] | None) -> T: ...

reveal_type(f(Annotated[int, 'meta']))  # int
reveal_type(g(Annotated[int, 'meta']))  # Argument 1 to "g" has incompatible type "<typing special form>"; expected "TypeForm[Never] | None"  [arg-type]

The same happens with int | str, Literal[...], Optional[...] or a string annotation. Assigning the same expression to a variable annotated TypeForm[int] | None works, so this is specific to call arguments.

Playground: https://mypy-play.net/?mypy=latest&python=3.14&gist=9ccf3597617fe30ec3d718ee9cdb1d9d.

Your Environment

  • Mypy version used: 2.3.1
  • Python version used: 3.14

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 minimal reproduction in the issue and compare direct TypeForm parameters with TypeForm inside unions, including Optional and string annotations. Trace mypy's call-argument analysis, then add a regression test confirming that these arguments are recognized and reveal the expected type without an incompatibility diagnostic.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.