Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Check for integer overflows when creating scalar form python int #16140

Merged
merged 5 commits into from
Jul 15, 2024

Conversation

seberg
Copy link
Contributor

@seberg seberg commented Jul 1, 2024

This aligns with NumPy, which deprecated this since a while and raises an error now on NumPy 2, for example for Scalar(-1, dtype=np.uint8).

Since it aligns with NumPy, the DeprecationWarning of earlier NumPy versions is inherited for those.

This (or similar handling) is required to be compatible with NumPy 2/pandas, since the default needs to be to reject operation when values are out of bounds for e.g. uint8_series + 1000, the 1000 should not be silently cast to a uint8.


Split from gh-15897

xref: rapidsai/build-planning#38

@seberg seberg requested a review from a team as a code owner July 1, 2024 10:44
Copy link

copy-pr-bot bot commented Jul 1, 2024

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions bot added the Python Affects Python cuDF API. label Jul 1, 2024
@seberg seberg changed the title Scalar strict convert API: Check for integer overflows when creating scalar form python int Jul 1, 2024
@galipremsagar galipremsagar added improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels Jul 1, 2024
@galipremsagar
Copy link
Contributor

/okay to test

1 similar comment
@mroeschke
Copy link
Contributor

/okay to test

@mroeschke
Copy link
Contributor

/okay to test

1 similar comment
@galipremsagar
Copy link
Contributor

/okay to test

def test_scalar_out_of_bounds_pyint_fails(value, dtype):
# Test that we align with NumPy on scalar creation behavior from
# Python integers.
np_ver = np.lib.NumpyVersion(np.__version__)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use packaging.version.parse here if you don't want to dip into np.lib

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed also here now, thanks. (And rebased.)

@@ -47,7 +47,9 @@
)


_DEFAULT_CATEGORICAL_VALUE = -1
# Using np.int8(-1) to allow silent wrap-around when casting to uint
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm guessing we should disallow this cast if this happens, but not for this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for not following up. NumPy allows np.int8(-1) to be cast to silently to other integers pretty easily including unsigned, which is what we rely on here.

If NumPy wants to change that, the _DEFAULT_CATEGORICAL_VALUE might have to be some mapping/function based on the actual dtype used, but for now this works.

Copy link
Contributor

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

An optional fix https://github.com/rapidsai/cudf/pull/16140/files#r1671415931 but overall looks good

This aligns with NumPy, which deprecated this since a while and
raises an error now, for example for `Scalar(-1, dtype=np.uint8)`.

Necessary to ensure we give the right errors when promotion doesn't
up-cast based on values.
@seberg seberg force-pushed the scalar-strict-convert branch 2 times, most recently from 7b569a1 to 39d240f Compare July 12, 2024 17:20
@mroeschke
Copy link
Contributor

/okay to test

@seberg
Copy link
Contributor Author

seberg commented Jul 15, 2024

Friendly ping, can we put these in soon? Or is there some release note update/dicussion needed?

@mroeschke
Copy link
Contributor

/merge

@rapids-bot rapids-bot bot merged commit 128f0c9 into rapidsai:branch-24.08 Jul 15, 2024
79 checks passed
@seberg seberg deleted the scalar-strict-convert branch July 15, 2024 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improvement / enhancement to an existing function non-breaking Non-breaking change Python Affects Python cuDF API.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants