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

AttributeError: 'Import' object has no attribute 'getattr' #289

Closed
yosoufe opened this issue May 13, 2021 · 6 comments
Closed

AttributeError: 'Import' object has no attribute 'getattr' #289

yosoufe opened this issue May 13, 2021 · 6 comments

Comments

@yosoufe
Copy link

yosoufe commented May 13, 2021

Hello,

I have an issue very similar to #193 but on different object type.

I am using

autoapi_options = ['members', 
                   'undoc-members',
                   'private-members',
                   'show-inheritance',
                   'show-module-summary',
                   'show-inheritance-diagram',
                   'special-members',
                   'imported-members',]

sphinx started to generate the following error. Unfortunately I cannot share the project.

# Sphinx version: 3.5.4
# Python version: 3.7.9 (CPython)
# Docutils version: 0.16 release
# Jinja2 version: 3.0.0

Exception occurred:
. inheritance_diagrams.py", line 18, in _do_import_class
    target = (target.getattr(path_part) or (None,))[0]
AttributeError: 'Import' object has no attribute 'getattr'

Thanks

@AWhetter
Copy link
Collaborator

What version of autoapi are you using?

@yosoufe
Copy link
Author

yosoufe commented May 13, 2021

sphinx-autoapi 1.8.1

@yosoufe
Copy link
Author

yosoufe commented May 14, 2021

Hello @AWhetter,

I created a sample project that creates such an error. Please have a look.

It seems the way I am importing stuff with absolute path from the package is kind of undesirable for autoapi. Like this:

import sample.module_b.bbb
class SubBBB:
    """Sub BBB Class
    """
    def __init__(self) -> None:
        pass

    def some_function(self, arg1: sample.module_b.bbb.BBB):
        return arg1

We would like to avoid relative imports using dots since it is not recommended in different coding style guidelines in python.

This is a very small sample project which should make the debugging easier for you.

Thanks a lot.

sample_project.zip

@MarcSeebold
Copy link

Possible fix:

diff --git a/autoapi/inheritance_diagrams.py b/autoapi/inheritance_diagrams.py
index f905dc2..8d471bd 100644
--- a/autoapi/inheritance_diagrams.py
+++ b/autoapi/inheritance_diagrams.py
@@ -11,7 +11,10 @@ def _do_import_class(name, currmodule=None):
         target = astroid.MANAGER.ast_from_module_name(currmodule)
         while target and path_stack:
             path_part = path_stack.pop()
-            target = (target.getattr(path_part) or (None,))[0]
+            try:
+                target = (target.getattr(path_part) or (None,))[0]
+            except AttributeError:
+                target = None
             while isinstance(target, astroid.ImportFrom):
                 try:
                     target = target.do_import_module(path_part)

@jeffq-intrepid
Copy link

I'm having the same problem and the proposed change @MarcSeebold resolves it for me

@yosoufe
Copy link
Author

yosoufe commented Jun 30, 2021

Thanks @AWhetter . Do you have an estimate when will this fix be released on PyPi server?

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

4 participants