Skip to content

Conversation

@ngoldbaum
Copy link
Member

@ngoldbaum ngoldbaum commented Jan 5, 2026

Fixes #30578

See https://cython.readthedocs.io/en/latest/src/userguide/fusedtypes.html#type-checking-specializations if you're unfamiliar with the fused types specialization syntax I'm using.

@ngoldbaum ngoldbaum added the 09 - Backport-Candidate PRs tagged should be backported label Jan 5, 2026
@ngoldbaum ngoldbaum added this to the 2.4.1 Release milestone Jan 5, 2026
@ngoldbaum ngoldbaum marked this pull request as ready for review January 5, 2026 20:55
@charris
Copy link
Member

charris commented Jan 5, 2026

I'm a bit confused, as the documentation states that the fused type with greatest precision is used. Which of double and int64 is the type with greatest precision? We want double, but the warning says int64.

@ngoldbaum
Copy link
Member Author

ngoldbaum commented Jan 5, 2026

I'm a bit confused, as the documentation states that the fused type with greatest precision is used.

What documentation are you referring to?

Fused types allow you to write code that can be called with any one of an enumerated set of types. Precision doesn't come into place in my use here.

In this PR, call sites that pass in ints will use the int version and call sites that use doubles will call the double version. There are two different functions for each case in the generated C code.

Maybe the docs you're reading are talking about a fused type like cython.integral that can be any of the integer types? In that case precision does matter.

@charris
Copy link
Member

charris commented Jan 5, 2026

My impression was that what is wanted is a cast of int64 to double, with maybe a check that the value is preserved.

@ngoldbaum
Copy link
Member Author

ngoldbaum commented Jan 5, 2026

I’m implementing @rkern’s suggestion from here.

But I think it could be worth robustifying both, using np.arange() in the first instance and adding an integer check_constraint() variant in the second.

This is exactly that suggestion. I could have simply casted to double, but that could overflow and IMO this is better.

Copy link
Member

@rkern rkern left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you!

@seberg
Copy link
Member

seberg commented Jan 6, 2026

LGTM also, thanks all!

@seberg seberg merged commit 7f80486 into numpy:main Jan 6, 2026
78 of 81 checks passed
charris pushed a commit to charris/numpy that referenced this pull request Jan 6, 2026
…#30580)

This avoids type unsafe function calls in cython random.
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 6, 2026
charris added a commit that referenced this pull request Jan 7, 2026
MAINT: avoid passing ints to random functions that take double (#30580)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: type-unsafe Cython C functions calls in RNG interface

4 participants