-
Notifications
You must be signed in to change notification settings - Fork 26
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
Change ArrayLike type to use read only properties #289
Conversation
Codecov Report
@@ Coverage Diff @@
## main #289 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 37 37
Lines 2782 2785 +3
=========================================
+ Hits 2782 2785 +3
|
LGTM - I tested your fix with the provided example and confirmed your results. I agree this is the correct way to represent read-only attributes in a Protocol. |
is there a reason we aren't just using |
I'd prefer that too. Looks like there's some argument against it in napari's Happy to change to use that, but might need some more context here. |
I'm not too familiar with the history or usage here, but poking around I see that Take this script for example - both functions return data that fail with import numpy as np
from napari.viewer import Viewer
from npe2.types import ArrayLike
def foo() -> ArrayLike:
return [[1, 2, 3], [4, 5, 6]]
def bar() -> np.typing.ArrayLike:
return [[1, 2, 3], [4, 5, 6]]
viewer = Viewer()
try:
viewer.add_image(foo())
except Exception as e:
print("failed to add image with `foo()`")
print(e)
try:
viewer.add_image(bar())
except Exception as e:
print("failed to add image with `bar()`")
print(e)
|
Yeah arraylike is probably too lose. It's anything that can be cast to an array (including scalars, etc)... here, we're just trying to indicate the protocol of an array object, without restricting to numpy arrays. Andy your solution is great. There are ways to test this (https://pypi.org/project/pytest-mypy-plugins/), and I have used it in other projects... but it's a pain, and adds significant maintenance burden to keep working. So let's not bother |
(In other words, if you want to say something must have a shape and a dtype, then numpy arraylike won't work unless you always call np.asarray inside the body of the function... we're not implying that here with this object) |
Description
The
np.ndarray
type does not seem to satisfy theArrayLike
type. This PR fixes that by using read-only properties instead of plain settable attributes.Here's a simple reproducer for mypy.
For me, mypy fails before this change, but passes afterwards. Let me know if there's a way to add some form of this to automated tests/CI.
Context
I encountered this while writing a plugin where I'm aiming for full(ish) typing coverage. I understand the
ArrayLike
type has other problems with respect to some napari layer data types (which have their own problems), but it seems like it should at least be satisfied bynp.ndarray
. This is also more consistent with napari's layer data protocol.