autosummary: documenting from import statements

Justin Giorgi avatarJustin Giorgi created an issue

Autosummary is including imported classes in the stub files when the classes are imported using a statement like:

from foo import bar

Comments (8)

  1. Anonymous

    I think this could be a quick fix to the problem: for classes/functions/exceptions in a module, we will check whether its "module" attribute matches the "name" attribute of the module:

                def get_module_members(obj, typ, include_public=[]):
                    items = []
                    for name in dir(obj)
                        if get_documenter(getattr(obj, name)).objtype == typ:
                            # check whether imported
                            if getattr(obj, '__module__') == getattr(obj, '__name__'):
                                items.append(name)
    
                    public = [x for x in items
                              if x in include_public or not x.startswith('_')]
                    return public, items
    
  2. Anonymous

    Ok, the following code should be an acceptable workaround.

    Add the following after get_members() in sphinx.ext.autosummary.generate(), and use it when the doc.objtype == 'module' instead of get_members.

    def get_module_members(obj, typ, include_public=[]):
        items = []
        for name in dir(obj):
            if get_documenter(getattr(obj, name)).objtype == typ:
                # check whether imported
                if (getattr(getattr(obj,name), '__module__') 
                        == getattr(obj, '__name__')):
                    items.append(name)
    
        public = [x for x in items
                  if x in include_public or not x.startswith('_')]
        return public, items
    
  3. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.