Skip to content

__variable__ before class treated as special method #4170

Closed

Description

@peterjc

Testing with sphinx from git commit 5194d15 using Python 3.6

Sample test case made up of three files, silly.py

"""This is a silly module."""

__version__ = "0.0.1"


assert True, "This line is a workaround for a sphinx bug"


class Silly(object):
    """Silly object."""

    def __str__(self):
        """String representation of the Silly object."""
        return "Very silly"


if __name__ == "__main__":
    s = Silly()
    print(s)

And silly.rst,

Module contents
---------------

.. automodule:: silly
    :members:
    :undoc-members:
    :show-inheritance:
    :special-members:

and a minimal conf.py as well:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Bio documentation build configuration file, created by hand

extensions = ['sphinx.ext.autodoc']
source_suffix = '.rst'

# The master toctree document.
master_doc = 'silly'

Running the test case as written:

$ python3 -msphinx -W . html
Running Sphinx v1.7+
making output directory...
loading pickled environment... not yet created
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: 1 added, 0 changed, 0 removed
reading sources... [100%] silly                                                                                                                                   
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] silly                                                                                                                                    
generating indices... genindex py-modindex
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded.

Commenting out the assert line workaround to show the bug:

$ python3 -msphinx -W . html
Running Sphinx v1.7+
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 0 source files that are out of date
updating environment: 0 added, 1 changed, 0 removed
reading sources... [100%] silly                                                                                                                                   

Warning, treated as error:
/private/tmp/test_case/silly.rst:4: (WARNING/2) autodoc: failed to import attribute 'Silly.__version__' from module 'silly'; the following exception was raised:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Sphinx-1.7.dev20171020-py3.6.egg/sphinx/util/inspect.py", line 170, in safe_getattr
    return getattr(obj, name, *defargs)
AttributeError: type object 'Silly' has no attribute '__version__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Sphinx-1.7.dev20171020-py3.6.egg/sphinx/ext/autodoc/__init__.py", line 405, in import_object
    obj = self.get_attr(obj, part)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Sphinx-1.7.dev20171020-py3.6.egg/sphinx/ext/autodoc/__init__.py", line 294, in get_attr
    return safe_getattr(obj, name, *defargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Sphinx-1.7.dev20171020-py3.6.egg/sphinx/util/inspect.py", line 186, in safe_getattr
    raise AttributeError(name)
AttributeError: __version__

This minimal test case was based on finding similar issues using __version__ = ... and __all__ = (..., ...) lines in Biopython.

Activity

  1. tk0miya commented on Oct 21, 2017

    @tk0miya
    Member

    It seems fixed by #4157. Could you check this again with HEAD of master please?

  2. added this to the 1.7 milestone on Oct 21, 2017
  3. peterjc commented on Oct 21, 2017

    @peterjc
    ContributorAuthor

    I will test this, but I think you are right - my issue looks like a duplicate of #4156.

  4. peterjc commented on Oct 21, 2017

    @peterjc
    ContributorAuthor

    Confirmed, fixed via #4157 (so this was the same problem as issue #4156). Thank you!

  5. locked as resolved and limited conversation to collaborators on Aug 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions