Skip to content

Added support for infinite and nan floats/doubles #215

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 1 commit into from
Apr 2, 2021

Conversation

badge
Copy link
Contributor

@badge badge commented Feb 23, 2021

Resolves #214.

This pull request adds support for the custom double values defined in the protobuf spec:

  • "Infinity"
  • "-Infinity"
  • "Nan"

Support is added by updates to the Message.to_dict and Message.from_dict methods, and not via json.loads, to ensure that only double and float fields will have their values casted to float("inf"), etc.

Furthermore,

  • Message.__eq__ has been updated to consider NaN values equal, otherwise a message may not be equal to itself
  • The test_message_json and test_binary_compatibility test functions have been updated to use the new dict_replace_nans function that replaces NaN float values with the string "NaN" (because assert float("nan") == float("nan") always fails).
  • Added a infinite_floats test to check correctness; this works with the protobuf package and fails in the current master branch

Warning

For the sake of clarity, json.loads(message.to_json()) is not equal to itself in the case that a message contains NaN values.

cetanu
cetanu previously approved these changes Mar 4, 2021
Copy link
Collaborator

@cetanu cetanu left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

Copy link
Collaborator

@Gobot1234 Gobot1234 left a comment

Choose a reason for hiding this comment

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

I'm still unsure about documenting utils functions as none of the others are


Parameters
----------
value : Any
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wouldn't document this. I also think value shouldn't be Any and rather str

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it can be a float too; will check and update.


Parameters
----------
value : float
Copy link
Collaborator

Choose a reason for hiding this comment

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

I again wouldn't document this


Parameters
----------
l : List
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
l : List
l: List

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not how the other functions are documented


Parameters
----------
l : Dict[Any, Any]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
l : Dict[Any, Any]
l: Dict[Any, Any]

@badge
Copy link
Contributor Author

badge commented Mar 5, 2021

I'm still unsure about documenting utils functions as none of the others are

What’s the downside? Documenting anything makes it easier for new people to contribute to the package; removing committed comments unnecessarily increases the barrier to entry.

@Gobot1234
Copy link
Collaborator

It's more to maintain

nat-n
nat-n previously approved these changes Mar 31, 2021
Copy link
Collaborator

@nat-n nat-n left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks for the contribution :)

message Test {
double positive = 1;
double negative = 2;
double nan = 3;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this makes me realise that I don't see any explicit test of regular floats anywhere!

Could we generalise this test case a little for completeness?

Suggested change
double nan = 3;
double nan = 3;
double zero = 4
double three_point_one_four = 5
double neg_three_point_one_four = 6

@nat-n nat-n mentioned this pull request Mar 31, 2021
- Added support for the custom double values from
   the protobuf json spec: "Infinity", "-Infinity", and "NaN"
- Added `infinite_floats` test data
- Updated Message.__eq__ to consider nan values
   equal
- Updated `test_message_json` and
   `test_binary_compatibility` to replace NaN float
   values in dictionaries before comparison
   (because two NaN values are not equal)
@nat-n nat-n dismissed stale reviews from cetanu and themself via 64a4668 April 2, 2021 13:03
@nat-n nat-n force-pushed the infinite-floats branch from 06bc55a to 64a4668 Compare April 2, 2021 13:03
@nat-n
Copy link
Collaborator

nat-n commented Apr 2, 2021

Rebased to resolve conflicts and enhanced the tests.

@nat-n nat-n merged commit 7c5ee47 into danielgtaylor:master Apr 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing support for infinite and nan floats/doubles
4 participants