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

Refine chunks=None handling #8249

Merged
merged 2 commits into from
Sep 28, 2023
Merged

Refine chunks=None handling #8249

merged 2 commits into from
Sep 28, 2023

Conversation

max-sixty
Copy link
Collaborator

@max-sixty max-sixty commented Sep 28, 2023

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

@max-sixty max-sixty changed the title ds chunks none Refine chunks=None handling 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
Comment on lines 2645 to 2648
if not isinstance(chunks, Mapping) and chunks is not None:
# The ignore seems to be required because of the type change (though not
# completely sure, the message is somewhat confusing)
chunks = dict.fromkeys(self.dims, chunks) # type: ignore[arg-type]
Copy link
Contributor

Choose a reason for hiding this comment

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

Mypy doesn't like redefinitions. I've started to warm up to mypys opinion as well, it is annoying when a variable changes behavior significantly after a small if-check.

Any time a variable changes type you should expect to create a new variable, so try something like this:

        chunks_dict: dict
        if not isinstance(chunks, Mapping) and chunks is not None:
            chunks_dict = dict.fromkeys(self.dims, chunks)

Remember the downstream names 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.

I agree! Though in this case, that didn't seem to completely satisfy mypy either. I think pushing until we remove the deprecation is probably reasonable...

Copy link
Contributor

Choose a reason for hiding this comment

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

Aha, the error was about the .fromkeys second input. Renaming still seems to work, the typing for chunks_mapping can probably be improved.

Sidenote but I'm quite surprised this worked:

dict.fromkeys((3,4,5), "auto")
Out[9]: {3: 'auto', 4: 'auto', 5: 'auto'}

# I was expecting:
{3: 'a', 4: 'u', 5: 't'}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK nice! I hadn't thought to make it Mapping[Any, Any], I agree it's better now even if not perfect. Thanks!

@max-sixty max-sixty merged commit d6c3767 into pydata:main Sep 28, 2023
@max-sixty max-sixty deleted the ds-chunks-none branch October 4, 2023 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants