Fix handling of Python scalars in get_incompatible_dtype for NumPy 2.…#3143
Open
LuigiGonnella wants to merge 1 commit intoactiveloopai:release/3.9.52from
Open
Conversation
|
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



NumPy 2.x (NEP 50) compatibility fix in
get_incompatible_dtype🚀 🚀 Pull Request
Impact
Description
NumPy 2.0 introduced NEP 50, which removed support for passing raw Python scalars (
int,float,bool) as the first argument tonp.can_cast. Under NumPy 2.x, callingnp.can_cast(1, np.float32)raises aTypeErrorinstead of returning a boolean.This caused
get_incompatible_dtypeindeeplake/util/casting.pyto crash whenever a Python scalar sample was validated against a tensor dtype.Steps to reproduce (NumPy 2.x):
Expected behavior: dtype compatibility check succeeds and returns
True/False.Actual behavior:
TypeError: Cannot cast scalar of type intFix:
_PYTHON_TYPE_TO_NUMPY_DTYPEdict mapping Python scalar types (float,int,bool,complex) to their canonical NumPy dtype equivalents.get_incompatible_dtypeto resolve Python scalars to a NumPy dtype first, then callnp.can_cast(from_dtype, to_dtype, casting="same_kind")— an API that is valid in both NumPy 1.x and 2.x.Things to be aware of
casting="same_kind"is used instead of the default"unsafe"to reject genuinely incompatible casts (e.g.float → int) while still allowing safe same-kind widening (e.g.float32 → float64). The downstreamintelligent_castcall handles the actual value-level conversion..dtypeattribute continue to be handled via their.dtypedirectly, unchanged in behavior.Things to worry about
_PYTHON_TYPE_TO_NUMPY_DTYPEmapping uses platform-independent dtypes (np.int64,np.float64). On 32-bit platforms or Windows, the defaultintdtype fromnp.array(0).dtypemay beint32. This is intentional — we are doing a type-level cast check, not a value-level one, so using a wider dtype is conservative and safe._get_bigger_dtypeat the top of the file still usesnp.object(deprecated in NumPy 1.20, removed in 1.24). This is a pre-existing issue and out of scope for this PR.Additional Context