Skip to content

fix(ndarray): implement __subclasscheck__ for ABC protocol compliance#65

Open
somnio-kimm wants to merge 2 commits intop2p-ld:mainfrom
somnio-kimm:fix/abc-protocol-compliance
Open

fix(ndarray): implement __subclasscheck__ for ABC protocol compliance#65
somnio-kimm wants to merge 2 commits intop2p-ld:mainfrom
somnio-kimm:fix/abc-protocol-compliance

Conversation

@somnio-kimm
Copy link
Copy Markdown

@somnio-kimm somnio-kimm commented Apr 6, 2026

Summary

This PR improves NDArrayMeta to comply with Python’s ABC protocol and prevent errors during subclass checks.

Changes

  • Implement __subclasscheck__ to safely handle issubclass() calls with non-NDArrayMeta types.
  • Add structural subclass comparison based on shape and dtype constraints.
  • Ensure compatibility with external libraries that rely on ABC registration and subclass checks.

Related Issue

Closes #66

Notes

  • This change avoids NPTypingError when issubclass() is used with unrelated types.

Test

  • Verified that issubclass() no longer raises errors for non-NDArray types.

- Prevent `NPTypingError` when `issubclass()` is called with non-NDArray types.
- Ensure compatibility with external libraries (e.g., `autobahn`) that perform internal ABC registration and subclass checks.
- Add structural subclass checking for parameterized `NDArray` types by comparing shape and dtype constraints.
)
)

def __subclasscheck__(cls, subclass: Any) -> bool:
Copy link
Copy Markdown
Collaborator

@sneakers-the-rat sneakers-the-rat Apr 6, 2026

Choose a reason for hiding this comment

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

no need to add it here, this vendored subpackage is destined for removal and shouldn't gain any new code.

Comment on lines +109 to +114
shape_match = cls_shape is Any or (
sub_shape is not Any and issubclass(sub_shape, cls_shape)
)
dtype_match = cls_dtype is Any or (
sub_dtype is not Any and issubclass(sub_dtype, cls_dtype)
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this isn't really how we check for shape/type, as mentioned in the issue i think i'll need to see the problem and how this is intended to be used in order to know how it should work - are we just trying to avoid the subclass error from the nptyping.Final metaclass?

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.

fix(ndarray): add ABC-compliant subclass checking for NDArrayMeta

2 participants