Skip to content

Conversation

@jorenham
Copy link
Member

The key used to sort np.ScalarType is not unique (e.g. int64 and longlong on linux both have key ('i', '8')). To remove duplicates, set is used, whose iteration order depends on the hashes of the containing scalartype classes. So in case of ties, the hash would be the tiebreaker. The problem is that these hashes are arbitrary, leading to differences in ScalarType order between platforms, python versions, machines, etc.

In case you're wondering why I care: Stubtest is currently reporting errors for ScalarType for certain platform and python versions: https://github.com/numpy/numtype/actions/runs/17778048694/job/50530549124?pr=706. And I'm sure you can imagine that it wasn't easy to figure out what was going on here (especially if you consider the stubtest error spaghetti output, but I digress). Hence the pedantic "backport candidate" label ;)

@jorenham jorenham added the 09 - Backport-Candidate PRs tagged should be backported label Sep 16, 2025
@ngoldbaum
Copy link
Member

Can we have a test that asserts the hard-coded sorted order?

(np.bytes_, np.str_),
(np.str_, np.void),
# bouncy castles
(np.datetime64, np.timedelta64),
Copy link
Member

Choose a reason for hiding this comment

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

Total quibble but I think you can write this with just a single static list and then generate these tuples with e.g. itertools.pairwise.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to do that at first. But I then decided against that because I didn't want to set the (IMO arbitrary) "kind" group ordering in stone (bool < complexfloating < np.floating < np.signedinteger < bouncy castles < object < flexible < unsignedinteger).

@charris charris merged commit b05c823 into numpy:main Sep 23, 2025
77 checks passed
@charris
Copy link
Member

charris commented Sep 23, 2025

Thanks Joren.

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Sep 23, 2025
@jorenham jorenham deleted the ScalarType-stable-order branch September 23, 2025 21:57
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.

3 participants