Reject non-integral float-like values (numpy floats) for integer parameters - #146
Merged
jhonabreul merged 2 commits intoAug 12, 2026
Merged
Conversation
The non-integral rejection added in fb5cce6 only covered exact Python floats: Runtime.PyFloat_Check compares the type pointer, so float subclasses such as numpy.float64 and __float__-only numbers such as numpy.float32 bypassed the guard in Converter.ToPrimitive and fell through to PyNumber_Long/__int__, silently truncating the value (e.g. SimpleMovingAverage(np.float64(20.5)) built a period-20 indicator). Extend the guard to any float-like value: Python floats including subclasses, and numbers that define __float__ but no __index__. True integer types advertising __index__ (numpy.int64/int32) and plain ints are unaffected, and integral-valued floats (20.0) keep converting. Adds embed tests with float-subclass / __float__-only / __index__ fixtures and a numpy-backed python test over ConversionTest fields and method binding.
jhonabreul
marked this pull request as ready for review
August 11, 2026 17:53
Martin-Molinero
approved these changes
Aug 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this implement/fix? Explain your changes.
Passing a non-integral numpy float where a .NET integer parameter is expected silently truncates instead of raising:
SimpleMovingAverage(np.float64(20.5))builds a period-20 indicator, while plainfloat(20.5)is correctly rejected with aTypeError.fb5cce6 (#128) added the non-integral rejection for Python floats, but the guard in
Converter.ToPrimitiveusesRuntime.PyFloat_Check, an exact type-pointer check. Two kinds of values bypass it and fall through toPyNumber_Long/__int__, which truncates:floatsubclasses —numpy.float64is a subclass of Pythonfloat__float__—numpy.float32/float16are notfloatsubclassesThis PR extends the guard to any float-like value:
floatincluding subclasses (PyObject_TypeCheckagainstPyFloatType), or__float__but not__index__(Python's marker for losslessly-int-convertible types)Integral-valued floats (
np.float64(20.0)) keep converting, numpy integer scalars (np.int64,np.int32, which advertise__index__) are unaffected, and plain ints take a fast early exit. If a probed__float__call fails, the pending Python error is cleared before rejecting, so no stale error leaks.Behavior note: non-numpy numeric types with
__float__/__int__but no__index__(e.g.decimal.Decimal,fractions.Fraction) previously also truncated silently into integer parameters (Decimal("20.5")→20); with this change their non-integral values are rejected the same way, while integral values keep converting exactly as before.Does this close any currently open issues?
No open issue; found while reproducing the fleet error-surface study (Agents#305, improvement 1).
Any other comments?
Tests:
TestFloatToIntConversionembed tests: new float-subclass,__float__-only and__index__fixture cases over single and overloaded targets (22 pass).tests/test_conversion.py::test_numpy_float_to_int_conversion: numpy-backed coverage (np.float64/np.float32integral and non-integral,np.int32/np.int64, plain-float regression, method binding), skipped automatically when numpy is absent.test_module.py::test_explicit_assembly_load) is a local-environment artifact and fails identically on cleanmaster.Checklist
Check all those that are applicable and complete.
AUTHORSCHANGELOG