Skip to content

BUG: Supply missing self-casts for legacy-backed DTypes - #33050

Open
ngoldbaum wants to merge 2 commits into
numpy:mainfrom
ngoldbaum:fix-rational2
Open

ngoldbaum wants to merge 2 commits into
numpy:mainfrom
ngoldbaum:fix-rational2

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Generate missing self-casts for DTypes using NPY_DT_legacy_descriptor_proto from their legacy copyswap implementations. This fixes a segfault triggerable by comparing a rational2 instance with the byteswapped version, found in review of #33019. I ended up net negative on lines of code because it replaces rational2’s custom self-cast.

I think that means eventually ml_dtypes might be able to use the automatically defined self cast (ping @YukaSadaoka). In principle I think we could also backport it with a shim in npy_2_compat.h in a followup.

Also fixes legacy user dtypes incorrectly allowing byte-order changes with casting="no", adds regression coverage for both rational dtypes, and clarifies dtype(copy=True) behavior (as requested by @seberg in #33019).

AI Disclosure

An AI model found the segfault and I iterated with it on the PR.

@ngoldbaum ngoldbaum added 00 - Bug component: numpy._core 63 - C API Changes or additions to the C API. Mailing list should usually be notified. labels Oct 15, 2026
@seberg

seberg commented Oct 15, 2026

Copy link
Copy Markdown
Member

Hmmm thanks. My quick thought is that I am a bit unsure. I omitted it initially intentionally, because:

  • I want users to start using the new casting paths (we should get rid of the old ones and we can start actually deprecating them
  • Backporting this is annoying and without it is useful in 3+ years

OTOH, yeah, unless we are unsure about copyswapn this is safe, so maybe we should just do that even if it adds a relatively small convenience.
(Unrelated, I guess resolve_dtypes could default to a no-op one for the same-dtype casts.)

@ngoldbaum

Copy link
Copy Markdown
Member Author

I'm happy to leave the legacy stuff alone too. I just thought the version that net deletes code is better. I'll let you think about it, no rush.

@seberg

seberg commented Oct 15, 2026

Copy link
Copy Markdown
Member

Well, it net deletes code by adding API promises :).

This branch has not been deployed

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

Labels

00 - Bug 63 - C API Changes or additions to the C API. Mailing list should usually be notified. component: numpy._core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants