-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
bpo-10379: deprecate locale.format in lieu of locale.format_string #259
Conversation
plusminushalf
commented
Feb 23, 2017
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow these steps to rectify the issue:
Thanks again to your contribution and we look forward to looking at it! |
Thanks for working on this. Doc changes are also needed. |
As well as tests for the deprecation message. |
Somehow I hadn't noticed that 'monetary' wasn't supported by format_string. So that is actually an enhancement, in addition to the deprecation, and needs to be documented as such (versionchanged, whats new entry). Also, format itself shouldn't be changed, just deprecated (that is, the deprecation message code added, but nothing else in it changed). |
@bitdancer I don't think to keep locale.format as it is and just displaying deprecated warning would be of any use. I think it is safe to call locale.format_string internally. |
Doc/library/locale.rst
Outdated
Added *monetary*, if true the conversion uses monetary thousands separator and | ||
grouping strings. | ||
Replaces :meth:`format`. | ||
|
||
|
There was a problem hiding this comment.
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"
That's the way we normally do deprecations: leave the existing code in place (so its behavior doesn't change) but point people to the preferred solution. It would be acceptable if there were no behavior changes, but the changed tests prove that there are. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Almost there :)
It would also be good to have an entry for the What's New document for 3.7 for the addition of the monetary parameter to format_string, as well as the deprecation of format. I haven't looked; if the 3.7 what's new hasn't been organized yet, just stick in a placeholder sentence with a reference to the bpo issue number.
Doc/library/locale.rst
Outdated
@@ -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. |
There was a problem hiding this comment.
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.
Lib/test/test_locale.py
Outdated
@@ -197,6 +198,13 @@ def test_padding(self): | |||
self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10)) | |||
self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10)) | |||
|
|||
def test_format_deprecation(self): | |||
with warnings.catch_warnings(record=True) as w: |
There was a problem hiding this comment.
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.
Doc/library/locale.rst
Outdated
|
||
Processes formatting specifiers as in ``format % val``, but takes the current | ||
locale settings into account. | ||
|
||
.. versionchanged:: 3.7 | ||
The *monetary* keyword parameter was added. | ||
Replaces :meth:`format`. |
There was a problem hiding this comment.
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.
Fixed recommended CR changes
052f6bc
to
4e24a3f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sticking with this :)
Doc/library/locale.rst
Outdated
|
||
Processes formatting specifiers as in ``format % val``, but takes the current | ||
locale settings into account. | ||
|
||
.. versionchanged:: 3.7 | ||
The *monetary* keyword parameter was added. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing indentation.
Doc/whatsnew/3.7.rst
Outdated
@@ -158,6 +158,11 @@ API and Feature Removals | |||
Python 3.1, and has now been removed. Use the :func:`~os.path.splitdrive` | |||
function instead. | |||
|
|||
* Deprecated :meth:`format` from :mod:`locale`, use the :meth:`format_string` | |||
instead. Added another argument *monetary* in :meth:`format_string` of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
format hasn't been removed, so the deprecation should go in the deprecation section. The feature addition of the monetary keyword should go in the 'improved modules' section.
Codecov Report
@@ Coverage Diff @@
## master #259 +/- ##
==========================================
- Coverage 83.38% 82.38% -1.01%
==========================================
Files 1367 1428 +61
Lines 344897 351221 +6324
==========================================
+ Hits 287596 289342 +1746
- Misses 57301 61879 +4578 Continue to review full report at Codecov.
|
Doc/whatsnew/3.7.rst
Outdated
@@ -93,6 +93,10 @@ New Modules | |||
Improved Modules | |||
================ | |||
|
|||
* Added another argument *monetary* in :meth:`format_string` of :mod:`locale`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a subsection header before this line for the locale module (the modules with enhancements are listed in this section in alphabetical order).
@bitdancer is it good to go now? |
Yes, I think so. I'd like to do a final overall review before I approve it, though. With any luck I'll do that tonight. |
Thanks! :) |
Hey, @bitdancer have you reviewed this? |
I did...and I feel bad that I have some more changes to suggest, so I was going to make them myself and submit it to the PR, but I haven't actually figured out how to do that yet. |
@bitdancer well I think you can suggest me the changes I'll do my best to figure them out. |
Doc/library/locale.rst
Outdated
@@ -365,12 +365,26 @@ The :mod:`locale` module defines the following exception and functions: | |||
Please note that this function will only work for exactly one %char specifier. | |||
For whole format strings, use :func:`format_string`. | |||
|
|||
.. deprecated:: 3.7 | |||
Use :meth:`format_string` instead |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So looking at this again, I think what we should do is move 'format' under 'format_string', and change its body to say "works like format_string but only accepts a single format specification. That followed by the deprecation notice will make it a lot less likely anyone will use it :)
Doc/whatsnew/3.7.rst
Outdated
If *monetary* is true, the conversion uses monetary thousands separator and | ||
grouping strings. | ||
(Contributed by Garvit in :issue:`10379`.) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit: our convention appears to be to put the Contributed by as part of the paragraph when there's only one paragraph. Ends up formatted the same in html, but might as well make the ReST consistent as well.
Lib/locale.py
Outdated
"""Deprecated, use format_string instead.""" | ||
warnings.warn( | ||
"This method will be removed in future versions. " | ||
"Use 'locale.format_string()' instead.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should read "This method will be removed in a future version of Python."
Lib/locale.py
Outdated
"""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. |
There was a problem hiding this comment.
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.
Lib/locale.py
Outdated
@@ -262,7 +266,7 @@ def currency(val, symbol=True, grouping=False, international=False): | |||
raise ValueError("Currency formatting is not possible using " | |||
"the 'C' locale.") | |||
|
|||
s = format('%%.%if' % digits, abs(val), grouping, monetary=True) | |||
s = format_string('%%.%if' % digits, abs(val), grouping, monetary=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we know there's only one format string here, I think this should be a call to _format.
Lib/locale.py
Outdated
@@ -298,7 +302,7 @@ def currency(val, symbol=True, grouping=False, international=False): | |||
|
|||
def str(val): | |||
"""Convert float to string, taking the locale into account.""" | |||
return format("%.12g", val) | |||
return format_string("%.12g", val) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, I think this should be _format.
Refactoring Doc, moving format doc below format_string, this is to make sure people have lesser probability of using format in future. Also using _format method inside locale module, since we know there is only one format function that is _format
There was no need to move format in the source file, but it doesn't hurt, either. |
Hey @bitdancer is there anything left to get this merged? |
Just adding the Misc/NEWS entry. I'm not sure what status is on how to do that currently, but I think it is still manual edit of that file. I haven't had time yet to learn how to update a PR before merge, so if you add the entry, I'll hit merge :) |
Misc/NEWS
Outdated
@@ -364,6 +364,8 @@ Library | |||
- bpo-29534: Fixed different behaviour of Decimal.from_float() | |||
for _decimal and _pydecimal. Thanks Andrew Nester. | |||
|
|||
- bpo-10379: Deprecate locale.format in lue of locale.format_string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Google says it's spelled 'lieu'. Also the entry should note that the monetary argument was added to format_string. So, how about "locale.format_string now supports the 'monetary' keyword argument, and locale.format is deprecated."
Update the Stackless patches for readthedocs.org based on the documentation. This simplified configuration is still untested.
Well, the previous commit didn't build on readthedocs.org. - Rename .readthedocs.yml to .readthedocs.yaml See https://docs.readthedocs.io/en/stable/config-file/index.html - Add a requirements file Doc/slp_readthedocs_requirements.txt and require the same packages as specified in Makefile. Already set the Sphinx version to 2.0.1, because upstream commit 7d23dbe
Set the css-file to "pydoctheme.css".
Remove the explicit specification of the css-file for readthedocs.org. It is not needed any more.