10000 bpo-10379: deprecate locale.format in lieu of locale.format_string by plusminushalf · Pull Request #259 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-10379: deprecate locale.format in lieu of locale.format_string #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Deprecating but not changing the function defination
  • Loading branch information
plusminushalf committed Feb 24, 2017
commit a08419fbd8b1e0e8fd0d3b22828245c7600a8186
3 changes: 1 addition & 2 deletions Doc/library/locale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ The :mod:`locale` module defines the following exception and functions:
locale settings into account.

.. versionchanged:: 3.7
Added *monetary*, if true the conversion uses monetary thousands separator and
grouping strings.
The *monetary* keyword parameter was added.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need the description of the monetary keyword (presumably copy and pasted from the existing format entry) in the description of the format_string function.

In fact, what we really want to do is copy most of the 'format' description into format_string, since it seems wrong somehow to refer to the docs of a deprecated from from the preferred function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing indentation.

Replaces :meth:`format`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the 'replaces' sentence is needed. The deprecated on format covers that.



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description of the monetary parameter should be added to the regular docs, and the version changed phrase should just say "the monetary keyword parameter was added"

Expand Down
10 changes: 8 additions & 2 deletions Lib/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ def format(percent, value, grouping=False, monetary=False, *additional):
DeprecationWarning, stacklevel=2
)

return format_string(percent, value, grouping)
match = _percent_re.match(percent)
if not match or len(match.group())!= len(percent):
raise ValueError(("format() must be given exactly one %%char "
"format specifier, %s not valid") % repr(percent))
return _format(percent, value, grouping, monetary, *additional)

def _format(percent, value, grouping=False, monetary=False, *additional):
if additional:
Expand Down Expand Up @@ -218,7 +222,9 @@ def _format(percent, value, grouping=False, monetary=False, *additional):
def format_string(f, val, grouping=False, monetary=False):
"""Formats a string in the same way that the % formatting would use,
but takes the current locale into account.
Grouping is applied if the third parameter is true."""
Grouping is applied if the third parameter is true.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we are editing this, I think there should be a blank line after the first sentence, before the 'Grouping' sentence.

Conversion uses monetary thousands separator and grouping strings if
forth parameter monetary is true."""
percents = list(_percent_re.finditer(f))
new_f = _percent_re.sub('%s', f)

Expand Down
18 changes: 8 additions & 10 deletions Lib/test/test_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,9 @@ def test_padding(self):
def test_format_deprecation(self):
with warnings.catch_warnings(record=True) as w:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use assertWarns here instead.

warnings.simplefilter("always", DeprecationWarning)
locale.format("%.0f KB", 100, grouping=True)
locale.format("%-10.f", 4200, grouping=True)
for warning in w:
self.assertTrue(warning.category is DeprecationWarning)
self.assertEqual(
locale.format("%.0f KB", 100, grouping=True), "100 KB")

def test_complex_formatting(self):
# Spaces in formatting string
Expand Down Expand Up @@ -238,13 +236,13 @@ class TestFormatPatternArg(unittest.TestCase):

def test_onlyOnePattern(self):
# Issue 2522: accept exactly one % pattern, and no extra chars.
self.assertRaises(TypeError, locale.format, "%f\n", 'foo')
self.assertRaises(TypeError, locale.format, "%f\r", 'foo')
self.assertRaises(TypeError, locale.format, "%f\r\n", 'foo')
self.assertRaises(TypeError, locale.format, " %f", 'foo')
self.assertRaises(TypeError, locale.format, "%fg", 'foo')
self.assertRaises(TypeError, locale.format, "%^g", 'foo')
self.assertRaises(TypeError, locale.format, "%f%%", 'foo')
self.assertRaises(ValueError, locale.format, "%f\n", 'foo')
self.assertRaises(ValueError, locale.format, "%f\r", 'foo')
self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo')
self.assertRaises(ValueError, locale.format, " %f", 'foo')
self.assertRaises(ValueError, locale.format, "%fg", 'foo')
self.assertRaises(ValueError, locale.format, "%^g", 'foo')
self.assertRaises(ValueError, locale.format, "%f%%", 'foo')


class TestLocaleFormatString(unittest.TestCase):
Expand Down
0