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 BaseFloat validation to match Float validation #1595

Merged
merged 3 commits into from
Nov 15, 2021

Conversation

mdickinson
Copy link
Member

The validations of the Float and BaseFloat trait types previously didn't match: in particular, for Python >= 3.8, Float accepts objects whose type is equipped an __index__ method, while BaseFloat does not. That was partly to do with the difficulty of expressing the condition to be accepted as a float in pure Python.

This PR sidesteps that difficulty by exposing the Float validation check to Python via a new private function traits.ctrait._number_to_float.

See also #1594, which makes essentially the same change to ensure that Complex and BaseComplex match. These two PRs will cause merge conflicts with each other, but they shouldn't be too difficult to resolve.

Checklist

  • Tests

@mdickinson

This comment has been minimized.

mdickinson added a commit that referenced this pull request Nov 8, 2021
"""
if type(value) is float: # fast path for common case
return value
try:
Copy link
Member Author

Choose a reason for hiding this comment

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

This is where the bug is: this code misses the chance to convert objects that have an __index__ method.

traits/tests/test_float.py Outdated Show resolved Hide resolved
Copy link
Contributor

@rahulporuri rahulporuri left a comment

Choose a reason for hiding this comment

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

LGTM with a question and a comment.

traits/tests/test_float.py Show resolved Hide resolved
@@ -3339,7 +3339,7 @@ validate_trait_integer(
*/

static PyObject *
as_float(PyObject *value)
validate_float(PyObject *value)
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, the internals of validate_float e.g. PyFloat_AsDouble and others handle __index__ correctly, which is why this is the right approach - which is also what you meant by

difficulty of expressing the condition to be accepted as a float in pure Python

Is that correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes! Moreover, the Python behaviour changes from version to version, and we want to match Python's behaviour regardless of Python version. The easiest way to do that is to use Python directly rather than trying to encode Python's logic in our own code (which would then mean having to update that code whenever Python's behaviour changes).

Related: https://bugs.python.org/issue40801

@rahulporuri
Copy link
Contributor

Still LGTM

@mdickinson mdickinson merged commit 9fd358b into main Nov 15, 2021
@mdickinson mdickinson deleted the fix/base-float-validation branch November 15, 2021 10:35
mdickinson added a commit that referenced this pull request Nov 15, 2021
* Do duck-typed Complex validation

* Fix skip condition

* Rename for consistency with #1595
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.

None yet

2 participants