Skip to content

Inconsistent Type Hinting for dims Parameter in xarray Methods #8210

@andersy005

Description

@andersy005

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:

InputDim = Hashable # Single dimension
InputDims = Iterable[InputDim , ...] # multiple dimensions
InputDimOrDims = Union[InputDim, InputDims]  # Single or multiple dimensions

Then we can easily go through our xarray methods and easily replace dim and dims arguments.

Hashable could be fine in NamedArray, we haven't introduced None 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:

HashableExcludingNone = Union[int, str, tuple, ...] # How many more Hashables are there?
InputDim = HashableExcludingNone # Single dimension
InputDims = Iterable[InputDim , ...] # multiple dimensions
InputDimOrDims = Union[InputDim, InputDims]  # Single or multiple dimensions

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:

InputDim = str # Single dimension
InputDims = tuple[InputDim , ...] # multiple dimensions
InputDimOrDims = Union[InputDim, InputDims]  # Single or multiple dimensions

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions