| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,6 +95,10 @@ implementation as the built-in :meth:`format` method. | |||
| 95 | 95 | an arbitrary set of positional and keyword arguments. | |
| 96 | 96 | :meth:`format` is just a wrapper that calls :meth:`vformat`. | |
| 97 | 97 | ||
| 98 | + .. deprecated:: 3.5 | ||
| 99 | + Passing a format string as keyword argument *format_string* has been | ||
| 100 | + deprecated. | ||
| 101 | + | ||
| 98 | 102 | .. method:: vformat(format_string, args, kwargs) | |
| 99 | 103 | ||
| 100 | 104 | This function does the actual work of formatting. It is exposed as a | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -526,6 +526,10 @@ Deprecated Python modules, functions and methods | |||
| 526 | 526 | :func:`~http.cookies.Morsel.set` method in order to avoid the deprecation | |
| 527 | 527 | warning. | |
| 528 | 528 | ||
| 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 | + | ||
| 529 | 533 | ||
| 530 | 534 | Deprecated functions and types of the C API | |
| 531 | 535 | ------------------------------------------- | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -94,7 +94,11 @@ def _invalid(self, mo): | |||
| 94 | 94 | raise ValueError('Invalid placeholder in string: line %d, col %d' % | |
| 95 | 95 | (lineno, colno)) | |
| 96 | 96 | ||
| 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 | ||
| 98 | 102 | if len(args) > 1: | |
| 99 | 103 | raise TypeError('Too many positional arguments') | |
| 100 | 104 | if not args: | |
@@ -120,7 +124,11 @@ def convert(mo): | |||
| 120 | 124 | self.pattern) | |
| 121 | 125 | return self.pattern.sub(convert, self.template) | |
| 122 | 126 | ||
| 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 | ||
| 124 | 132 | if len(args) > 1: | |
| 125 | 133 | raise TypeError('Too many positional arguments') | |
| 126 | 134 | if not args: | |
@@ -160,7 +168,22 @@ def convert(mo): | |||
| 160 | 168 | # The field name parser is implemented in _string.formatter_field_name_split | |
| 161 | 169 | ||
| 162 | 170 | 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 | ||
| 164 | 187 | return self.vformat(format_string, args, kwargs) | |
| 165 | 188 | ||
| 166 | 189 | def vformat(self, format_string, args, kwargs): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,6 +26,7 @@ def test_regular_templates(self): | |||
| 26 | 26 | self.assertEqual(s.substitute(dict(who='tim', what='ham')), | |
| 27 | 27 | 'tim likes to eat a bag of ham worth $100') | |
| 28 | 28 | self.assertRaises(KeyError, s.substitute, dict(who='tim')) | |
| 29 | + self.assertRaises(TypeError, Template.substitute) | ||
| 29 | 30 | ||
| 30 | 31 | def test_regular_templates_with_braces(self): | |
| 31 | 32 | s = Template('$who likes ${what} for ${meal}') | |
@@ -198,6 +199,9 @@ def test_keyword_arguments(self): | |||
| 198 | 199 | eq(s.substitute(dict(mapping='one'), mapping='two'), | |
| 199 | 200 | 'the mapping is two') | |
| 200 | 201 | ||
| 202 | + s = Template('the self is $self') | ||
| 203 | + eq(s.substitute(self='bozo'), 'the self is bozo') | ||
| 204 | + | ||
| 201 | 205 | def test_keyword_arguments_safe(self): | |
| 202 | 206 | eq = self.assertEqual | |
| 203 | 207 | raises = self.assertRaises | |
@@ -216,6 +220,9 @@ def test_keyword_arguments_safe(self): | |||
| 216 | 220 | raises(TypeError, s.substitute, d, {}) | |
| 217 | 221 | raises(TypeError, s.safe_substitute, d, {}) | |
| 218 | 222 | ||
| 223 | + s = Template('the self is $self') | ||
| 224 | + eq(s.safe_substitute(self='bozo'), 'the self is bozo') | ||
| 225 | + | ||
| 219 | 226 | def test_delimiter_override(self): | |
| 220 | 227 | eq = self.assertEqual | |
| 221 | 228 | raises = self.assertRaises | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,6 +31,21 @@ def test_basic_formatter(self): | |||
| 31 | 31 | self.assertEqual(fmt.format("foo"), "foo") | |
| 32 | 32 | self.assertEqual(fmt.format("foo{0}", "bar"), "foobar") | |
| 33 | 33 | 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-') | ||
| 34 | 49 | ||
| 35 | 50 | def test_auto_numbering(self): | |
| 36 | 51 | fmt = string.Formatter() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,6 +30,10 @@ Core and Builtins | |||
| 30 | 30 | Library | |
| 31 | 31 | ------- | |
| 32 | 32 | ||
| 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 | + | ||
| 33 | 37 | - Issue #23502: The pprint module now supports mapping proxies. | |
| 34 | 38 | ||
| 35 | 39 | - Issue #17530: pprint now wraps long bytes objects and bytearrays. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments