-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from kylefawcett/fix/slots
Updated "Use __dict__ and ignore __slots__ on classes #169
- Loading branch information
Showing
11 changed files
with
232 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Test classes that put attributes in `__slots__`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.. automodapi:: sphinx_automodapi.tests.example_module.slots |
17 changes: 17 additions & 0 deletions
17
.../slots/output/api/sphinx_automodapi.tests.example_module.slots.DerivedParam.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
DerivedParam | ||
============ | ||
|
||
.. currentmodule:: sphinx_automodapi.tests.example_module.slots | ||
|
||
.. autoclass:: DerivedParam | ||
:show-inheritance: | ||
|
||
.. rubric:: Methods Summary | ||
|
||
.. autosummary:: | ||
|
||
~DerivedParam.derived_from_slot_class_method | ||
|
||
.. rubric:: Methods Documentation | ||
|
||
.. automethod:: derived_from_slot_class_method |
27 changes: 27 additions & 0 deletions
27
...ts/output/api/sphinx_automodapi.tests.example_module.slots.DerivedSlotParam.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
DerivedSlotParam | ||
================ | ||
|
||
.. currentmodule:: sphinx_automodapi.tests.example_module.slots | ||
|
||
.. autoclass:: DerivedSlotParam | ||
:show-inheritance: | ||
|
||
.. rubric:: Attributes Summary | ||
|
||
.. autosummary:: | ||
|
||
~DerivedSlotParam.extra_attr | ||
|
||
.. rubric:: Methods Summary | ||
|
||
.. autosummary:: | ||
|
||
~DerivedSlotParam.derived_from_slot_class_method | ||
|
||
.. rubric:: Attributes Documentation | ||
|
||
.. autoattribute:: extra_attr | ||
|
||
.. rubric:: Methods Documentation | ||
|
||
.. automethod:: derived_from_slot_class_method |
29 changes: 29 additions & 0 deletions
29
...ases/slots/output/api/sphinx_automodapi.tests.example_module.slots.SlotDict.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
SlotDict | ||
======== | ||
|
||
.. currentmodule:: sphinx_automodapi.tests.example_module.slots | ||
|
||
.. autoclass:: SlotDict | ||
:show-inheritance: | ||
|
||
.. rubric:: Attributes Summary | ||
|
||
.. autosummary:: | ||
|
||
~SlotDict.class_attr | ||
~SlotDict.instance_attr | ||
|
||
.. rubric:: Methods Summary | ||
|
||
.. autosummary:: | ||
|
||
~SlotDict.my_method | ||
|
||
.. rubric:: Attributes Documentation | ||
|
||
.. autoattribute:: class_attr | ||
.. autoattribute:: instance_attr | ||
|
||
.. rubric:: Methods Documentation | ||
|
||
.. automethod:: my_method |
19 changes: 19 additions & 0 deletions
19
sphinx_automodapi/tests/cases/slots/output/index.rst.automodapi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
sphinx_automodapi.tests.example_module.slots Module | ||
--------------------------------------------------- | ||
|
||
.. automodule:: sphinx_automodapi.tests.example_module.slots | ||
|
||
Classes | ||
^^^^^^^ | ||
|
||
.. automodsumm:: sphinx_automodapi.tests.example_module.slots | ||
:classes-only: | ||
:toctree: api | ||
|
||
Class Inheritance Diagram | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
.. automod-diagram:: sphinx_automodapi.tests.example_module.slots | ||
:private-bases: | ||
:parts: 1 |
8 changes: 8 additions & 0 deletions
8
sphinx_automodapi/tests/cases/slots/output/index.rst.automodsumm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.. currentmodule:: sphinx_automodapi.tests.example_module.slots | ||
|
||
.. autosummary:: | ||
:toctree: api | ||
|
||
SlotDict | ||
DerivedParam | ||
DerivedSlotParam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
"""Test classes containing __slots__ | ||
Instance attributes named in ``__slots__`` can be introspected and are listed | ||
in the Attributes section of the class documentation. Class attributes are | ||
listed in the same section of the generated docs so docstrings should be used | ||
to distinguish class attributes vs instance attributes. Regular instance | ||
attributes are dynamically inserted into ``__dict__`` and cannot be reliably | ||
introspected so they're not included in the documentation. | ||
""" | ||
from __future__ import annotations | ||
|
||
__all__ = ['SlotDict', 'DerivedParam', 'DerivedSlotParam',] | ||
|
||
|
||
class SlotDict(object): | ||
""" | ||
A class that uses __slots__ and __dict__ for its attribute namespace. | ||
""" | ||
__slots__ = { | ||
"instance_attr": "instance attribute docstring can be added here", | ||
"__dict__": None, # Allows additional instance attributes to be added | ||
} | ||
|
||
class_attr = "class attribute value" | ||
"""(class attr) this is a class attribute.""" | ||
|
||
def __init__(self, param: str, other_param: str): | ||
""" | ||
Initializes a SlotDict object. | ||
Parameters | ||
---------- | ||
param : str | ||
A parameter | ||
other_param : str | ||
Another parameter | ||
""" | ||
|
||
self.instance_attr = param | ||
"""Instance attributes declared in slots can also define their docstring | ||
here | ||
""" | ||
|
||
if other_param is not None: | ||
self.other_attr = other_param | ||
"""This instance attribute is dynamic (not declared in a slot) so | ||
it's not included in the docs | ||
""" | ||
|
||
def my_method(self): | ||
""" | ||
Prints the SlotDict parameters. | ||
""" | ||
print(f"instance_attr: {self.instance_attr}") | ||
print(f"other_attr: {self.other_attr}") | ||
|
||
|
||
class DerivedParam(SlotDict): | ||
""" | ||
Extends SlotDict by adding an extra parameter | ||
""" | ||
def __init__(self, param: str, other_param: str, extra_param: str): | ||
""" | ||
Initializes a DerivedParam object. | ||
Parameters | ||
---------- | ||
param : str | ||
A parameter | ||
other_param : str | ||
Another parameter | ||
extra_param : str | ||
An extra parameter | ||
""" | ||
super(DerivedParam, self).__init__(param, other_param) | ||
self.extra_attr = extra_param | ||
|
||
def derived_from_slot_class_method(self): | ||
""" | ||
Prints the DerivedParam parameters. | ||
""" | ||
print(f"instance_attr: {self.instance_attr}") | ||
print(f"other_attr: {self.other_attr}") | ||
print(f"extra_attr: {self.extra_attr}") | ||
|
||
|
||
class DerivedSlotParam(SlotDict): | ||
""" | ||
Extends SlotDict by adding a slot parameter | ||
""" | ||
|
||
__slots__ = ('extra_attr',) | ||
|
||
def __init__(self, param: str, other_param: str, extra_param: str): | ||
""" | ||
Initializes a DerivedSlotParam object. | ||
Parameters | ||
---------- | ||
param : str | ||
A parameter | ||
other_param : str | ||
Another parameter | ||
extra_param : str | ||
An extra parameter | ||
""" | ||
super(DerivedSlotParam, self).__init__(param, other_param) | ||
self.extra_attr = extra_param | ||
|
||
def derived_from_slot_class_method(self): | ||
""" | ||
Prints the DerivedSlotParam parameters. | ||
""" | ||
print(f"instance_attr: {self.instance_attr}") | ||
print(f"other_attr: {self.other_attr}") | ||
print(f"extra_attr: {self.extra_attr}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters