-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
bpo-34311: Add locale.localize #15275
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
Conversation
Misc/NEWS.d/next/Library/2019-08-14-13-19-50.bpo-33731.9esS0d.rst
Outdated
Show resolved
Hide resolved
44b7d83
to
bcf84aa
Compare
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
I have made the requested changes; please review again |
Thanks for making the requested changes! @matrixise: please review the changes made to this pull request. |
ping |
1 similar comment
ping |
I really would like to see this landed in Python so I could apply a backport to fix https://bugs.tryton.org/issue8574 |
Lib/locale.py
Outdated
@@ -321,6 +327,11 @@ def delocalize(string): | |||
string = string.replace(dd, '.') | |||
return string | |||
|
|||
def localize(string, grouping=False, monetary=False): | |||
"""Parses a string as locale number according to the locale settings.""" | |||
float(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.
What's the float(string) for?
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.
Ensure that it is a string that represent a valid number.
Lib/locale.py
Outdated
@@ -321,6 +327,11 @@ def delocalize(string): | |||
string = string.replace(dd, '.') | |||
return string | |||
|
|||
def localize(string, grouping=False, monetary=False): | |||
"""Parses a string as locale number according to the locale settings.""" | |||
float(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.
Ensure that it is a string that represent a valid number.
Hmm, I'm not sure that I agree with this means of validating the strings. Primarily because it will result in a non-obvious error message for users. For example, localize('')
would result in:
ValueError: could not convert string to float: ''
I suspect that this message would make zero sense to users unless they looked at the implementation of the function, which should not be a requirement for diagnosing error messages.
IMO, you should at the very least catch the ValueError
and then re-raise it with a more informative error message. For example:
float(string) | |
try: | |
float(string) | |
except ValueError: | |
raise ValueError(f"{string} is not a valid number") |
Edit: It may also be worth considering that there might be some valid floats that you wouldn't consider to be valid for this function, such as float('NAN')
, float('INF')
, and float('-INF')
. For more thorough validation, it could be worthwhile to use regex 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.
Indeed I added this for #15275 (review). But as delocalize does not perform any check and passing a non valid number will not fail, I think it is better to relax the check.
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.
That review link seems to be no longer working. Who was it that originally suggested that change? Particularly if it was from @matrixise (or other core dev), I would highly recommended further discussing the removal of that change with them.
ping @matrixise |
This allow to format Decimal without losing precision by using the new format syntax and than localize the result.
@matrixise could you review the changes you requested, thanks. |
@cedk If you wouldn't mind, could you avoid force-pushing the PR branch after each commit in the future? It makes it rather difficult to follow the commit history of the PR, before and after the review feedback. We advise against doing so in the devguide:
|
@aeros sorry, I would no more do that. Indeed I'm used to mercurial for which such workflow is well supported thanks to evolve. |
@matrixise could you point me to the changes you are requesting? |
@cedk I am sincerely sorry if I was absent, I will look at your PR in the next two weeks. |
@cedk just one thing, could you update it with the last master? Thank you so much, |
But there is no conflict. |
Thank you so much for your PR, and I am really sorry for this very bid delay between your proposal and the merge. Thank you again for your patience. Take care |
This allow to format Decimal without losing precision by using the new
format syntax and than localize the result.
https://bugs.python.org/issue34311