Skip to content

API: Allow comparisons with and between any python integers #24915

Merged
ngoldbaum merged 8 commits intonumpy:mainfrom
seberg:pyint-comparisons
Oct 19, 2023
Merged

API: Allow comparisons with and between any python integers #24915
ngoldbaum merged 8 commits intonumpy:mainfrom
seberg:pyint-comparisons

Conversation

@seberg
Copy link
Copy Markdown
Member

@seberg seberg commented Oct 12, 2023

This implements comparisons between NumPy integer arrays and arbitrary valued
Python integers when weak promotion is enabled.

To achieve this:

  • I allow abstract DTypes (with small bug fixes) to register as loops (ArrayMethods).
    This is fine, you just need to take more care.
    It does muddy the waters between promotion and not a bit if the
    result DType would also be abstract.
    (For the specific case it doesn't, but in general it does.)
  • A new resolve_descriptors_raw function, which does the same job as
    resolve_descriptors but I pass it this scalar argument
    (can be expanded, but starting small).
    • This only happens when available, so there are some niche paths were this cannot
      be used (ufunc.at and the explicit resolution function right now),
      we can deal with those by keeping the previous rules (things will just raise
      trying to convert).
    • The function also gets the actual arrays.dtype instances while I normally ensure that
      we pass in dtypes already cast to the correct DType class.
      (The reason is that we don't define how to cast the abstract DTypes as of now,
      and even if we did, it would not be what we need unless the dtype instance actually had
      the value information.)
  • There are new loops added (for combinations!), which:
    • Use the new resolve_descriptors_raw (a single function dealing with everything)
    • Return the current legacy loop when that makes sense.
    • Return an always true/false loop when that makes sense.
    • To achieve this, they employ a hack/trick: get_loop() needs to know the value,
      but only resolve_descriptors_raw() does right now, so this is encoded on whether we use
      the np.dtype("object") singleton or a fresh instance!
      (Yes, probably ugly, but avoids channeling things to more places.)

Additionally, there is a promoter to say that Python integer comparisons can just use
object dtype (in theory weird if the input then wasn't a Python int,
but that is breaking promises).


This is the split out from gh-23912, to solve the biggest pain point/problem. Comparisons with Python integers that are out-of-bounds.

May need a bit of cleanup, but the main discussion should be if the appraoch is really what we want (but we really need something).

Loading
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