autoclass and instance attributes

2024/9/29 21:20:48

According to the sphinx documentation, the .. autoattribute directive should be able to document instance attributes. However, if I do::

.. currentmodule:: xml.etree.ElementTree.. autoclass:: ElementTree.. autoattribute:: ElementTree._root

Then when building I get an AttributeError:

Traceback (most recent call last):etree.ElementTree.ElementTree                 File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/ext/autodoc.py", line 326, in import_objectobj = self.get_attr(obj, part)File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/ext/autodoc.py", line 232, in get_attrreturn safe_getattr(obj, name, *defargs)File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/util/inspect.py", line 70, in safe_getattrraise AttributeError(name)
AttributeError: _root

even though if I instantiate ElementTree and try and access the _root attribute, it works fine::

>>> from xml.etree.ElementTree import ElementTree
>>> e = ElementTree()
>>> hasattr(e, '_root')
True

What am I doing wrong?

(I'm actually having this issue with one of my own classes, but am just using the ElementTree class as an example since it's in the standard library)

Answer

This looks like a bug in the way non-public instance attributes are handled. Sphinx is supposed to be able to recognize instance attributes defined in __init__.

I can't say how this should be fixed. There is an open bug report that seems to be related: Non-public instance attributes are not documented without __slots__.

If the following line is added to the definition of the ElementTree class in ElementTree.py,

__slots__  = ["_root"]

then the AttributeError that you get goes away.

https://en.xdnf.cn/q/71165.html

Related Q&A

python sqlite3 update not updating

Question: Why is this sqlite3 statement not updating the record?Info:cur.execute(UPDATE workunits SET Completed=1 AND Returns=(?) WHERE PID=(?) AND Args=(?),(pickle.dumps(Ret),PID,Args))Im using py…

Unable to reinstall PyTables for Python 2.7

I am installing Python 2.7 in addition to 2.7. When installing PyTables again for 2.7, I get this error -Found numpy 1.5.1 package installed. .. ERROR:: Could not find a local HDF5 installation. You ma…

How can I invoke a thread multiple times in Python?

Im sorry if it is a stupid question. I am trying to use a number of classes of multi-threading to finish different jobs, which involves invoking these multi-threadings at different times for many times…

Matplotlib interactive graph embedded in PyQt

Ive created a simple python script that when run should display an embedded matplotlib graph inside a PyQT window. Ive used this tutorial for embedding and running the graph. Aside from some difference…

How to pass path names to Python script by dropping files/folders over script icon

I am working in Mac OS X and have been writing simple file/folder copy scripts in Python. Is there a way to drag and drop a folder on top of a Python script icon and pass the file or folders path as an…

Why wouldnt I want to add Python.exe to my System Path at install time?

Im reinstalling Python, on Windows 7, and one of the first dialog boxes is the Customize Python screen.The default setting for "Add Python.exe to Path" is "Entire feature will be unavail…

How to install python-gtk2, python-webkit and python-jswebkit on OSX

Ive read through many of the related questions but am still unclear how to do this as there are many software combinations available and many solutions seem outdated.What is the best way to install the…

Forking python, defunct child

I have some troubles with Python child processes so I wrote a very simple script:import os import sys import timepid = os.fork() if pid:#parenttime.sleep(30) else:#child#os._exit(0)sys.exit()While pare…

is there a way to know the length of a bytearray variable in python?

I have this code:variable = "FFFF" message = bytearray( variable.decode("hex") )after this, I want to perform something like this:message.len()but it seems that bytearray does not h…

Matplotlib: Check for empty plot

I have a loop which loads and plots some data, something like this:import os import numpy as np import matplotlib.pyplot as pltfor filename in filenames:plt.figure()if os.path.exists(filename):x, y = n…