[New-bugs-announce] [issue24309] string.Template should be using str.format and/or deprecated

Dmitry Kazakov report at bugs.python.org
Thu May 28 06:25:30 CEST 2015

New submission from Dmitry Kazakov:

I came across this piece of code in Lib/string.py:146:

    # We use this idiom instead of str() because the latter will
    # fail if val is a Unicode containing non-ASCII characters.
    return '%s' % (mapping[named],)

This seems vestigial. I think it'd be appropriate to "fix" the string.Template by replacing the printf-style formatting with newer str.format everywhere in the Template's source. The obvious advantage is that by tweaking some regexes we'll make possible formatting using the following syntax


by dropping to the str.format

    return '{named}'.format(**mapping)  # <-- new version

It'd also make sense to use the str.format_map to implement the Template.safe_substitute.

Borrowing some ideas from issue1198569, we can then expose an additional attribute called Template.bracepattern to allow programmers use the str.format-based substitution extensively:

	${name.thing: >16}

This change won't break any existing code.

But I'm not exactly sure string.Template should be in Python 3 at all. It's one of the least used features and PEP 292 states it was added as an alternative to %-based substitution. I think it'd be deprecated and removed from the standard library.

So what's the resolution? Should we fix it or deprecate it or both?

components: Library (Lib)
messages: 244262
nosy: vlth
priority: normal
severity: normal
status: open
title: string.Template should be using str.format and/or deprecated
type: enhancement

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list