non-pep695-type-alias (UP040)
Derived from the pyupgrade linter.
Fix is always available.
What it does
Checks for use of TypeAlias
annotations and TypeAliasType
assignments
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 TypeVar
variables. As
such, rewriting a type alias using a PEP-695 type
statement may change
the variance of the alias's type parameters.
Unlike type aliases that use simple assignments, definitions created using
PEP 695 type
statements cannot be used as drop-in replacements at
runtime for the value on the right-hand side of the statement. This means
that while for some simple old-style type aliases you can use them as the
second argument to an isinstance()
call (for example), doing the same
with a PEP 695 type
statement will always raise TypeError
at
runtime.
Example
Use instead: