-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
Handle NaN #63
Handle NaN #63
Conversation
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: |
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.
Same
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -26,6 +26,17 @@ | |||
NumberOrString: TypeAlias = "float | str" | |||
|
|||
|
|||
def format_non_finite(value: float) -> str: |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm making it private...will it be merged then?
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.
Will merge when it's ready, thank you for all your work!
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 more to add?
Thank you for the PR! |
Fixes #62 Handling math.nan in metric() method
Changes proposed in this pull request: