Skip to content
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

Inspecting arguments with accessors #7234

Open
ljstrnadiii opened this issue Oct 28, 2022 · 2 comments
Open

Inspecting arguments with accessors #7234

ljstrnadiii opened this issue Oct 28, 2022 · 2 comments

Comments

@ljstrnadiii
Copy link

What is your issue?

I recently read about the suggested approach to extending xarray here: Extending xarray 2

We have a lot of functions on xarray datasets that are domain-specific enough to spatial mosaics that I think I want to start using these.

One issue I am seeing so far is that when I use the accessor in jupyter notebook, the arguments and docs are inspected and returned, which makes it nice and usable. However, within vscode, it is unable to inspect the args. The side effect is slower development and no more type checking, which isn’t ideal. This is also an issue with the rio accessor.

Am I missing something here? Has anyone noticed this and is there an existing fix? This must also be an issue with pandas custom accessors, too. Is there a way to more officially register accessors within my packages entry points?

For example

## inside notebooks
ds.sp.my_function() # I can inspect as usual
ds.rio.write_crs() # I can inspect as usual

## inside vscode
ds.sp.my_function() # I can not inspect the args or their types
ds.rio.write_crs() # I can not inspect the args or their types

I have seen sphinx-autosummary-accessors — sphinx-autosummary-accessors documentation 1, but that only seems relevant for doc generation.

@ljstrnadiii ljstrnadiii added the needs triage Issue that has not been reviewed by xarray team member label Oct 28, 2022
@TomNicholas TomNicholas added bug and removed needs triage Issue that has not been reviewed by xarray team member labels Oct 28, 2022
@headtr1ck
Copy link
Collaborator

Unfortunately I don't think this is possible since this kind of inspection is using static code analysis while jupyter Notebook can inspect dynamically.

In #7117
I tried to enable this for mypy.

But without any kind of plugin I doubt it is possible to tell static checkers about this.

@ljstrnadiii
Copy link
Author

ljstrnadiii commented Oct 31, 2022

Yeah, I was afraid of that lol. This must also be a pandas issue, too, since they recommend a similar way of extending pandas dataframes. I couldn't find a similar pandas issue/pr and was a bit surprised by that.

My temp solution is to use type narrowing:

def use_domain_specific_dataset(dset: xr.Dataset):
    if not isinstance(dset.ma, MyAccessor):
        raise something
    # gets some type checking
    dset.ma.my_special_func()

Seems to at least provide the basic sanity checks

@headtr1ck headtr1ck removed the bug label Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants