Mypy error: PEP 695 type aliases are not yet supported

Question:

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+?

Asked By: Adrian

||

Answers:

PEP 695 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 any existing version of mypy won’t.

Note that even after it will be implemented in the latest mypy, it likely won’t 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).

Update (May 2024): a lot of support has just been added to the main branch and will likely be released in mypy 1.11. It’s still not complete, but much closer to it (the code in the question all works). It might require --enable-incomplete-feature=NewGenericSyntax (but that flag doesn’t exist yet in 1.10.0 and I didn’t need it with 1.11.0+dev).

Answered By: Marcin Wrochna