Update generics reference to use PEP 695 type parameter syntax#2242
Update generics reference to use PEP 695 type parameter syntax#2242BHUVANSH855 wants to merge 5 commits intopython:mainfrom
Conversation
| # This is a user-defined generic class | ||
| class Receiver(Generic[T]): | ||
| def accept(self, value: T) -> None: ... | ||
| # This is a user-defined generic class |
There was a problem hiding this comment.
This still uses the old syntax. Please make sure all examples (other than the one mentioning the old syntax explicitly) use the new syntax.
There was a problem hiding this comment.
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.
docs/reference/generics.rst
Outdated
| 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Thanks so far, but I also think the whole "Defining subclasses of generic classes" needs to be rewritten, as with Python 3.12 syntax, |
@srittau I made a few final refinements:
Everything should now consistently reflect the Python 3.12+ style. |
This PR updates the
reference/generics.rstdocumentation to reflect the modern type parameter syntax introduced in PEP 695.Changes include:
Stack,Box) to useclass Foo[T]syntaxTypeVar/Genericto PEP 695This improves consistency with the spec documentation and reduces confusion for users of Python 3.12+.
Fixes #2227