diff options
-rw-r--r-- | Doc/library/string.rst | 4 | ||||
-rw-r--r-- | Doc/whatsnew/3.5.rst | 4 | ||||
-rw-r--r-- | Lib/string.py | 29 | ||||
-rw-r--r-- | Lib/test/test_pep292.py | 7 | ||||
-rw-r--r-- | Lib/test/test_string.py | 15 | ||||
-rw-r--r-- | Misc/NEWS | 4 |
6 files changed, 60 insertions, 3 deletions
diff --git a/Doc/library/string.rst b/Doc/library/string.rst index f9da5fa..eab9f37 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -95,6 +95,10 @@ implementation as the built-in :meth:`format` method. an arbitrary set of positional and keyword arguments. :meth:`format` is just a wrapper that calls :meth:`vformat`. + .. deprecated:: 3.5 + Passing a format string as keyword argument *format_string* has been + deprecated. + .. method:: vformat(format_string, args, kwargs) This function does the actual work of formatting. It is exposed as a diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst index 44915a3..aa5af04 100644 --- a/Doc/whatsnew/3.5.rst +++ b/Doc/whatsnew/3.5.rst @@ -526,6 +526,10 @@ Deprecated Python modules, functions and methods :func:`~http.cookies.Morsel.set` method in order to avoid the deprecation warning. +* Passing a format string as keyword argument *format_string* to the + :meth:`~string.Formatter.format` method of the :class:`string.Formatter` + class has been deprecated. + Deprecated functions and types of the C API ------------------------------------------- diff --git a/Lib/string.py b/Lib/string.py index 72a09f7..f3365c6 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -94,7 +94,11 @@ class Template(metaclass=_TemplateMetaclass): raise ValueError('Invalid placeholder in string: line %d, col %d' % (lineno, colno)) - def substitute(self, *args, **kws): + def substitute(*args, **kws): + if not args: + raise TypeError("descriptor 'substitute' of 'Template' object " + "needs an argument") + self, *args = args # allow the "self" keyword be passed if len(args) > 1: raise TypeError('Too many positional arguments') if not args: @@ -120,7 +124,11 @@ class Template(metaclass=_TemplateMetaclass): self.pattern) return self.pattern.sub(convert, self.template) - def safe_substitute(self, *args, **kws): + def safe_substitute(*args, **kws): + if not args: + raise TypeError("descriptor 'safe_substitute' of 'Template' object " + "needs an argument") + self, *args = args # allow the "self" keyword be passed if len(args) > 1: raise TypeError('Too many positional arguments') if not args: @@ -160,7 +168,22 @@ class Template(metaclass=_TemplateMetaclass): # The field name parser is implemented in _string.formatter_field_name_split class Formatter: - def format(self, format_string, *args, **kwargs): + def format(*args, **kwargs): + if not args: + raise TypeError("descriptor 'format' of 'Formatter' object " + "needs an argument") + self, *args = args # allow the "self" keyword be passed + try: + format_string, *args = args # allow the "format_string" keyword be passed + except ValueError: + if 'format_string' in kwargs: + format_string = kwargs.pop('format_string') + import warnings + warnings.warn("Passing 'format_string' as keyword argument is " + "deprecated", DeprecationWarning, stacklevel=2) + else: + raise TypeError("format() missing 1 required positional " + "argument: 'format_string'") from None return self.vformat(format_string, args, kwargs) def vformat(self, format_string, args, kwargs): diff --git a/Lib/test/test_pep292.py b/Lib/test/test_pep292.py index 6da8d2e..fd5256c 100644 --- a/Lib/test/test_pep292.py +++ b/Lib/test/test_pep292.py @@ -26,6 +26,7 @@ class TestTemplate(unittest.TestCase): self.assertEqual(s.substitute(dict(who='tim', what='ham')), 'tim likes to eat a bag of ham worth $100') self.assertRaises(KeyError, s.substitute, dict(who='tim')) + self.assertRaises(TypeError, Template.substitute) def test_regular_templates_with_braces(self): s = Template('$who likes ${what} for ${meal}') @@ -198,6 +199,9 @@ class TestTemplate(unittest.TestCase): eq(s.substitute(dict(mapping='one'), mapping='two'), 'the mapping is two') + s = Template('the self is $self') + eq(s.substitute(self='bozo'), 'the self is bozo') + def test_keyword_arguments_safe(self): eq = self.assertEqual raises = self.assertRaises @@ -216,6 +220,9 @@ class TestTemplate(unittest.TestCase): raises(TypeError, s.substitute, d, {}) raises(TypeError, s.safe_substitute, d, {}) + s = Template('the self is $self') + eq(s.safe_substitute(self='bozo'), 'the self is bozo') + def test_delimiter_override(self): eq = self.assertEqual raises = self.assertRaises diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py index 3e65071..f8731b8 100644 --- a/Lib/test/test_string.py +++ b/Lib/test/test_string.py @@ -31,6 +31,21 @@ class ModuleTest(unittest.TestCase): self.assertEqual(fmt.format("foo"), "foo") self.assertEqual(fmt.format("foo{0}", "bar"), "foobar") self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6") + self.assertRaises(TypeError, fmt.format) + self.assertRaises(TypeError, string.Formatter.format) + + def test_format_keyword_arguments(self): + fmt = string.Formatter() + self.assertEqual(fmt.format("-{arg}-", arg='test'), '-test-') + self.assertRaises(KeyError, fmt.format, "-{arg}-") + self.assertEqual(fmt.format("-{self}-", self='test'), '-test-') + self.assertRaises(KeyError, fmt.format, "-{self}-") + self.assertEqual(fmt.format("-{format_string}-", format_string='test'), + '-test-') + self.assertRaises(KeyError, fmt.format, "-{format_string}-") + with self.assertWarnsRegex(DeprecationWarning, "format_string"): + self.assertEqual(fmt.format(arg='test', format_string="-{arg}-"), + '-test-') def test_auto_numbering(self): fmt = string.Formatter() @@ -30,6 +30,10 @@ Core and Builtins Library ------- +- 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. + - Issue #23502: The pprint module now supports mapping proxies. - Issue #17530: pprint now wraps long bytes objects and bytearrays. |