Skip to content
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

Align typing of dimension inputs #7094

Open
3 tasks
headtr1ck opened this issue Sep 27, 2022 · 5 comments
Open
3 tasks

Align typing of dimension inputs #7094

headtr1ck opened this issue Sep 27, 2022 · 5 comments

Comments

@headtr1ck
Copy link
Collaborator

headtr1ck commented Sep 27, 2022

What is your issue?

Currently the input type for "one or more dims" is changing from function to function.
There are some open PRs that move to str | Iterable[Hashable] which allows the use of tuples as dimensions.

Some changes are still required:

@headtr1ck headtr1ck added needs triage Issue that has not been reviewed by xarray team member enhancement needs discussion topic-typing and removed needs triage Issue that has not been reviewed by xarray team member labels Sep 27, 2022
@derhintze
Copy link

derhintze commented Oct 6, 2022

Iterables (implement __iter__) not only aren't ordered, but also neither have a length (implement __len__ nor allow for membership tests (implement __contains__). So in these cases one needs to use Sequences, too; in addition to cases where order matters.

See https://docs.python.org/3.9/library/collections.abc.html#collections-abstract-base-classes

@headtr1ck
Copy link
Collaborator Author

headtr1ck commented Oct 9, 2022

Yes, sequence sounds right if the order matters.
Usually we do not use len(input) or x in input though. Maybe reversible container or something else exists?

A bit unfortunate that np.ndarray is not a sequence though, but I think using them for dimensions of not a very common use case.

@max-sixty
Copy link
Collaborator

Usually we do not use len(input) or x in input though. Maybe reversible container or something else exists?

We can build a list if we need a len? Or IIRC there is a Sized type — a Set but not an Iterable.

@headtr1ck
Copy link
Collaborator Author

headtr1ck commented Oct 13, 2022

We can build a list if we need a len? Or IIRC there is a Sized type — a Set but not an Iterable.

We already do that.

We have the following solutions:

  1. We allow any Iterable and it is up to the user to ensure correct ordering (mypy does not complain when a set is supplied)
  2. We require a Sequence which ensures ordering (user cannot supply a np.ndarray e.g.)
  3. We make the type more complicated to support ArrayLike as well.

@max-sixty
Copy link
Collaborator

Ah excellent, thanks @headtr1ck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants