Chilipp / autodocsumm

Extending your autodoc API docs with a summary
Apache License 2.0
48 stars 14 forks source link

"Failed to import" error if a class is imported and it has an attribute without a default value #41

Open xuhdev opened 3 years ago

xuhdev commented 3 years ago

Consider a class that has an attribute without a default value, and is imported. If autoclass is applied to this class and autosummary is turned on, sphinx-build errors out.

Reproduce

Download bug.tar.gz. Unarchive it, and run

PYTHONPATH=. sphinx-build -v -d "./_build" docs "_build/html" -b html -W --color

It produces an error:

Running Sphinx v3.4.1
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: [new config] 1 added, 0 changed, 0 removed
reading sources... [100%] index

Traceback (most recent call last):
  File "/home/hong/.local/lib/python3.7/site-packages/sphinx/cmd/build.py", line 280, in build_main
    app.build(args.force_all, filenames)
  File "/home/hong/.local/lib/python3.7/site-packages/sphinx/application.py", line 352, in build
    self.builder.build_update()
  File "/home/hong/.local/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 298, in build_update
    len(to_build))
  File "/home/hong/.local/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 310, in build
    updated_docnames = set(self.read())
  File "/usr/lib/python3.7/contextlib.py", line 119, in __exit__
    next(self.gen)
  File "/home/hong/.local/lib/python3.7/site-packages/sphinx/util/logging.py", line 214, in pending_warnings
    memhandler.flushTo(logger)
  File "/home/hong/.local/lib/python3.7/site-packages/sphinx/util/logging.py", line 179, in flushTo
    logger.handle(record)
  File "/usr/lib/python3.7/logging/__init__.py", line 1529, in handle
    self.callHandlers(record)
  File "/usr/lib/python3.7/logging/__init__.py", line 1591, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python3.7/logging/__init__.py", line 901, in handle
    rv = self.filter(record)
  File "/usr/lib/python3.7/logging/__init__.py", line 762, in filter
    result = f.filter(record)
  File "/home/hong/.local/lib/python3.7/site-packages/sphinx/util/logging.py", line 422, in filter
    raise exc
sphinx.errors.SphinxWarning: /home/hong/tmp/myproject/_impl.py:docstring of myproject._impl.A.rst:1:autosummary: failed to import myproject.A.a

Warning, treated as error:
/home/hong/tmp/myproject/_impl.py:docstring of myproject._impl.A.rst:1:autosummary: failed to import myproject.A.a

Content of bug.tar.gz:

├── docs
│   ├── conf.py
│   └── index.rst
└── myproject
    ├── _impl.py
    └── __init__.py

_impl.py:

class A:
    "A's doc"

    a: int
    "a's doc."

__init__.py:

from ._impl import A

conf.py:

project = 'myproject'
copyright = '2020, someone'
author = 'someone'
extensions = [
    'sphinx.ext.autodoc',
    'autodocsumm'
]
html_theme = 'alabaster'

index.rst:

Welcome to myproject's documentation!
=====================================

.. currentmodule:: myproject

.. autoclass:: A
   :members:
   :show-inheritance:
   :autosummary:

Either removing :autosummary: or put the definition of class A to __init__.py resolves the error.

Chilipp commented 3 years ago

Hey @xuhdev ! Thanks a lot for reporting this issue. I think that this should rather be resolved within the autosummary extension, shouldn't it? I saw that you already created the corresponding issue in the sphinx repo, so maybe we keep this issue here open and close it when https://github.com/sphinx-doc/sphinx/issues/8645 has been resolved

xuhdev commented 3 years ago

Hi @Chilipp ! I'm not sure whether the two issues are actually the same issue: https://github.com/sphinx-doc/sphinx/issues/8645 doesn't involve imported module, but this issue is only reproduceable if an imported module is involved. But they may be related though, I just don't understand the extension enough to make the judgment.