Fix intcomma() failing with a string as input when ndigits is not None#52
Merged
hugovk merged 1 commit intopython-humanize:mainfrom Aug 30, 2022
Merged
Conversation
Calling `humanize.intcomma("1", 0)` would fail with the error message
```
ValueError: Unknown format code 'f' for object of type 'str'
```
Fix this by first converting the string to a `float` or `int`. Always converting the string to a `float` would not work for cases like `humanize.intcomma("1")`, as this would output `'1.0'` instead of the desired output `'1'`.
This also requires using a while loop instead of recursion, as calling this function again would now remove the thousands separator, leading to an infinite recursion. This also makes it easier to reason about IMO.
Also add some new test cases covering this and a new example.
Codecov Report
@@ Coverage Diff @@
## main #52 +/- ##
=======================================
Coverage 99.27% 99.28%
=======================================
Files 9 9
Lines 692 696 +4
=======================================
+ Hits 687 691 +4
Misses 5 5
Flags with carried forward coverage won't be shown. Click here to find out more.
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Member
|
Thank you! |
intcomma() failing with a string as input when ndigits is not None
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Calling
humanize.intcomma("1", 0)would fail with the error messageFix this by first converting the string to a
floatorint. Always converting the string to afloatwould not work for cases likehumanize.intcomma("1"), as this would output'1.0'instead of the desired output'1'.This also requires using a while loop instead of recursion, as calling this function again would now remove the thousands separator, leading to an infinite recursion. This also makes it easier to reason about IMO.
Changes proposed in this pull request: