Uh oh!
There was an error while loading. Please reload this page.
Change isinstance checks to duck Dask Array checks #4208 - #4221
Conversation
jthielen
left a comment
There was a problem hiding this comment.
Thank you for going through and finding/adjusting all of these Dask checks!
I've left some comments below, which, other than the one about is_duck_dask_array still using isinstance instead of is_dask_collection, are mostly there to help get discussion going on a few points.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
jthielen
commented
Jul 14, 2020
Also, a broader discussion that's I've seen hinted to in the past, but is brought to the forefront by this PR: how should xarray be checking for general duck array types it can wrap? Right now, it looks like there is a mix of is_array_like(data)and hasattr(data, "__array_function__") orisinstance(data, dask_array_type)and it would be nice to bring consistency. However, both these checks seem like they'd let too many types through. For example, |
keewis
commented
Jul 14, 2020
Revisiting it I now realize that |
As long as that doesn't break any of the current uses, I think that would be the best way forwards. This would require xarray to be on NumPy 1.16+ (in order to ensure is_duck_array(x) andnotisinstance(x, (DataArray, Variable))with Although, while we're at it, do we also need more careful handling of upcast types? I'm not sure if there even are any out there right now (not sure if a HoloViews Dataset counts here), but that doesn't necessarily mean there never will be any. |
rpmanser
commented
Jul 15, 2020
I'm getting a bit lost within the testing suite while trying to figure out the implications of this change and what kinds of tests are necessary for it. Would a test module for this
Any suggestions would be greatly appreciated. |
jthielen
commented
Jul 17, 2020
@rpmanser For what it's worth, I'd think doing tests like |
dcherian
commented
Jul 17, 2020
Thanks for working on this @rpmanser. Since you've changed every
Seems like this is covered by We could add specific pint-as-dask tests to This could be a discussion topic for our next dev meeting on wednesday. |
rpmanser
commented
Jul 20, 2020
I have erred on the side of leaving the On another note, if it is appropriate to apply these changes:
I can do that as well. If not, should a new issue be opened for this? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
* Rename `is_array_like` to `is_duck_array` * `is_duck_array` checks for `__array_function__` and `__array_ufunc__` in addition to previous checks * Replace checks for `is_duck_dask_array` and `__array_function__` with `is_duck_array`
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
dcherian
left a comment
There was a problem hiding this comment.
I took a second pass and found one issue.
LGTM otherwise.
Uh oh!
There was an error while loading. Please reload this page.
shoyer
commented
Aug 27, 2020
I would suggest adding an explicit check for NumPy arrays. Otherwise we would need to bump our minimum supported NumPy version. |
keewis
commented
Aug 27, 2020
we went with xarray/xarray/core/variable.py Line 938 in edd5c1e xarray/xarray/core/formatting.py Line 554 in 3337b6f defis_duck_array_or_ndarray(array):
returnis_duck_array(array) or (notIS_NEP18_ACTIVEandisinstance(data, np.ndarray)) |
Uh oh!
There was an error while loading. Please reload this page.
* Add explicit check for NumPy array to is_duck_array * Replace is_duck_array_or_ndarray checks with is_duck_array
rpmanser
commented
Aug 28, 2020
I meant |
keewis
commented
Aug 28, 2020
you mean the commit message? If so, you can use |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: keewis <keewis@users.noreply.github.com>
keewis
left a comment
There was a problem hiding this comment.
I have two comments, otherwise looks good to me.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| if isinstance(padded.data, dask_array_type): | ||
| if is_duck_dask_array(padded.data): | ||
| raise AssertionError("should not be reachable") |
There was a problem hiding this comment.
@pydata/xarray: why is that here? If it is never reachable, it should be fine to just not do the branching (and if it is necessary, it should be fine to use assert not is_duck_dask_array(padded.data), "should not be reachable").
Also, if we raise the code below will never be reached, so we should be able to remove it.
There was a problem hiding this comment.
I think it's from #3040 . We would like to use this code path but there are bugs IIRC
There was a problem hiding this comment.
then I guess we should raise a NotImplementedError or a ValueError instead of a AssertionError and improve the error message?
There was a problem hiding this comment.
We could comment it out and add a TODO note. The if condition in Line 420 makes sure that this is never reached.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
rpmanser
commented
Aug 31, 2020
No worries, thanks for the reviews! |
dcherian
commented
Sep 2, 2020
Things seem to have died down here so let's test it out. Thanks @rpmanser This is an amazing first PR. Thanks for your efforts and welcome to xarray! |
rpmanser
commented
Sep 2, 2020
@dcherian No problem! Thanks for all your help! |
isort -rc . && black . && mypy . && flake8I added the discussed
is_duck_dask_array(x)function and replacedisinstance(x, dask_array_type)checks withis_duck_dask_array(x). Existing tests are passing. I did not changeisinstance(x, dask_array_type)checks in the testing modules since I am unsure if that is appropriate.As for additional tests, I am leaning towards testing Xarray ( Pint ( Dask ) ) ) objects as implemented in upcoming Pint v0.15 rather than a mock class, but I am open to discussion. I am unfamiliar with the release schedules of Pint and Xarray but I imagine that would influence which direction the testing should go.