Skip to content

Conversation

@rubenvdg
Copy link
Contributor

I believe this resolves #6230.

We could also check for nans in the literal(.) factory, but imo that doesn't add anything really.

Copy link
Contributor

@Fokko Fokko left a comment

Choose a reason for hiding this comment

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

Two minor comments, apart from that, this looks great! Thanks, @rubenvdg for working on this 👏🏻

def __init__(self, value: L, value_type: Type[L]):
if value is None or not isinstance(value, value_type):
raise TypeError(f"Invalid literal value: {value!r} (not a {value_type})")
if isinstance(value, float):
Copy link
Contributor

Choose a reason for hiding this comment

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

Python has lazy evaluation, so we can combine these in the same line 👍🏻

Suggested change
if isinstance(value, float):
if isinstance(value, float) and isnan(value):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed! I never realized that -- good to know.

assert "Invalid literal value: None" in str(e.value)


@pytest.mark.parametrize("literal_class", [FloatLiteral, DoubleLiteral])
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add one more tests where we use literal(float("NaN"))? The literal is meant as the public API, and people shouldn't necessarily initialize the specific literal instances themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@Fokko Fokko merged commit a237cd5 into apache:master Dec 20, 2022
@Fokko
Copy link
Contributor

Fokko commented Dec 20, 2022

Thanks @rubenvdg for fixing this 👏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Disallow NaN values in Literals

2 participants