Skip to content

fix: implement is_exact_type_of to check dtype and shape for PyArray#550

Open
asjad2401 wants to merge 1 commit into
PyO3:mainfrom
asjad2401:fix/is-exact-type-of-dtype-check
Open

fix: implement is_exact_type_of to check dtype and shape for PyArray#550
asjad2401 wants to merge 1 commit into
PyO3:mainfrom
asjad2401:fix/is-exact-type-of-dtype-check

Conversation

@asjad2401
Copy link
Copy Markdown

Problem

PyTypeInfo::is_exact_type_of was not overridden for PyArray<T, D>, so the
default PyO3 implementation (a plain Python type object pointer comparison) was
used. This caused cast_exact and downcast_exact to ignore element dtype and
array dimensionality — a float32 array would silently pass a cast_exact::<PyArray2<f64>>() check.

Reported in #549, also related to #547.

Fix

Override is_exact_type_of with the same implementation as is_type_of, which
calls Self::extract internally. This checks:

  1. That the object is a NumPy array
  2. That the dimensionality matches D
  3. That the element dtype matches T

Test

Added cast_exact_checks_dtype_and_shape to tests/array.rs which verifies:

  • cast_exact succeeds when both dtype and shape match
  • cast_exact fails when dtype does not match
  • cast_exact fails when dimensionality does not match

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant