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

Add option to document properties using autoproperty instead of autoattribute #197

Merged
merged 5 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Changes in sphinx-automodapi
0.19.0 (unreleased)
-------------------

- Add ``automodsumm_properties_are_attributes`` configuration to control if
class properties are treated with ``autoattribute`` or ``autoproperty``.
[#197]

0.18.0 (2024-09-13)
-------------------

Expand Down
5 changes: 5 additions & 0 deletions sphinx_automodapi/automodapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ class are included in the generated documentation. Defaults to ``False``.
methods like ``__getitem__`` and ``__setitem__``. Defaults to
``['__init__', '__call__']``.

* ``automodsumm_properties_are_attributes``
Should be a bool and if ``True`` properties are treated as attributes in the
documentation meaning that no property specific documentation is generated.
Defaults to ``True``.

.. _automodule: http://sphinx-doc.org/latest/ext/autodoc.html?highlight=automodule#directive-automodule
"""

Expand Down
29 changes: 23 additions & 6 deletions sphinx_automodapi/automodsumm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class members that are inherited from a base class. This value can be
methods like ``__getitem__`` and ``__setitem__``. Defaults to
``['__init__', '__call__']``.

* ``automodsumm_properties_are_attributes``
Should be a bool and if ``True`` properties are treated as attributes in the
documentation meaning that no property specific documentation is generated.
Defaults to ``True``.

.. _sphinx.ext.autosummary: http://sphinx-doc.org/latest/ext/autosummary.html
.. _autosummary: http://sphinx-doc.org/latest/ext/autosummary.html#directive-autosummary

Expand Down Expand Up @@ -321,7 +326,8 @@ def process_automodsumm_generation(app):
lines, sfn, app=app, builder=app.builder,
base_path=app.srcdir,
inherited_members=app.config.automodsumm_inherited_members,
included_members=app.config.automodsumm_included_members)
included_members=app.config.automodsumm_included_members,
properties_are_attributes=app.config.automodsumm_properties_are_attributes)


# _automodsummrex = re.compile(r'^(\s*)\.\. automodsumm::\s*([A-Za-z0-9_.]+)\s*'
Expand Down Expand Up @@ -462,7 +468,8 @@ def generate_automodsumm_docs(lines, srcfn, app=None, suffix='.rst',
base_path=None, builder=None,
template_dir=None,
inherited_members=False,
included_members=('__init__', '__call__')):
included_members=('__init__', '__call__'),
*, properties_are_attributes=True):
"""
This function is adapted from
`sphinx.ext.autosummary.generate.generate_autosummmary_docs` to
Expand Down Expand Up @@ -595,10 +602,10 @@ def get_members_class(obj, typ, include_public=[],
continue
if typ is None or documenter.objtype == typ:
items.append(name)
elif typ == 'attribute' and documenter.objtype == 'property':
# In Sphinx 2.0 and above, properties have a separate
# objtype, but we treat them the same here.
items.append(name)
# elif typ == 'attribute' and documenter.objtype == 'property':
# # In Sphinx 2.0 and above, properties have a separate
# # objtype, but we treat them the same here.
# items.append(name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be removed?

public = [x for x in items
if x in include_public or not x.startswith('_')]
return public, items
Expand Down Expand Up @@ -629,6 +636,15 @@ def get_members_class(obj, typ, include_public=[],
ns['attributes'], ns['all_attributes'] = \
get_members_class(obj, 'attribute',
include_base=include_base)
public_properties, all_properties = \
get_members_class(obj, 'property',
include_base=include_base)
if properties_are_attributes:
ns['attributes'].extend(public_properties)
ns['all_attributes'].extend(all_properties)
else:
ns['properties'] = public_properties
ns['all_properties'] = all_properties
Comment on lines +639 to +647
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the real change.

ns['methods'].sort()
ns['attributes'].sort()

Expand Down Expand Up @@ -703,6 +719,7 @@ def setup(app):
app.add_config_value('automodsumm_inherited_members', False, 'env')
app.add_config_value(
'automodsumm_included_members', ['__init__', '__call__'], 'env')
app.add_config_value('automodsumm_properties_are_attributes', True, 'env')

return {'parallel_read_safe': True,
'parallel_write_safe': True}
12 changes: 10 additions & 2 deletions sphinx_automodapi/templates/autosummary_core/class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{% endif %}

{% block attributes_summary %}
{% if attributes %}
{% if attributes or properties %}

.. rubric:: Attributes Summary

Expand All @@ -27,6 +27,10 @@
~{{ name }}.{{ item }}
{%- endfor %}

{% for item in properties %}
~{{ name }}.{{ item }}
{%- endfor %}

{% endif %}
{% endblock %}

Expand All @@ -44,14 +48,18 @@
{% endblock %}

{% block attributes_documentation %}
{% if attributes %}
{% if attributes or properties%}

.. rubric:: Attributes Documentation

{% for item in attributes %}
.. autoattribute:: {{ item }}
{%- endfor %}

{% for item in properties %}
.. autoproperty:: {{ item }}
{%- endfor %}

{% endif %}
{% endblock %}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
SequenceSubclass
================

.. currentmodule:: sphinx_automodapi.tests.example_module.abstract_classes

.. autoclass:: SequenceSubclass
:show-inheritance:

.. rubric:: Attributes Summary

.. autosummary::

~SequenceSubclass.my_property

.. rubric:: Methods Summary

.. autosummary::

~SequenceSubclass.my_method

.. rubric:: Attributes Documentation

.. autoproperty:: my_property

.. rubric:: Methods Documentation

.. automethod:: my_method
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

sphinx_automodapi.tests.example_module.abstract_classes Module
--------------------------------------------------------------

.. automodule:: sphinx_automodapi.tests.example_module.abstract_classes

Classes
^^^^^^^

.. automodsumm:: sphinx_automodapi.tests.example_module.abstract_classes
:classes-only:
:toctree: api

Class Inheritance Diagram
^^^^^^^^^^^^^^^^^^^^^^^^^

.. automod-diagram:: sphinx_automodapi.tests.example_module.abstract_classes
:private-bases:
:parts: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. currentmodule:: sphinx_automodapi.tests.example_module.abstract_classes

.. autosummary::
:toctree: api

SequenceSubclass
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Egg
===

.. currentmodule:: sphinx_automodapi.tests.example_module

.. autoclass:: Egg
:show-inheritance:

.. rubric:: Attributes Summary

.. autosummary::

~Egg.weight

.. rubric:: Methods Summary

.. autosummary::

~Egg.buy
~Egg.eat

.. rubric:: Attributes Documentation

.. autoproperty:: weight

.. rubric:: Methods Documentation

.. automethod:: buy
.. automethod:: eat
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

sphinx_automodapi.tests.example_module Package
----------------------------------------------

.. automodule:: sphinx_automodapi.tests.example_module

Classes
^^^^^^^

.. automodsumm:: sphinx_automodapi.tests.example_module
:classes-only:
:toctree: api
:allowed-package-names: sphinx_automodapi.tests.example_module.classes

Class Inheritance Diagram
^^^^^^^^^^^^^^^^^^^^^^^^^

.. automod-diagram:: sphinx_automodapi.tests.example_module
:private-bases:
:parts: 1
:allowed-package-names: sphinx_automodapi.tests.example_module.classes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. currentmodule:: sphinx_automodapi.tests.example_module

.. autosummary::
:toctree: api

Egg
Spam
2 changes: 2 additions & 0 deletions sphinx_automodapi/tests/cases/attribute_class/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This example is to make sure that classes can have attributes and properties
and they can be distiguished if ``automodsumm_properties_are_attributes = False``
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodapi:: sphinx_automodapi.tests.example_module.attribute_class
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ClassWithAttribute
==================

.. currentmodule:: sphinx_automodapi.tests.example_module.attribute_class

.. autoclass:: ClassWithAttribute
:show-inheritance:

.. rubric:: Attributes Summary

.. autosummary::

~ClassWithAttribute.my_attribute
~ClassWithAttribute.my_property

.. rubric:: Methods Summary

.. autosummary::

~ClassWithAttribute.my_method

.. rubric:: Attributes Documentation

.. autoattribute:: my_attribute
.. autoattribute:: my_property

.. rubric:: Methods Documentation

.. automethod:: my_method
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

sphinx_automodapi.tests.example_module.attribute_class Module
-------------------------------------------------------------

.. automodule:: sphinx_automodapi.tests.example_module.attribute_class

Classes
^^^^^^^

.. automodsumm:: sphinx_automodapi.tests.example_module.attribute_class
:classes-only:
:toctree: api

Class Inheritance Diagram
^^^^^^^^^^^^^^^^^^^^^^^^^

.. automod-diagram:: sphinx_automodapi.tests.example_module.attribute_class
:private-bases:
:parts: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. currentmodule:: sphinx_automodapi.tests.example_module.attribute_class

.. autosummary::
:toctree: api

ClassWithAttribute
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
ClassWithAttribute
==================

.. currentmodule:: sphinx_automodapi.tests.example_module.attribute_class

.. autoclass:: ClassWithAttribute
:show-inheritance:

.. rubric:: Attributes Summary

.. autosummary::

~ClassWithAttribute.my_attribute

~ClassWithAttribute.my_property

.. rubric:: Methods Summary

.. autosummary::

~ClassWithAttribute.my_method

.. rubric:: Attributes Documentation

.. autoattribute:: my_attribute

.. autoproperty:: my_property

.. rubric:: Methods Documentation

.. automethod:: my_method
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

sphinx_automodapi.tests.example_module.attribute_class Module
-------------------------------------------------------------

.. automodule:: sphinx_automodapi.tests.example_module.attribute_class

Classes
^^^^^^^

.. automodsumm:: sphinx_automodapi.tests.example_module.attribute_class
:classes-only:
:toctree: api

Class Inheritance Diagram
^^^^^^^^^^^^^^^^^^^^^^^^^

.. automod-diagram:: sphinx_automodapi.tests.example_module.attribute_class
:private-bases:
:parts: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. currentmodule:: sphinx_automodapi.tests.example_module.attribute_class

.. autosummary::
:toctree: api

ClassWithAttribute
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Egg
===

.. currentmodule:: sphinx_automodapi.tests.example_module.classes

.. autoclass:: Egg
:show-inheritance:

.. rubric:: Attributes Summary

.. autosummary::

~Egg.weight

.. rubric:: Methods Summary

.. autosummary::

~Egg.buy
~Egg.eat

.. rubric:: Attributes Documentation

.. autoproperty:: weight

.. rubric:: Methods Documentation

.. automethod:: buy
.. automethod:: eat
Loading
Loading