Skip to content

PEP 750: restrict support for Template + str addition #4395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

davepeck
Copy link
Contributor

@davepeck davepeck commented May 1, 2025

  • Change is either:
    • To a Draft PEP
    • To an Accepted or Final PEP, with Steering Council approval
    • To fix an editorial issue (markup, typo, link, header, etc)
  • PR title prefixed with PEP number (e.g. PEP 123: Summary of changes)

Following this PR opened with the steering-council and related discussion, we propose an update to PEP 750 that:

  1. Removes support for Template.__radd__ entirely
  2. Removes support for str in Template.__add__

A rationale is provided and the two relevant sections of the PEP are updated ("Template String Concatenation" and, in rejected ideas, "Disallowing Template Concatenation").


📚 Documentation preview 📚: https://pep-previews--4395.org.readthedocs.build/

@AA-Turner
Copy link
Member

Adding the label to prevent inadvertent merging before pronouncement

template = t"Hello " + Template(Interpolation(name, "name"))

Python's implicit concatenation syntax is supported between any combination
of ``Template`` and ``str``:
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if it shouldn't be more explicit that the str is a literal string, not the result of an f-string expression? See this conversation:

sql_string = t"SELECT id,name "
                   t"FROM users "
                   f"WHERE name={col}"  # 1-chart typo causing injection

Copy link
Contributor Author

@davepeck davepeck May 20, 2025

Choose a reason for hiding this comment

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

The intent was to continue to allow t"" f"". We should definitely explicitly mention it in the PEP edit.

As mentioned in Python discuss, either we keep both t"" "" and t"" f"" implicit concatenation, or we drop both.

The proposed change to the PEP keeps them, with the view that t"" "" is plausibly useful, and t"" f"" is both rare and trivially lintable. It also acknowledges that there may be real implementation risk in removing some kinds of implicit concat while retaining others (aka ”” f”” should continue to work) — risk that may simply be beyond what is acceptable at this late stage.

An argument to instead drop both forms of implicit concatenation would be: "hey, why should any form of literal construction of t-strings, no matter how uncommon, lead to a plausible injection?"

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

Successfully merging this pull request may close these issues.

3 participants