Closed
Description
PEP 695 was accepted, and it looks like it will make it into Python 3.12. This PEP adds support for a new type parameter syntax for generic classes and methods. It also adds a new syntax for type aliases (both generic and not).
PEP 695 functionality is implemented in pyright today. It would be great to see it also implemented in mypy and the other major type checkers so we could start to use the new features in type stubs.
Here is a rough task list associated with this work:
- Add support for variance inference (similar to variance enforcement in Protocol classes today); see this section of PEP for detailsAdd support for new AST nodes for
type
statement; see this section of PEP for detailsAdd support for new AST nodes for generic classes and functions; see this section of PEP for detailsAdd support for evaluating type parameters using the new syntax (including TypeVar bounds and constraints, TypeVarTuple and ParamSpec); see this section of PEP for detailsAdd support for defining type aliases using the newtype
statement (including generic and recursive type definitions); see this section of PEP for detailsAdd support for defining generic classes and functions with explicit type parameter lists using new syntaxAdd support for functional form ofTypeAliasType
, supported for backward compatibility; see this section of PEP for detailsAdd detection and error reporting for use of traditional TypeVar, TypeVarTuple, or ParamSpec in conjunction with new syntax; see this section of PEP for detailsAdd detection and error reporting for use of assignment expressions, yield, yield from, and await expressions within the type parameter scope; see this section of PEP for details
Metadata
Metadata
Assignees
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
AlexWaygood commentedon May 14, 2023
Just to note — even if mypy implemented support for PEP 695 tomorrow, it will probably be quite a while before typeshed is able to use PEP 695 syntax, and I imagine the same will be true for many stub authors.
Mypy uses the Python
ast
module to parse stubs, meaning PEP 695 syntax will fail to be parsed by mypy when mypy is run on Python <=3.11. As such, we probably won't be able to adopt PEP 695 syntax in typeshed until we drop support for Python 3.11, unfortunately.Make PEP 695 constructs give a reasonable error message
Make PEP 695 constructs give a reasonable error message
Make PEP 695 constructs give a reasonable error message (#16013)
Make PEP 695 constructs give a reasonable error message (#16013)
abrahammurciano commentedon Oct 31, 2023
Python3.12 has been out for a while now, is there a timeline for when the new generics will be supported?
JelleZijlstra commentedon Oct 31, 2023
When you (or anyone else reading this) submit a PR implementing it. Mypy is a volunteer-run project and has no set timelines.
I did a little preliminary work on this myself, but not sure I'll be able to complete it any time soon.
73 remaining items