python / python/cpython

Better `repr` for `string.templatelib.Template`.

Open
#151,099 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

Currently the repr output for template strings shows how the data is stored internally, but this is not how the template could be reconstructed via an constructor call and the format is unintuitive since the template parts are not output in the order they appear in the template source. The repr result should output the parts in the order as iterating over the template object does, i.e.

x = 1
y = 2
print(repr(t'{x} + {y}'))

should print

Template(Interpolation(1, 'x', None, ''), ' + ', Interpolation(2, 'y', None, ''))

instead of

Template(strings=('', ' + ', ''), interpolations=(Interpolation(1, 'x', None, ''), Interpolation(2, 'y', None, '')))

This has already been discussed at https://discuss.python.org/t/repr-output-of-t-strings/107653

I'm working on a patch/pull request.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/repr-output-of-t-strings/107653

Linked PRs
  • gh-151104

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the string.templatelib.Template repr behavior described in the issue and compare it with iteration over a template object. Verify that repr outputs Template parts in iteration order and matches the proposed constructor-call format; gh-151104 is linked, so review that work before starting.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.