You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using entry_points to register dataset and dataarray accessors? #7348
External libraries often use the dataset/dataarray accessor pattern (e.g. metpy). These accessors are not available until importing the external package where the registration occurs. This means scripts using these accessors must include an often-unused import that linters will complain about e.g.
import metpy # linter complains here
# some data
ds: xr.Dataset = ...
ds.metpy....
Describe the solution you'd like
Use importlib entrypoints to register these as entrypoints so that registration is automatically handled. This is currently enabled for the array backend, but not for accessors (e.g. metpy's setup.cfg).
Whilst I agree that this would be a more "correct" way of providing accessor functionality, I think there is a big downside in that Entrypoints are quite a lot harder for users to use than the accessors are.
Whilst the way the accessors actually add the method is kind of black magic, all the user has to do is copy-paste the example from the docs and change the methods and accessor name to what they want, and it will immediately work.
Adding an entrypoint requires going into your setup.py (which you wouldn't even have if you're running a script or notebook), and it's conceptually complicated. The accessors stuff is useful even for relatively novice users, so I don't really want to make it harder to understand...
I see your point, but xarray could do both, and most accessors I've used come in a pip installable package and we can make that workflow a bit smoother with entry points. IMO it is an advantage of entrypoints that they don't require editing source code, just a 1 line change to a setup.py, setup.cfg, or pyproject.toml.
I wonder how often "users" define their own accessors...I use python functions and modules. The "black magic" you mention breaks most static analysis tooling (type checking, linting, completion) and saves at most a couple of characters, so I never felt the need, but that's a discussion for another day.
I think it would be nice to have the entry point method as an optional way to get accessors loaded. Essentially this is a way to have xarray execute the needed import rather than the user needing to do it manually.
It looks like the entry points syntax could be used to simply define some modules where accessors live that xarray could import and still use the standard registration decorators that are used today. You're just using setup.cfg/pyproject.toml to register the hooks at install time and have importlib.metadata as an easy way to have Python scan the environment for the appropriate hooks.
Activity
TomNicholas commentedon Jan 13, 2023
Thanks for the suggestion @nbren12 !
Whilst I agree that this would be a more "correct" way of providing accessor functionality, I think there is a big downside in that Entrypoints are quite a lot harder for users to use than the accessors are.
Whilst the way the accessors actually add the method is kind of black magic, all the user has to do is copy-paste the example from the docs and change the methods and accessor name to what they want, and it will immediately work.
Adding an entrypoint requires going into your setup.py (which you wouldn't even have if you're running a script or notebook), and it's conceptually complicated. The accessors stuff is useful even for relatively novice users, so I don't really want to make it harder to understand...
Interested in what other people think though?
nbren12 commentedon Jan 20, 2023
I see your point, but xarray could do both, and most accessors I've used come in a pip installable package and we can make that workflow a bit smoother with entry points. IMO it is an advantage of entrypoints that they don't require editing source code, just a 1 line change to a setup.py, setup.cfg, or pyproject.toml.
I wonder how often "users" define their own accessors...I use python functions and modules. The "black magic" you mention breaks most static analysis tooling (type checking, linting, completion) and saves at most a couple of characters, so I never felt the need, but that's a discussion for another day.
max-sixty commentedon Sep 14, 2023
Are there similar patterns in other libraries around using entrypoints?
I agree it's important to allow defining them in python. Also allowing entrypoints sounds viable; would be interesting to see prior art.
dopplershift commentedon Sep 14, 2023
I think it would be nice to have the entry point method as an optional way to get accessors loaded. Essentially this is a way to have xarray execute the needed import rather than the user needing to do it manually.
It looks like the entry points syntax could be used to simply define some modules where accessors live that xarray could import and still use the standard registration decorators that are used today. You're just using
setup.cfg/pyproject.tomlto register the hooks at install time and haveimportlib.metadataas an easy way to have Python scan the environment for the appropriate hooks.