Skip to content

don't type check __getattr__ #4616

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

Closed
wants to merge 1 commit into from

Conversation

mathause
Copy link
Collaborator

It's not pretty as I had to define a number of empty methods... I think this should wait for 0.17

@@ -137,6 +137,11 @@ Internal Changes
By `Yash Saboo <https://github.com/yashsaboo>`_, `Nirupam K N
<https://github.com/Nirupamkn>`_ and `Mathias Hauser
<https://github.com/mathause>`_.
- Added ``@no_type_check`` to ``__getattr__`` of :py:class:`Dataset` and :py:class:`DataArray`.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
- Added ``@no_type_check`` to ``__getattr__`` of :py:class:`Dataset` and :py:class:`DataArray`.
- Added ``@no_type_check`` to ``__getattr__`` of :py:class:`Dataset` and :py:class:`DataArray`.

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.

Thanks for pushing this forward @mathause .

@@ -154,7 +154,8 @@ def _sum_of_weights(
""" Calculate the sum of weights, accounting for missing values """

# we need to mask data values that are nan; else the weights are wrong
mask = da.notnull()
# no type checking because ops are injected
mask = da.notnull() # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this mean that any library downstream that runs .notnull will get a type error? I worry that'll cause a lot of false positives.

What do others think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nix this! @keewis has handled, too fast for me #4618

@mathause
Copy link
Collaborator Author

Does this mean that any library downstream that runs notnull will get a type error?

Yes you are right - I haven't thought this through! This does not only apply for notnull but also for mean etc. because all these operations are injected. So this is definitely a blocker.

@mathause mathause marked this pull request as draft November 30, 2020 09:43
@max-sixty
Copy link
Collaborator

In a way we have two mistakes cancelling each other out — missing methods and not type checking any method because of __getattr__.

We could attempt to add in the methods explicitly rather than injecting them dynamically. I had thoughts about writing something to generate the python code & docstrings, which would give us the benefits of having them statically without the toil of writing them all out / keeping docstrings up to date, but not sure it would save enough time to make it worthwhile.

Any other ideas / thoughts?

@mathause
Copy link
Collaborator Author

mathause commented Dec 1, 2020

Yes, I think that would be worthwhile. Not necessarily because of static typing but because it's quite difficult to understand. There is also a TODO on that:

TODO(shoyer): rewrite this module, making use of xarray.core.computation,
NumPy's __array_ufunc__ and mixin classes instead of the unintuitive "inject"
functions.

I think it should not be too difficult to create a Mixin class. Maybe the docstrings could still be injected. Not sure what is meant with xarray.core.computation (apply_ufunc?) and how the __array_ufunc__ part would work.

@mathause mathause closed this Oct 18, 2021
@mathause mathause deleted the no_type_check_getattr branch October 18, 2021 14:06
@mathause mathause restored the no_type_check_getattr branch October 18, 2021 14:22
@mathause
Copy link
Collaborator Author

I closed the PR by accident - no strong opinion whether to re-open it. Would be nice to get it in, but this can only be done once all the injected methods are replaced & I currently don't have time to work on this.

@mathause mathause deleted the no_type_check_getattr branch January 26, 2022 08:41
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.

Don't type check __getattr__?
2 participants