-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
include accessors in __dir__
#9985
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
base: main
Are you sure you want to change the base?
Changes from all commits
5314f02
4dc0527
bc5a25b
9433b64
2b7d0c8
050d834
9862105
580fd3f
bdc7984
92d9f89
336eb26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ | |
| from operator import methodcaller | ||
| from os import PathLike | ||
| from types import EllipsisType | ||
| from typing import IO, TYPE_CHECKING, Any, Generic, Literal, cast, overload | ||
| from typing import IO, TYPE_CHECKING, Any, ClassVar, Generic, Literal, cast, overload | ||
|
|
||
| import numpy as np | ||
| from pandas.api.types import is_extension_array_dtype | ||
|
|
@@ -57,6 +57,7 @@ | |
| from xarray.core.arithmetic import DatasetArithmetic | ||
| from xarray.core.array_api_compat import to_like_array | ||
| from xarray.core.common import ( | ||
| AccessorMixin, | ||
| DataWithCoords, | ||
| _contains_datetime_like_objects, | ||
| get_chunksizes, | ||
|
|
@@ -550,6 +551,7 @@ class Dataset( | |
| DataWithCoords, | ||
| DatasetAggregations, | ||
| DatasetArithmetic, | ||
| AccessorMixin, | ||
| Mapping[Hashable, "DataArray"], | ||
| ): | ||
| """A multi-dimensional, in memory, array database. | ||
|
|
@@ -709,6 +711,7 @@ class Dataset( | |
| _close: Callable[[], None] | None | ||
| _indexes: dict[Hashable, Index] | ||
| _variables: dict[Hashable, Variable] | ||
| _accessors: ClassVar[set[str]] = set() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think using a mutable default is bad practice.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, the only issue would be that subclasses inherit this and try to modify this. However, if they do it would probably be better to just have them override the class variable. (Additionally, as far as I understand the class is an instance of the metaclass, so having the usual "sentinel then switch to mutable on first access" doesn't make sense for class variables) |
||
|
|
||
| __slots__ = ( | ||
| "__weakref__", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,10 @@ def __init__(self, xarray_obj): | |
| def foo(self): | ||
| return "bar" | ||
|
|
||
| assert "demo" in dir(xr.DataTree) | ||
| assert "demo" in dir(xr.Dataset) | ||
| assert "demo" in dir(xr.DataArray) | ||
|
Comment on lines
+37
to
+39
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this PR also allow completion of the actual accessor methods, or just the accessor namespace? If so then we should test that too.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it does, but but how would you test that? This is functionality of the auto-completer... Edit: to be clear, I think auto-completion of the namespace was already working before this PR
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, I just tried
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Surely just ds = xr.Dataset()
assert "foo" in dir(ds.demo)?
I just tried seeing if
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Edit: And
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are any of you using a different IDE that does show the expected code-completion suggestion?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @keewis is right, most IDEs nowadays support auto-completion via LSP servers based on (somewhat strict) static code analysers such as pyright / pylance. So I'm afraid there's nothing much we can do to support auto-completion for dynamic attributes. IDE configs using jedi under the hood may support it, although IIUC jedi operates under different modes (Interpreter vs. Script) so I'm not sure auto-completion for dynamic attributes will always work (probably only in the Interpreter mode?).
Decorators are just syntax sugar for functions applied on other functions or classes, so if the decorator function is properly typed (i.e., its return type is clear or can be statically inferred) any static code analyser will be able to provide auto-completions.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to enable this for mypy here: #7117 Maybe something similar is possible for pyright, pylance?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the insight @benbovy. Sadly, for me at least, this seems to confirm one of the reasons that I'm not a fan of dynamically registered accessors.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sorry, just saw this.
This is also my understanding — a kernel will give better info than an IDE because it's doing something like calling |
||
|
|
||
| dt: xr.DataTree = xr.DataTree() | ||
| assert dt.demo.foo == "bar" | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.