PEP 835: Shorthand syntax for Annotated type metadata#4995
PEP 835: Shorthand syntax for Annotated type metadata#4995till-varoquaux wants to merge 2 commits into
Conversation
|
@ilevkivskyi The draft is up and ready for your official sponsorship sign-off whenever you have a moment! |
Documentation build overview
701 files changed ·
|
ilevkivskyi
left a comment
There was a problem hiding this comment.
The draft LG! But please take a look at comments by @JelleZijlstra
b646aa7 to
c237409
Compare
|
Thanks so much for the review and the help getting this staged, @JelleZijlstra and @ilevkivskyi ! Since my sponsor (@ilevkivskyi) has officially signed off on the draft, could one of the PEP editors go ahead and assign this an official PEP number? Let me know if there is anything else needed from my end before we can get this merged and move the conversation over to Discourse. |
Please use 835. |
| Title: Shorthand syntax for Annotated type metadata (Draft Proposal) | ||
| Author: Till Varoquaux <till.varoquaux@gmail.com> | ||
| Sponsor: Ivan Levkivskyi <levkivskyi@gmail.com> | ||
| Discussions-To: https://discuss.python.org/t/106888 |
There was a problem hiding this comment.
Put this old one in the Post-History, the new one will go here.
| Status: Draft | ||
| Type: Standards Track | ||
| Topic: Typing | ||
| Created: 19-Apr-2026 |
There was a problem hiding this comment.
This is the date the PEP number is assigned.
| Created: 19-Apr-2026 | |
| Created: 12-Jun-2026 |
| Topic: Typing | ||
| Created: 19-Apr-2026 | ||
| Python-Version: 3.16 | ||
| Post-History: 19-Apr-2026 |
There was a problem hiding this comment.
This should be the date of each Discuss topic, with links.
| @@ -0,0 +1,473 @@ | |||
| PEP: 9999 | |||
There was a problem hiding this comment.
| PEP: 9999 | |
| PEP: 835 |
I've also added the new PEP checklist to the PR description, please check off the relevant ones, and remember the CODEOWNERS file.
| @@ -0,0 +1,473 @@ | |||
| PEP: 9999 | |||
| Title: Shorthand syntax for Annotated type metadata (Draft Proposal) | |||
There was a problem hiding this comment.
| Title: Shorthand syntax for Annotated type metadata (Draft Proposal) | |
| Title: Shorthand syntax for Annotated type metadata |
| that was concise but problematic for type checkers and runtime defaults:: | ||
|
|
||
| # The older, now discouraged pattern |
There was a problem hiding this comment.
If you like, you can add red or green sidebars to code blocks to indicate which are "bad"/"discouraged" or "good"/"preferred". See how PEP 8 does it.
For example:
| that was concise but problematic for type checkers and runtime defaults:: | |
| # The older, now discouraged pattern | |
| that was concise but problematic for type checkers and runtime defaults: | |
| .. code-block:: | |
| :class: bad | |
| # The older, now discouraged pattern |
Not required, and will leave others for you if you like :)
|
|
||
| Since its introduction in :pep:`593`, ``Annotated`` has become the standard | ||
| mechanism for attaching context-specific metadata to types. It is widely | ||
| embraced by libraries such as **Pydantic**, **FastAPI**, and **SQLModel**. |
There was a problem hiding this comment.
Not sure if we need bold for these in the abstract and here, at least not the second time.
| embraced by libraries such as **Pydantic**, **FastAPI**, and **SQLModel**. | |
| embraced by libraries such as Pydantic, FastAPI, and SQLModel. |
| the language. | ||
|
|
||
| This syntax also draws inspiration from other languages with strong metadata | ||
| ecosystems, notably Java. In Java (formalized in `JSR 308 <https://jcp.org/en/jsr/detail?id=308>`_) |
There was a problem hiding this comment.
Prefer anonymous links:
| ecosystems, notably Java. In Java (formalized in `JSR 308 <https://jcp.org/en/jsr/detail?id=308>`_) | |
| ecosystems, notably Java. In Java (formalized in `JSR 308 <https://jcp.org/en/jsr/detail?id=308>`__) |
| and other JVM languages, the ``@`` symbol is standard for type annotations:: | ||
|
|
There was a problem hiding this comment.
| and other JVM languages, the ``@`` symbol is standard for type annotations:: | |
| and other JVM languages, the ``@`` symbol is standard for type annotations: | |
| .. code-block:: java |
| object as ``X @ Y``:: | ||
|
|
There was a problem hiding this comment.
pycon for Python console
| object as ``X @ Y``:: | |
| object as ``X @ Y``: | |
| .. code-block:: pycon | |
| The ``repr()`` of an ``AnnotatedType`` uses the shorthand syntax:: | ||
|
|
There was a problem hiding this comment.
| The ``repr()`` of an ``AnnotatedType`` uses the shorthand syntax:: | |
| The ``repr()`` of an ``AnnotatedType`` uses the shorthand syntax: | |
| .. code-block:: pycon | |
| origin:: | ||
|
|
There was a problem hiding this comment.
| origin:: | |
| origin: | |
| .. code-block:: pycon | |
This PR introduces a draft PEP proposing a shorthand syntax for
typing.Annotatedusing the@operator (e.g.,x: int @ "metadata"instead oftyping.Annotated[int, "metadata"]).This proposal aims to improve the developer ergonomics of metadata-heavy typing frameworks like Pydantic, FastAPI, and SQLModel, aligning Python's type annotations with the conciseness of JVM languages (Java, Kotlin).
All prototype implementations (CPython, Mypy, Pyright, Ruff) are complete and linked in the Reference Implementation section.
Basic requirements (all PEP Types)
pep-NNNN.rst), PR title (PEP 123: <Title of PEP>) andPEPheaderAuthororSponsor, and formally confirmed their approvalAuthor,Status(Draft),TypeandCreatedheaders filled out correctlyPEP-Delegate,Topic,RequiresandReplacesheaders completed if appropriate.github/CODEOWNERSfor the PEPStandards Track requirements
Python-Versionset to valid (pre-beta) future Python version, if relevantDiscussions-ToandPost-History