Skip to content

Support for PEP 695 #15238

Closed
Closed
@erictraut

Description

@erictraut

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 infer_variance keyword parameter in TypeVar; see this section of PEP for details
    Add support for variance inference (similar to variance enforcement in Protocol classes today); see this section of PEP for details
    Add support for new AST nodes for type statement; see this section of PEP for details
    Add support for new AST nodes for generic classes and functions; see this section of PEP for details
    Add support for evaluating type parameters using the new syntax (including TypeVar bounds and constraints, TypeVarTuple and ParamSpec); see this section of PEP for details
    Add support for defining type aliases using the new type statement (including generic and recursive type definitions); see this section of PEP for details
    Add support for defining generic classes and functions with explicit type parameter lists using new syntax
    Add support for functional form of TypeAliasType, supported for backward compatibility; see this section of PEP for details
    Add detection and error reporting for use of traditional TypeVar, TypeVarTuple, or ParamSpec in conjunction with new syntax; see this section of PEP for details
    Add 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

Activity

AlexWaygood

AlexWaygood commented on May 14, 2023

@AlexWaygood
Member

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.

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.

abrahammurciano

abrahammurciano commented on Oct 31, 2023

@abrahammurciano

Python3.12 has been out for a while now, is there a timeline for when the new generics will be supported?

JelleZijlstra

JelleZijlstra commented on Oct 31, 2023

@JelleZijlstra
Member

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

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jstasiak@JelleZijlstra@JukkaL@dbzix@erictraut

        Issue actions

          Support for PEP 695 · Issue #15238 · python/mypy