-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
include accessors in __dir__
#9985
base: main
Are you sure you want to change the base?
Changes from 9 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 | ||
|
@@ -709,6 +709,7 @@ class Dataset( | |
_close: Callable[[], None] | None | ||
_indexes: dict[Hashable, Index] | ||
_variables: dict[Hashable, Variable] | ||
_accessors: ClassVar[set[str]] = set() | ||
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. 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
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. 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 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 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 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 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? 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. 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? 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. 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" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this has become a bit messy: while
_accessors
is a class variable, it is only available on its subclass. To really make sense we'd have to overrideDataTree.__dir__
,DataArray.__dir___
andDataset.__dir__
withor create a new mixin containing
_accessors
and have all three classes inherit from that (afterAttrAccessMixin
, sosuper().__dir__()
dispatches to that).However,
set(dir(type(self)))
already uses a similar trick (defer to the subclass type's__dir__
) so we may also choose to ignore the typing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What stops you from adding
_accessors: ClassVar[set[str]]
to this mixing class?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mostly that this is a mixin specialized on providing attribute-based syntax (using
.
) for entries inattrs
. I guess it would be best to have another mixin for the accessorsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've created a separate mixin, which allows a separation of concerns (and makes removing the
AttrAccessMixin
easier, should we ever wish to). The downside is that now we have one more base class / mixin.