Skip to content
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

Fix intcomma() failing with a string as input when ndigits is not None #52

Merged

Conversation

Luflosi
Copy link
Contributor

@Luflosi Luflosi commented Aug 25, 2022

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.

Changes proposed in this pull request:

  • Fix error described above
  • Add some new test cases
  • Add a new example

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-commenter
Copy link

codecov-commenter commented Aug 25, 2022

Codecov Report

Merging #52 (33005e5) into main (e0a0ef0) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main      #52   +/-   ##
=======================================
  Coverage   99.27%   99.28%           
=======================================
  Files           9        9           
  Lines         692      696    +4     
=======================================
+ Hits          687      691    +4     
  Misses          5        5           
Flag Coverage Δ
macos-latest 97.55% <100.00%> (+0.01%) ⬆️
ubuntu-latest 97.55% <100.00%> (+0.01%) ⬆️
windows-latest 96.98% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
tests/test_number.py 100.00% <ø> (ø)
src/humanize/number.py 97.08% <100.00%> (+0.08%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@hugovk hugovk added the changelog: Fixed For any bug fixes label Aug 25, 2022
@hugovk hugovk merged commit d8e2739 into python-humanize:main Aug 30, 2022
@hugovk
Copy link
Member

hugovk commented Aug 30, 2022

Thank you!

@Luflosi Luflosi deleted the fix-intcomma-with-str-and-ndigits branch August 30, 2022 13:37
@hugovk hugovk changed the title Fix intcomma() failing with a string as input when ndigits is not None Fix intcomma() failing with a string as input when ndigits is not None Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Fixed For any bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants