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

Annotations for .data_vars() and .coords() #3207

Merged
merged 2 commits into from
Aug 12, 2019

Conversation

crusaderky
Copy link
Contributor

No description provided.

Copy link
Collaborator

@max-sixty max-sixty left a comment

Choose a reason for hiding this comment

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

Looks great!

raise NotImplementedError()

@property
def dims(self) -> Union[Mapping[Hashable, int], Tuple[Hashable, ...]]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why the change here from return self._data.dims?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because DataArray.dims is a Tuple[Hashable, ...] whereas Dataset.dims is a Mapping[Hashable, int]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See below for the two implementations, which are identical in code but different in signature.
The method needs stay in the abstract class because other methods of the abstract class invoke it; this signature with NotImplementedError() guarantees that the methods of the abstract class can only use dims as the common denominator of the two output types, which is a Collection[Hashable].

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK great

self.update({key: value})

@property
def indexes(self):
def _names(self) -> Set[Hashable]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this new? Should this be on the abstract class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a property invoked by the methods of the abstract class, defined in both the implementations.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Here, I'm not so sure - do all concrete classes need to define this? Generally we wouldn't define private methods on an abstract class (even if all currently concrete classes implement it), because the implementation would be up to the concrete class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What I basically did was turn AbstractCoordinates from a mixin (that is, a class that invokes own methods that are only defined in a subclass) into a C++-style abstract class.
mypy, justifiably, doesn't like mixins - short of hacking your way through with # type tags.

This, in synthesis, is how it was before:

class C:
    def g(self) -> int:
        return self.f()


class D(C):
    def f(self) -> int:
        return 123

mypy correctly states:
3: error: "C" has no attribute "f"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right - I agree this should either be an abstract class without implementations or a mixin with implementations - not a bit of each. It's a bit odd we call this class Abstract but then enforce a _names property.

That a nice upside of mypy - we get to find those things out

I agree we're in a reasonable local maximum here.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, thanks for fixing this up!

xarray/core/coordinates.py Show resolved Hide resolved
@crusaderky crusaderky merged commit 14f1a97 into pydata:master Aug 12, 2019
@crusaderky crusaderky deleted the annotations branch August 12, 2019 21:03
Copy link
Member

@shoyer shoyer left a comment

Choose a reason for hiding this comment

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

Thanks!

self.update({key: value})

@property
def indexes(self):
def _names(self) -> Set[Hashable]:
Copy link
Member

Choose a reason for hiding this comment

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

Yes, thanks for fixing this up!

dcherian added a commit to dcherian/xarray that referenced this pull request Aug 14, 2019
* upstream/master:
  chunk sparse arrays (pydata#3202)
  Annotations for .data_vars() and .coords() (pydata#3207)
  Remove duck_array_ops.as_like_arrays() (pydata#3204)
  Match mypy version between CI and pre-commit hook (pydata#3203)
  BUG fix +test .sel method gives error with float32 values (pydata#3153)
  fix precommit file (pydata#3201)
  Enforce mypy compliance in CI (pydata#3197)
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.

3 participants