-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Inconsistent Type Hinting for dims Parameter in xarray Methods #8210
Comments
I think you can tell Mypy that None is not allowed by overloading the function and use the But we do not want to do that for every method that accepts dimension arguments... I assume that the most common non-string types are enums and composite types (e.g. |
Can I ask — is the extent of the problem that Or that we can't discriminate between supplying dims and not supplying dims, because |
I think currently it is totally valid and possible to use None as a dimension or variable name (since None is hashable). However, you will have a hard time using this Variable/DataArray/Dataset since every method with a "dim" argument uses None as default with a special meaning, so you can never do your operations along the None-dimension. |
Based on @headtr1ck's comment, here is one example: In [9]: c = xr.DataArray([[1,2], [0, 0]]).assign_coords(coords={"dim_0": [3,4]}).rename({"dim_0": None})
In [10]: c
Out[10]:
<xarray.DataArray (None: 2, dim_1: 2)>
array([[1, 2],
[0, 0]])
Coordinates:
* None (None) int64 3 4
Dimensions without coordinates: dim_1
In [11]: c.mean(dim=None) # incorrect: i don't want to collapse all dimensions just `None`
Out[11]:
<xarray.DataArray ()>
array(0.75) |
Rephrasing my question slightly — does this have any impact for those who don't use Or is the issue "mypy says you can use ...which arguably isn't too bad — it does seem like a corner case, and there's lots & lots of things mypy doesn't pick up on? |
No. At least I cannot think of any realistic problem (maybe accidentally renaming something to
When the return type is different one can always overload it and Mypy will know what to do with the overlapping (we do that already occasionally).
With the current definition this is somewhat true. I think this is a minor problem. Probably very few people ever tried this. Theoretically you can use None as e.g. a dimension name and then even use some methods by supplying
I also don't find it too bad, especially since we always specify I think this issue came up when thinking about the following change: Allow While definitely doable, I am not in favor of such an approach because |
Is this close-able? My takeaway is "if you use (but there's a very large space of things that won't work, and almost as large a space of things mypy won't catch) |
None
is not really practical in current xarray so not allowing it as a dimension is probably the easiest path, but type hinting will not be correct.I want
dims
to have a type hint that is consistent, easy to read and understand. In a dream world it would look something like this:Then we can easily go through our xarray methods and easily replace
dim
anddims
arguments.Hashable
could be fine inNamedArray
, we haven't introducedNone
as a typical default value there yet.But it wouldn't be easy in xarray because we use
None
as default value a lot, which will (I suspect) lead to a bunch of refactoring and deprecations. I haven't tried it maybe it's doable?Another idea is to try and make a HashableExcludingNone:
I suspect this is harder than it seems.
Another idea is drop the idea of Hashable and just allow a few common ones that are used:
Very clear! I think a few users (and maintainers) will be sad because of the lack of flexibility though.
No easy paths, and trying to be backwards compatible is very demotivating.
Originally posted by @Illviljan in #8075 (comment)
The text was updated successfully, but these errors were encountered: