Skip to content

Commit b876df4

Browse files
Issue #23671: string.Template now allows to specify the "self" parameter as
keyword argument. string.Formatter now allows to specify the "self" and the "format_string" parameters as keyword arguments.
2 parents d9d769f + 8ffe917 commit b876df4

6 files changed

Lines changed: 60 additions & 3 deletions

File tree

Doc/library/string.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ implementation as the built-in :meth:`format` method.
9595
an arbitrary set of positional and keyword arguments.
9696
:meth:`format` is just a wrapper that calls :meth:`vformat`.
9797

98+
.. deprecated:: 3.5
99+
Passing a format string as keyword argument *format_string* has been
100+
deprecated.
101+
98102
.. method:: vformat(format_string, args, kwargs)
99103

100104
This function does the actual work of formatting. It is exposed as a

Doc/whatsnew/3.5.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,10 @@ Deprecated Python modules, functions and methods
526526
:func:`~http.cookies.Morsel.set` method in order to avoid the deprecation
527527
warning.
528528

529+
* Passing a format string as keyword argument *format_string* to the
530+
:meth:`~string.Formatter.format` method of the :class:`string.Formatter`
531+
class has been deprecated.
532+
529533

530534
Deprecated functions and types of the C API
531535
-------------------------------------------

Lib/string.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ def _invalid(self, mo):
9494
raise ValueError('Invalid placeholder in string: line %d, col %d' %
9595
(lineno, colno))
9696

97-
def substitute(self, *args, **kws):
97+
def substitute(*args, **kws):
98+
if not args:
99+
raise TypeError("descriptor 'substitute' of 'Template' object "
100+
"needs an argument")
101+
self, *args = args # allow the "self" keyword be passed
98102
if len(args) > 1:
99103
raise TypeError('Too many positional arguments')
100104
if not args:
@@ -120,7 +124,11 @@ def convert(mo):
120124
self.pattern)
121125
return self.pattern.sub(convert, self.template)
122126

123-
def safe_substitute(self, *args, **kws):
127+
def safe_substitute(*args, **kws):
128+
if not args:
129+
raise TypeError("descriptor 'safe_substitute' of 'Template' object "
130+
"needs an argument")
131+
self, *args = args # allow the "self" keyword be passed
124132
if len(args) > 1:
125133
raise TypeError('Too many positional arguments')
126134
if not args:
@@ -160,7 +168,22 @@ def convert(mo):
160168
# The field name parser is implemented in _string.formatter_field_name_split
161169

162170
class Formatter:
163-
def format(self, format_string, *args, **kwargs):
171+
def format(*args, **kwargs):
172+
if not args:
173+
raise TypeError("descriptor 'format' of 'Formatter' object "
174+
"needs an argument")
175+
self, *args = args # allow the "self" keyword be passed
176+
try:
177+
format_string, *args = args # allow the "format_string" keyword be passed
178+
except ValueError:
179+
if 'format_string' in kwargs:
180+
format_string = kwargs.pop('format_string')
181+
import warnings
182+
warnings.warn("Passing 'format_string' as keyword argument is "
183+
"deprecated", DeprecationWarning, stacklevel=2)
184+
else:
185+
raise TypeError("format() missing 1 required positional "
186+
"argument: 'format_string'") from None
164187
return self.vformat(format_string, args, kwargs)
165188

166189
def vformat(self, format_string, args, kwargs):

Lib/test/test_pep292.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_regular_templates(self):
2626
self.assertEqual(s.substitute(dict(who='tim', what='ham')),
2727
'tim likes to eat a bag of ham worth $100')
2828
self.assertRaises(KeyError, s.substitute, dict(who='tim'))
29+
self.assertRaises(TypeError, Template.substitute)
2930

3031
def test_regular_templates_with_braces(self):
3132
s = Template('$who likes ${what} for ${meal}')
@@ -198,6 +199,9 @@ def test_keyword_arguments(self):
198199
eq(s.substitute(dict(mapping='one'), mapping='two'),
199200
'the mapping is two')
200201

202+
s = Template('the self is $self')
203+
eq(s.substitute(self='bozo'), 'the self is bozo')
204+
201205
def test_keyword_arguments_safe(self):
202206
eq = self.assertEqual
203207
raises = self.assertRaises
@@ -216,6 +220,9 @@ def test_keyword_arguments_safe(self):
216220
raises(TypeError, s.substitute, d, {})
217221
raises(TypeError, s.safe_substitute, d, {})
218222

223+
s = Template('the self is $self')
224+
eq(s.safe_substitute(self='bozo'), 'the self is bozo')
225+
219226
def test_delimiter_override(self):
220227
eq = self.assertEqual
221228
raises = self.assertRaises

Lib/test/test_string.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ def test_basic_formatter(self):
3131
self.assertEqual(fmt.format("foo"), "foo")
3232
self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
3333
self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
34+
self.assertRaises(TypeError, fmt.format)
35+
self.assertRaises(TypeError, string.Formatter.format)
36+
37+
def test_format_keyword_arguments(self):
38+
fmt = string.Formatter()
39+
self.assertEqual(fmt.format("-{arg}-", arg='test'), '-test-')
40+
self.assertRaises(KeyError, fmt.format, "-{arg}-")
41+
self.assertEqual(fmt.format("-{self}-", self='test'), '-test-')
42+
self.assertRaises(KeyError, fmt.format, "-{self}-")
43+
self.assertEqual(fmt.format("-{format_string}-", format_string='test'),
44+
'-test-')
45+
self.assertRaises(KeyError, fmt.format, "-{format_string}-")
46+
with self.assertWarnsRegex(DeprecationWarning, "format_string"):
47+
self.assertEqual(fmt.format(arg='test', format_string="-{arg}-"),
48+
'-test-')
3449

3550
def test_auto_numbering(self):
3651
fmt = string.Formatter()

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Core and Builtins
3030
Library
3131
-------
3232

33+
- Issue #23671: string.Template now allows to specify the "self" parameter as
34+
keyword argument. string.Formatter now allows to specify the "self" and
35+
the "format_string" parameters as keyword arguments.
36+
3337
- Issue #23502: The pprint module now supports mapping proxies.
3438

3539
- Issue #17530: pprint now wraps long bytes objects and bytearrays.

0 commit comments

Comments
 (0)