Skip to content

PEP 849: "More Expressive Type Expressions" - #5132

Open
ImogenBits wants to merge 25 commits into
python:mainfrom
ImogenBits:ast_format
Open

ImogenBits wants to merge 25 commits into
python:mainfrom
ImogenBits:ast_format

Conversation

@ImogenBits

Copy link
Copy Markdown

Basic requirements (all PEP Types)

  • Read and followed PEP 1 & PEP 12
  • File created from the latest PEP template
  • PEP has next available number, & set in filename (pep-NNNN.rst), PR title (PEP 123: <Title of PEP>) and PEP header
    • Tip: find the next available number with pepotron — run uvx pepotron next (or pipx install pepotron then pep next)
  • Title clearly, accurately and concisely describes the content in 79 characters or less
  • Core dev/PEP editor listed as Author or Sponsor, and formally confirmed their approval
  • Author, Status (Draft), Type and Created headers filled out correctly
  • PEP-Delegate, Topic, Requires and Replaces headers completed if appropriate
  • Required sections included
    • Abstract (first section)
    • Copyright (last section; exact wording from template required)
  • Code is well-formatted (PEP 7/PEP 8) and is in code blocks, with the right lexer names if non-Python
  • PEP builds with no warnings, pre-commit checks pass and content displays as intended in the rendered HTML
  • Authors/sponsor added to .github/CODEOWNERS for the PEP

Standards Track requirements

  • PEP topic discussed in a suitable venue with general agreement that a PEP is appropriate
  • Suggested sections included (unless not applicable)
    • Motivation
    • Specification
    • Rationale
    • Backwards Compatibility
    • Security Implications
    • How to Teach This
    • Reference Implementation
    • Rejected Ideas
    • Open Issues
    • Acknowledgements
    • Footnotes
    • Change History
  • Python-Version set to valid (pre-beta) future Python version, if relevant
  • Any project stated in the PEP as supporting/endorsing/benefiting from the PEP formally confirmed such
  • Right before or after initial merging, PEP discussion thread created and linked to in Discussions-To and Post-History

I've used 849 since this is a continuation of PEPs 649 and 749 and it's almost the next available number. I'm happy to change it to whatever is next (currently 848) if that is more appropriate.

@ImogenBits
ImogenBits requested a review from a team as a code owner September 20, 2026 09:56
@python-cla-bot

python-cla-bot Bot commented Sep 20, 2026

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@read-the-docs-community

read-the-docs-community Bot commented Sep 20, 2026

Copy link
Copy Markdown

Documentation build overview

📚 pep-previews | 🛠️ Build #34668782 | 📁 Comparing b5d9810 against latest (8766f78)

  🔍 Preview build  

5 files changed · + 1 added · ± 4 modified

+ Added

± Modified

@JelleZijlstra JelleZijlstra left a comment

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.

Thank you!

Comment thread peps/pep-0849.rst Outdated
Comment thread peps/pep-0849.rst
PEP: 849
Title: More Expressive Type Expressions
Author: Imogen Hergeth <python at imogen.tech>
Sponsor: Jelle Zijlstra <jelle.zijlstra at gmail.com>

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.

Confirming my sponsorship

Comment thread peps/pep-0849.rst Outdated
Comment thread peps/pep-0849.rst Outdated
Comment thread peps/pep-0849.rst
this a worthwhile trade off. The specification of the proposed format also is open
enough that many optimizations are possible should they be deemed necessary in
the future. For many users, this proposal will even be a slight performance
increase since their code never evaluates any annotate functions.

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.

What about the effect on e.g. dataclasses, which need to evaluate their annotations? Will this make dataclass creation slower?

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.

I've added a paragraph explaining the impact of this PEP on these kinds of introspection tools.

Comment thread peps/pep-0849.rst Outdated
Comment thread peps/pep-0849.rst Outdated
***********************

Since this PEP only adds new functionality, there are no direct backwards
compatibility concerns. However, we also want to point out that future additions

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.

Deprecating get_type_hints is a pretty big compatibility concern

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.

Do you mean that I should add a part in the backwards compatibility section talking about this or drop the deprecation entirely? I don't have any strong opinions either way, I just assumed that since get_type_hints would be largely useless it is better to deprecate it than have a somewhat broken helper function in the standard library.

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.

I think deprecating it is fine, but it deserves explicit discussion here.

Comment thread peps/pep-0849.rst
Storing Annotation Source Code
==============================

Instead of storing binary data that defines the annotations' ASTs an alternative

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.

The current approach means that the STRING format will still not faithfully represent the original string though, right? I think that's a minor issue but worth noting.

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.

I've thought about this but I'm not sure what the best approach is. The AST data can be used to recreate the actual source code for the STRING format (same with FORWARDREF). We wouldn't even have to add much since the current approach already is to construct an AST and then unparse it. We could either make the annotate functions directly do that to support the format or implement another case in get_annotations. I haven't added that to the PEP so far just because it isn't directly related to the main idea behind it, but if it would be better to include it I'd be happy to.

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.

Oh, I was thinking of the fact that we're losing formatting details (e.g. whitespace), which I assume would still be the case with your proposal.

I definitely think that we should implement STRING on top of AST now. It would be much simpler (essentially just calling ast.unparse) and work better in edge cases.

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.

That's a good point, I've now explicitly pointed this out in the PEP.

Comment thread peps/pep-0849.rst Outdated
Comment thread peps/pep-0849.rst Outdated
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
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.

2 participants