Skip to content

Update generics reference to use PEP 695 type parameter syntax#2242

Open
BHUVANSH855 wants to merge 5 commits intopython:mainfrom
BHUVANSH855:update-generics-pep695
Open

Update generics reference to use PEP 695 type parameter syntax#2242
BHUVANSH855 wants to merge 5 commits intopython:mainfrom
BHUVANSH855:update-generics-pep695

Conversation

@BHUVANSH855
Copy link
Copy Markdown

This PR updates the reference/generics.rst documentation to reflect the modern type parameter syntax introduced in PEP 695.

Changes include:

  • Updating initial examples (e.g., Stack, Box) to use class Foo[T] syntax
  • Adding a note explaining the transition from TypeVar/Generic to PEP 695
  • Retaining mention of older syntax for backward compatibility

This improves consistency with the spec documentation and reduces confusion for users of Python 3.12+.

Fixes #2227

# This is a user-defined generic class
class Receiver(Generic[T]):
def accept(self, value: T) -> None: ...
# This is a user-defined generic class
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still uses the old syntax. Please make sure all examples (other than the one mentioning the old syntax explicitly) use the new syntax.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @JelleZijlstra, I’ve updated the remaining example to use PEP 695 syntax so that all examples are now consistent, with the old syntax kept only in the explicitly marked compatibility section.

Comment on lines +11 to +14
Python 3.12 introduced :pep:`695`, which allows defining generics using
type parameter syntax (e.g., ``class Foo[T]:`` and ``def func[T](x: T) -> T:``).
The older ``TypeVar`` and ``Generic``-based syntax remains supported
for compatibility with earlier Python versions.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't mention the PEP. This is mostly historic information and not really relevant to users. Also, the wording suggests that the user is already familiar with both syntaxes. I would rather move this note to the bottom of the section (before the old syntax example) and reword it to says something along the line that the current syntax was only introduced with Python 3.12, and here's the legacy syntax for older Python versions.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @srittau, I’ve removed the PEP reference, moved the note next to the legacy syntax example, and reworded it to clarify that the current syntax was introduced in Python 3.12 and the older syntax is for earlier versions.

@BHUVANSH855 BHUVANSH855 requested a review from srittau April 8, 2026 11:00
@srittau
Copy link
Copy Markdown
Collaborator

srittau commented Apr 8, 2026

Thanks so far, but I also think the whole "Defining subclasses of generic classes" needs to be rewritten, as with Python 3.12 syntax, Generic isn't used anymore.

@BHUVANSH855
Copy link
Copy Markdown
Author

Thanks so far, but I also think the whole "Defining subclasses of generic classes" needs to be rewritten, as with Python 3.12 syntax, Generic isn't used anymore.

@srittau I made a few final refinements:

  • Updated wording of the note for clarity
  • Fully aligned subclassing examples with PEP 695 syntax
  • Removed outdated discussion of Generic

Everything should now consistently reflect the Python 3.12+ style.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update reference/generics.html with PEP695 type parameter syntax

3 participants