non-pep695-type-alias (UP040)#
Derived from the pyupgrade linter.
Fix is always available.
What it does#
Checks for use of TypeAlias
annotation for declaring type aliases.
Why is this bad?#
The type
keyword was introduced in Python 3.12 by PEP 695 for defining
type aliases. The type
keyword is easier to read and provides cleaner
support for generics.
Known problems#
PEP 695 uses inferred variance for type parameters, instead of the
covariant
and contravariant
keywords used by TypeParam
variables. As
such, rewriting a TypeParam
variable to a type
alias may change its
variance.
Unlike TypeParam
variables, PEP 695-style type
aliases cannot be used
at runtime. For example, calling isinstance
on a type
alias will throw
a TypeError
. As such, rewriting a TypeParam
via the type
keyword will
cause issues for parameters that are used for such runtime checks.
Example#
Use instead: