Skip to content

Add support for t-strings (PEP 750).#20850

Merged
JukkaL merged 4 commits intopython:masterfrom
nascheme:tstring
Feb 25, 2026
Merged

Add support for t-strings (PEP 750).#20850
JukkaL merged 4 commits intopython:masterfrom
nascheme:tstring

Conversation

@nascheme
Copy link
Member

This is based on the #19333 by @brianschubert. I (with the help of Claude Code) modified as suggested by the PR comments to add mypy AST nodes. I'm not very familiar with mypy internals but this seems to work. Tests have been added to check that t-strings are handled as expected. I also tested with Python 3.13 to ensure this doesn't break with older versions.

Fixes #19329

Co-authored-by: Brian Schubert <[email protected]>
Co-authored-by: Claude <[email protected]>
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

On Python < 3.14, ast_Interpolation is Any.  Avoid typing error by using
"type: ignore".  This matches what is done for `ast_TypeVarTuple`.
@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! It's important to have support for all Python features. This looks pretty good overall. There is at least one issues I could find (missing implicit dependency to string.templatelib), but it's easy enough for me to fix myself, so I'll prepare a follow-up PR soon and we can merge this. I'll also add some more test coverage.

@JukkaL JukkaL merged commit 15dcec8 into python:master Feb 25, 2026
24 checks passed
@ilevkivskyi
Copy link
Member

@nascheme
Btw another thing worth considering is adding precise checking for format specifiers, substitutions etc. We already have a lot of logic in checkstrformat.py (added for f-string). Would it be possible to simply apply the ~same logic to t-strings?

@ilevkivskyi
Copy link
Member

If what I am saying above is desired but non-trivial, please open a follow-up issue so that we will not forget about it.

JukkaL added a commit that referenced this pull request Feb 25, 2026
If a file uses t-strings, implicitly depend on `string.templatelib`.

Also improve test coverage a bit.

Follow-up to #20850.
JukkaL added a commit that referenced this pull request Feb 25, 2026
These are unsupported by mypyc for now.

Follow-up to #20850.
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.

MyPy fails on Python 3.14 PEP 750 template string (tstring, t-string), e.g. t'{42}'

3 participants