Skip to content

The parameter specification (ParamSpec) of a generic type alias can be covariant whether a new or old syntax against the doc "always invariant" #20499

@hyperkai

Description

@hyperkai

*Memo:

  • mypy test.py
  • mypy 1.19.1
  • Python 3.14.0
  • Windows 11

The doc says a parameter specification (ParamSpec) is always invariant as shown below:

  1. If the type parameter is variadic (TypeVarTuple) or a parameter specification (ParamSpec), it is always considered invariant. No further inference is needed.

But the parameter specification (ParamSpec) of a generic type alias can be covariant whether a new or old syntax against the doc "always invariant" as shown below:

from collections.abc import Callable
# from typing import ParamSpec, TypeAlias

''' New syntax '''
type TA[**P] = Callable[P, None] # Covariant
''' New syntax '''

''' Old syntax '''
# P = ParamSpec('P')

# TA: TypeAlias = Callable[P, None] # Covariant
''' Old syntax '''

v: TA[float, float] = lambda x, y: None

# Covariant
a: TA[object, object] = v   # Error
b: TA[complex, complex] = v # Error
c: TA[float, float] = v     # No error
d: TA[int, int] = v         # No error
e: TA[bool, bool] = v       # No error

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