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.
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
🩹 add overload for
ndarray.__matmul__
#286base: main
Are you sure you want to change the base?
🩹 add overload for
ndarray.__matmul__
#286Changes from 1 commit
cf0f0db
bec757d
3bb2d1a
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
np.
is not needed hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ScalarT
is bound tonp.generic
, so this would also accept e.g.datetime64
andstr_
, which would raise an error innp.matmul
. Specifically, it accepts these types:which that translates to
bool_ | number | object_
. Maybe there's already aTypeVar
with that bound, that could be reused here.If you feel like it; you could try to broaden the
rhs
type a bit more by exploiting the fact thatnp.bool
andbuiltins.bool
will always "promote". So for example,rhs: _Array1D[_ScalarT | bool_]
is also valid here here, and also things likeSequence[_ScalarT | py_bool | bool_]
.And just to be clear; it's already valid and type-safe. It's just that there are some easy ways to improve it, if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_ArrayLikeObject_co
is alias for_ArrayLike[np.object_]
, so it accepts only things that can be expressed using the numpynp.object_
, and rejects e.g. lists ofdecimal.Decimal
, even though that'd be valid in this case:So this would be falsely rejected.
(I kinda expected that there would be a test for this, but apparently not)
Anyway, I guess I'm trying to say that object dtypes are very difficult to properly type, especially because of the lack of tests for it. So it might be for the best to leave it as
rhs: object
for now, and look at it again once we have better testing in place.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using the
decimal.Decimal
type, an overlap issue arises between the following overloads:_MatmulScalarT
includesdecimal.Decimal
, so this overload is a valid match.decimal.Decimal
is also an instance ofobject
, this overload is also a valid match.The Conflict:
These two overloads return different types:
_MatmulScalarT
(i.e.,Decimal
).NDArray[object_]
.Example:
Because both overloads match
A @ B
, the type checker cannot determine which one to use, leading to a type error. I'm not sure about how should this overlap issue be resolved to correctly handle object without ambiguity?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no need for this overload: The the parameter types are identical, so
lhs @ rhs
will always use__matmul__
, and never__rmatmul__