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

Fix & normalize typing for chunks #8247

Merged
merged 3 commits into from
Sep 28, 2023
Merged

Conversation

max-sixty
Copy link
Collaborator

I noticed that "auto" wasn't allowed as a value in a dict. So this normalizes all chunk types, and defines the mapping as containing the inner type.

Allows removing some ignores (though also adds one).

One question — not necessary to answer now — is whether we should allow a tuple of definitions, for each dimension. Generally we use names, which helps prevent mistakes, and allows us to be less concerned about dimension ordering.

I noticed that `"auto"` wasn't allowed as a value in a dict. So this normalizes all chunk types, and defines the mapping as containing the inner type.

Allows removing some ignores (though also adds one).

One question — not necessary to answer now — is whether we should allow a tuple of definitions, for each dimension. Generally we use names, which helps prevent mistakes, and allows us to be less concerned about dimension ordering.
if isinstance(chunks, (Number, str, int)):
chunks = dict.fromkeys(self.dims, chunks)
if not isinstance(chunks, Mapping):
# We need to ignore since mypy doesn't recognize this can't be `None`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait... is the above check for None correct? I'm not sure that the chunks_kwargs can be None, isn't it always a dict? So this would make the warning not reachable...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think chunks_kwargs can be None, but chunks can... Does that answer your Q?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it answers the question.
But then this means that the above code is wrong because the if statement is never reachable...
Doesn't need to be fixed in this PR, but maybe rewriting this will remove the need for the type ignore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what you mean...

T_Chunks = Union[int, dict[Any, Any], Literal["auto"], None]
# int, Literal["auto"], None, tuple[int, ...], tuple[tuple[int, ...], ...]
# In some cases we don't allow `None`, which this doesn't take account of.
T_ChunkDim: TypeAlias = Union[int, Literal["auto"], None, tuple[int, ...]]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slowly we should agree on a naming convention, haha.
But the addition of TypeAlias is nice, we should do that for all other types here as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! I even went back & forth on whether this should be T_ given it's not a TypeVar or Generic... But then I saw it existed elsewhere and didn't want to make a big change without having a strong view...

@max-sixty max-sixty enabled auto-merge (squash) September 28, 2023 16:10
@max-sixty max-sixty merged commit 0d6cd2a into pydata:main Sep 28, 2023
@max-sixty max-sixty deleted the chunk-type branch September 28, 2023 16:47
max-sixty added a commit to max-sixty/xarray that referenced this pull request Sep 28, 2023
Based on comment in pydata#8247. This doesn't make it perfect, but allows the warning to get hit and clarifies the type comment, as a stop-gap
max-sixty added a commit to max-sixty/xarray that referenced this pull request Sep 28, 2023
Based on comment in pydata#8247. This doesn't make it perfect, but allows the warning to get hit and clarifies the type comment, as a stop-gap
max-sixty added a commit that referenced this pull request Sep 28, 2023
* Refine `chunks=None` handling

Based on comment in #8247. This doesn't make it perfect, but allows the warning to get hit and clarifies the type comment, as a stop-gap

* Test avoiding redefinition

---------

Co-authored-by: Illviljan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants