Skip to content

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

Merged
merged 3 commits into from
Apr 12, 2021
Merged

bpo-34311: Add locale.localize #15275

merged 3 commits into from
Apr 12, 2021

Conversation

cedk
Copy link
Contributor

@cedk cedk commented Aug 14, 2019

This allow to format Decimal without losing precision by using the new
format syntax and than localize the result.

https://bugs.python.org/issue34311

@cedk cedk force-pushed the locale-format branch 2 times, most recently from 44b7d83 to bcf84aa Compare August 24, 2019 23:03
@matrixise matrixise self-assigned this Sep 11, 2019
@bedevere-bot
Copy link

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. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@cedk
Copy link
Contributor Author

cedk commented Sep 12, 2019

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@matrixise: please review the changes made to this pull request.

@cedk
Copy link
Contributor Author

cedk commented Sep 29, 2019

ping

1 similar comment
@cedk
Copy link
Contributor Author

cedk commented Nov 6, 2019

ping

@cedk
Copy link
Contributor Author

cedk commented Dec 3, 2019

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)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@aeros aeros added the type-feature A feature request or enhancement label Jan 12, 2020
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)
Copy link
Contributor

@aeros aeros Jan 12, 2020

Choose a reason for hiding this comment

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

@cedk

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:

Suggested change
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.

Copy link
Contributor Author

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.

Copy link
Contributor

@aeros aeros Jan 12, 2020

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.

@cedk
Copy link
Contributor Author

cedk commented Jan 27, 2020

ping @matrixise

This allow to format Decimal without losing precision by using the new
format syntax and than localize the result.
@cedk
Copy link
Contributor Author

cedk commented Mar 31, 2020

@matrixise could you review the changes you requested, thanks.

@aeros
Copy link
Contributor

aeros commented Apr 1, 2020

@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:

Your pull request may involve several commits as a result of addressing code review comments. Please keep the commit history in the pull request intact by not squashing, amending, or anything that would require a force push to GitHub. A detailed commit history allows reviewers to view the diff of one commit to another so they can easily verify whether their comments have been addressed. The commits will be squashed when the pull request is merged.

@cedk
Copy link
Contributor Author

cedk commented Apr 2, 2020

@aeros sorry, I would no more do that. Indeed I'm used to mercurial for which such workflow is well supported thanks to evolve.

@cedk
Copy link
Contributor Author

cedk commented Mar 27, 2021

@matrixise could you point me to the changes you are requesting?

@matrixise
Copy link
Member

@cedk I am sincerely sorry if I was absent, I will look at your PR in the next two weeks.

@matrixise
Copy link
Member

Hi @cedk ,

As said @aeros , you don't need to force the push, just push with your commits (we can see all the commits and discuss some points), and when we merge the PR, we will use the squash and merge feature. It's pretty useful.

I continue the review of your PR.

Thank you

@matrixise
Copy link
Member

@cedk just one thing, could you update it with the last master? Thank you so much,

@cedk
Copy link
Contributor Author

cedk commented Apr 12, 2021

@cedk just one thing, could you update it with the last master? Thank you so much,

But there is no conflict.

@matrixise matrixise merged commit e126547 into python:master Apr 12, 2021
@matrixise
Copy link
Member

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

@cedk cedk deleted the locale-format branch April 12, 2021 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants