fix(ndarray): implement __subclasscheck__ for ABC protocol compliance#65
Open
somnio-kimm wants to merge 2 commits intop2p-ld:mainfrom
Open
fix(ndarray): implement __subclasscheck__ for ABC protocol compliance#65somnio-kimm wants to merge 2 commits intop2p-ld:mainfrom
ndarray): implement __subclasscheck__ for ABC protocol compliance#65somnio-kimm wants to merge 2 commits intop2p-ld:mainfrom
Conversation
- 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: |
Collaborator
There was a problem hiding this comment.
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) | ||
| ) |
Collaborator
There was a problem hiding this comment.
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?
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.
Summary
This PR improves
NDArrayMetato comply with Python’s ABC protocol and prevent errors during subclass checks.Changes
__subclasscheck__to safely handleissubclass()calls with non-NDArrayMetatypes.shapeanddtypeconstraints.Related Issue
Closes #66
Notes
NPTypingErrorwhenissubclass()is used with unrelated types.Test
issubclass()no longer raises errors for non-NDArraytypes.