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
A type expression passed as a call argument is only recognized as a
TypeFormwhen the parameter type is exactlyTypeForm[...]. With a union such asTypeForm[T] | None, the argument is evaluated as a value expression instead.The same happens with
int | str,Literal[...],Optional[...]or a string annotation. Assigning the same expression to a variable annotatedTypeForm[int] | Noneworks, so this is specific to call arguments.Playground: https://mypy-play.net/?mypy=latest&python=3.14&gist=9ccf3597617fe30ec3d718ee9cdb1d9d.
Your Environment