Skip to content

Using entry_points to register dataset and dataarray accessors? #7348

Open

Description

@nbren12

Is your feature request related to a problem?

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).

Describe alternatives you've considered

No response

Additional context

No response

Activity

  1. TomNicholas commented on Jan 13, 2023

    @TomNicholas
    Member

    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?

  2. nbren12 commented on Jan 20, 2023

    @nbren12
    ContributorAuthor

    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.

  3. max-sixty commented on Sep 14, 2023

    @max-sixty
    Collaborator

    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.

  4. dopplershift commented on Sep 14, 2023

    @dopplershift
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions