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

Use Self rather than concrete types, remove casts #8216

Merged
merged 21 commits into from
Sep 21, 2023

Conversation

max-sixty
Copy link
Collaborator

This should also allow for subtyping

Copy link
Collaborator

@headtr1ck headtr1ck left a comment

Choose a reason for hiding this comment

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

That's something I've wanted to do for quite a while now. Thanks!

xarray/core/dataarray.py Show resolved Hide resolved
xarray/core/dataarray.py Outdated Show resolved Hide resolved
xarray/core/dataarray.py Outdated Show resolved Hide resolved
xarray/core/dataarray.py Outdated Show resolved Hide resolved
xarray/core/dataarray.py Outdated Show resolved Hide resolved
xarray/core/dataarray.py Outdated Show resolved Hide resolved
return _broadcast_helper(
cast("T_Dataset", args[1]), exclude, dims_map, common_coords
cast("Dataset", args[1]), exclude, dims_map, common_coords
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we have to look at what align does to subclasses

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I looked at this, but eventually thought it wasn't worth gating on — it's quite complicated. It's something like "the 'biggest' class" — so "da + da = da", but "da + ds = ds"...

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 adjusted these a bit, see what you think. Allowed removing some casts

@max-sixty max-sixty added the plan to merge Final call for comments label Sep 20, 2023
@@ -664,6 +664,8 @@ def copy(
variables = {
k: v._copy(deep=deep, memo=memo) for k, v in self.variables.items()
}
# Currently this requires using the concrete type of `Coordinates`, rather than
# `Self`, ref #8216
Copy link
Contributor

Choose a reason for hiding this comment

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

why doesn't type(self) work here?

        return type(self)._construct_direct(
            coords=variables, indexes=dict(self.xindexes), dims=dict(self.sizes)
        )

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 went to answer this question, and then got it working :)

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 actually the typing worked but the tests didn't; switching back. Added a TODO but won't chase it down atm...

@max-sixty max-sixty merged commit 3ace2fb into pydata:main Sep 21, 2023
25 of 26 checks passed
@max-sixty max-sixty deleted the typing-self branch September 21, 2023 01:53
@Illviljan Illviljan mentioned this pull request Sep 26, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants