Conversation
carterbox
left a comment
There was a problem hiding this comment.
IN addition to comment below, docstring needs to be updated to specify what happens with nonfinite numbers. I believe floats can also represent inf, so it would be great if you used math.isfinite() to handle both inf and nan.
Codecov Report
@@ Coverage Diff @@
## main #63 +/- ##
==========================================
- Coverage 99.29% 98.36% -0.93%
==========================================
Files 9 9
Lines 710 735 +25
==========================================
+ Hits 705 723 +18
- Misses 5 12 +7
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. |
|
Please could you also add tests? The inputs and expected outputs can go in this list: Lines 199 to 229 in c35fa9c |
for more information, see https://pre-commit.ci
|
Is it ok now or should I add more tests? |
|
@carterbox @bersbersbers Thanks for your reviews, how's it look now? |
|
Looks good for |
|
Thanks for your feedback! |
I'd prefer pushing to this PR, but either way is fine :) |
for more information, see https://pre-commit.ci
|
I have just raised a |
|
When you raise a My rule thumb is, if you wonder if a test is necessary, a test is necessary. |
|
Okay then adding the check for |
|
I wonder if it might make sense to have a |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
src/humanize/number.py
Outdated
| return "NaN" | ||
| if math.isinf(value) and value < 0: | ||
| return "-Inf" | ||
| elif math.isinf(value) and value > 0: |
There was a problem hiding this comment.
You may be interested in https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/no-else-return.html :)
src/humanize/number.py
Outdated
| return "-Inf" | ||
| elif math.isinf(value) and value > 0: | ||
| return "+Inf" | ||
| else: |
src/humanize/number.py
Outdated
| def format_non_finite(value: float) -> str: | ||
| """Utility function to handle infinite and nan cases.""" | ||
| if math.isnan(value): | ||
| return "NaN" | ||
| if math.isinf(value) and value < 0: | ||
| return "-Inf" | ||
| elif math.isinf(value) and value > 0: | ||
| return "+Inf" | ||
| return "" |
There was a problem hiding this comment.
This function seems overly complicated and not necessary to me. Why not just use the built-in string conversions? "nan", "-inf", and "inf"?
if not math.isfinite(float(value)):
return str(float(value))There was a problem hiding this comment.
My argument for NaN was the capitalization in https://en.wikipedia.org/wiki/NaN, which I also prefer. But I don't have a strong opinion on that.
src/humanize/number.py
Outdated
| NumberOrString: TypeAlias = "float | str" | ||
|
|
||
|
|
||
| def format_non_finite(value: float) -> str: |
There was a problem hiding this comment.
Let's have this as a "private" function:
| def format_non_finite(value: float) -> str: | |
| def _format_non_finite(value: float) -> str: |
There was a problem hiding this comment.
I'm making it private...will it be merged then?
There was a problem hiding this comment.
Will merge when it's ready, thank you for all your work!
There was a problem hiding this comment.
What more to add?
|
Thank you for the PR! |
Fixes #62 Handling math.nan in metric() method
Changes proposed in this pull request: