Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PEP 695 #5062

Closed
charliermarsh opened this issue Jun 13, 2023 · 5 comments
Closed

Add support for PEP 695 #5062

charliermarsh opened this issue Jun 13, 2023 · 5 comments
Assignees
Labels
core Related to core functionality python312 Related to Python 3.12

Comments

@charliermarsh
Copy link
Member

Summary

PEP 695 adds new syntax for type parameters and generics. A few examples:

class ClassA[T: str]:
    def method1(self) -> T:
        ...

type ListOrSet[T] = list[T] | set[T]

There are a few pieces of work involved in this:

  • Adding these elements to the AST.
  • Adding these elements to the parser (especially the type soft keyword).
  • Adding these elements to the visitor, and to any rules that do custom visiting.
  • Adding these elements to our semantic model (need to look into how scoping and resolution works).

This will be Ruff's first time undergoing a significant grammar and syntax change (apart from adding structural pattern matching), so it's a great opportunity to set the project's standards for keeping up with Python's evolution.

Specifically, the final Python 3.12 release is expected on 2023-10-02. We should aim to support PEP 695 by that date at the latest.

@charliermarsh charliermarsh added the core Related to core functionality label Jun 13, 2023
@charliermarsh charliermarsh self-assigned this Jun 13, 2023
@charliermarsh
Copy link
Member Author

I will take ownership of this work.

@charliermarsh
Copy link
Member Author

\cc @zanieb who's working on this :)

@zanieb
Copy link
Member

zanieb commented Jul 14, 2023

Progress on the Rust Python parser can be tracked at RustPython/Parser#82

zanieb added a commit that referenced this issue Jul 18, 2023
zanieb added a commit that referenced this issue Jul 18, 2023
zanieb added a commit that referenced this issue Jul 18, 2023
evanrittenhouse pushed a commit to evanrittenhouse/ruff that referenced this issue Jul 19, 2023
evanrittenhouse pushed a commit to evanrittenhouse/ruff that referenced this issue Jul 19, 2023
evanrittenhouse pushed a commit to evanrittenhouse/ruff that referenced this issue Jul 19, 2023
evanrittenhouse pushed a commit to evanrittenhouse/ruff that referenced this issue Jul 19, 2023
konstin pushed a commit that referenced this issue Jul 19, 2023
konstin pushed a commit that referenced this issue Jul 19, 2023
zanieb added a commit that referenced this issue Jul 25, 2023
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

Part of #5062 
Requires astral-sh/RustPython-Parser#32

Adds visitation of type alias statements and type parameters in class
and function definitions.

Duplicates tests for `PreorderVisitor` into `Visitor` with new
snapshots. Testing required node implementations for the `TypeParam`
enum, which is a chunk of the diff and the reason we need `Ranged`
implementations in
astral-sh/RustPython-Parser#32.

## Test Plan

<!-- How was it tested? -->

Adds unit tests with snapshots.
zanieb added a commit that referenced this issue Jul 28, 2023
Requires astral-sh/RustPython-Parser#42
Related PyCQA/pyflakes#778
[PEP-695](https://peps.python.org/pep-0695)
Part of #5062 

<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->
Adds a scope for type parameters, a type parameter binding kind, and
checker visitation of type parameters in type alias statements, function
definitions, and class definitions.

A few changes were necessary to ensure correctness following the
insertion of a new scope between function and class scopes and their
parent.

## Test Plan

<!-- How was it tested? -->
Undefined name snapshots.

Unused type parameter rule will be added as follow-up.
@zanieb
Copy link
Member

zanieb commented Jul 31, 2023

We now support parsing and analysis (#6109) of type aliases and parameters.

Formatting support is being added in #6161 and #6162.

zanieb added a commit that referenced this issue Aug 2, 2023
…6161)

Part of #5062 
Closes #5931

Implements formatting of a sequence of type parameters in a dedicated
struct for reuse by classes, functions, and type aliases (preparing for
#5929). Adds formatting of type parameters in class and function
definitions — previously, they were just elided.
zanieb added a commit that referenced this issue Aug 2, 2023
@zanieb zanieb closed this as completed Aug 2, 2023
zanieb added a commit that referenced this issue Aug 3, 2023
Adds rule to convert type aliases defined with annotations i.e. `x:
TypeAlias = int` to the new PEP-695 syntax e.g. `type x = int`.

Does not support using new generic syntax for type variables, will be
addressed in a follow-up.
Added as part of pyupgrade — ~the code 100 as chosen to avoid collision
with real pyupgrade codes~.

Part of #4617 
Builds on #5062
@zanieb zanieb added the python312 Related to Python 3.12 label Aug 24, 2023
@zanieb
Copy link
Member

zanieb commented Aug 28, 2023

Additional resource; may be useful for refinements to scoping https://jellezijlstra.github.io/pep695

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to core functionality python312 Related to Python 3.12
Projects
None yet
Development

No branches or pull requests

2 participants