Skip to content

Better repr for string.templatelib.Template. #151099

@doerwalter

Description

@doerwalter

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions