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

ENH: Improve object fully qualified names. #18198

Open
Carreau opened this issue Jan 20, 2021 · 2 comments
Open

ENH: Improve object fully qualified names. #18198

Carreau opened this issue Jan 20, 2021 · 2 comments

Comments

@Carreau
Copy link
Contributor

Carreau commented Jan 20, 2021

Feature

For reasons related to documentation and showing it in IPython/Jupyter I have some questions/suggestions wrt fully qualified names of object and improvement.

For example if a user does:

import <whatever> as x
x?

I want to be able to say "view online docs at https://numpy.org/doc/stable/reference/generated/{fully_qual}.html".format(full_qual(x))" with roughly:

def full_qual(x):
    return x.__module__+'.'+x.__qualname__

As well as the "inverse" operation: from a str get the object back:

parts = full_qual_name.split('.')
target = parts[0]
for k in parts[1:]:
    target = getattr(target, k)

And I have some issues with a few object, the mostly fell in 2 categories:

  1. Object with no module
# full_qual(np.exp) # AttrError , no attribute __module__, (no __qualname__, but have __name__) same with sin, cos...

Could it be possible to add a module to those ?

full_qual(np.memmap) # 'numpy.memmap' is Ok
full_qual(np.memmap.flush)  # 'numpy.core.memmap.memmap.flush' is problematic:

target = numpy
for k in ['core', 'memmap', 'memmap', 'flush']:
    target = getattr(target, k) # fails with AttributeError: type object 'memmap' has no attribute 'memmap'

For this second one this is due to numpy.core.memmap being ambiguous as core/__init__.py contain from .memmap import memmap so the class shadow the module in which it's defined.

Yes I know I can access .flush via np.memmap.flush it's just one example.

Would it be ok to have a consistent way to go from objects to fully_qualified name and vice versa, and should I open issues when I find such functions/methods/class ?

@eric-wieser
Copy link
Member

#4952 I think is most of your problem here

@seberg
Copy link
Member

seberg commented Jan 20, 2021

I am happy to add a module and/or qualname to ufuncs, we just need to decide on how...

Currently, the name is basically passed to the C creation function. We could do it like Python's static types and just assume everything before the first . is part of the module, that is not ideal (for one some ufuncs couldrequire a __qualname__ technically), but likely "just works" (have to check if pickle is fine with getting __module__ + "."+ __qualname__, but I expect it is).

The other option, is to make __module__ and __qualname__ writeable attribute (or writeable exactly once, with the option of disallowing it when used with future API).


np.memmap thing seems like the @set_module('numpy') decorator can't or doesn't properly set the module of the attributes. I guess the decorator could try to be smart about methods or is there a better solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants