I encountered an error while trying to use type aliases in Python with MyPy. Here's a simplified version of my code:
type IntList = list[int] # This line causes the error
type OtherType = int # This line causes the error
class test:
pass
type SomeType = test # This line causes the error
I'm using Python 3.12, which supposedly supports PEP 695 type aliases. Am I doing something wrong, missing an import, or is Mypy's support for PEP 695 incomplete?
Edit: Looks like I get no errors from MyPy if I am doing it this way.
IntList: TypeAlias = list[int]
OtherType: TypeAlias = int
class test:
pass
SomeType: TypeAlias = test
Edit: How do you alias a type in Python?
Is this because of the option in Python 3.9+?
Update (October 2024): mypy 1.12 has just been released with full support for PEP 695, enabled by default (no experimental flag is needed anymore).
Update (July 2024): mypy 1.11 has just been released, with support for almost all of PEP 695 (all the code in the question checks, in particular). This is a large release, so (as the new warning will tell you) the support only works with
--enable-incomplete-feature=NewGenericSyntax
on the command line or enable_incomplete_feature = "NewGenericSyntax"
in a configuration file (e.g. under [tool.mypy]
in pyproject.toml).
The few not-yet-supported parts of PEP 695 are tracked and detailed in this issue. Currently it's only:
:=
, yield
, await
,infer_variance
argument to TypeVar
,Original answer: PEP 695 (the new generics syntax) is not implemented in mypy≤1.10 yet (as the error message tries to say), the issue tracking that is here: https://github.com/python/mypy/issues/15238
So you can use type x = y
in Python 3.12 and the interpreter will work, but checking types with mypy≤1.10 won't.
Note that even after it's implemented in the latest mypy, it likely won't ever be supported by mypy running on Python ≤3.11. So this alias syntax won't be used in any place that commits to support 3.11.
Backwards-compatible code needs to use older style or TypeAliasType
(supported in mypy since 1.10).