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

Support for non-default dataclass fields #116

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion sphinx_automodapi/autodoc_enhancements.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Miscellaneous enhancements to help autodoc along.
"""
import dataclasses

from sphinx.ext.autodoc import AttributeDocumenter

__all__ = []
Expand Down Expand Up @@ -58,7 +60,19 @@ def type_object_attrgetter(obj, attr, *defargs):
return base.__dict__[attr]
break

return getattr(obj, attr, *defargs)
try:
return getattr(obj, attr, *defargs)
except AttributeError as e:
Copy link
Member

Choose a reason for hiding this comment

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

extreme nitpick: it's best to avoid single-letter variables and use something descriptive, even when the single letter feels trivial.

# for dataclasses, get the attribute from the __dataclass_fields__
if dataclasses.is_dataclass(obj):
if attr in obj.__dataclass_fields__:
return obj.__dataclass_fields__[attr].name
else:
# raise original AttributeError
raise e
else:
# raise original AttributeError
raise e
Comment on lines +67 to +75
Copy link
Member

Choose a reason for hiding this comment

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

The logic here could be simplified a bit and all put into one conditional and a raise outside of it.



def setup(app):
Expand Down
14 changes: 13 additions & 1 deletion sphinx_automodapi/automodsumm.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class members that are inherited from a base class. This value can be
import inspect
import os
import re
import dataclasses

from sphinx.util import logging
from sphinx.ext.autosummary import Autosummary
Expand Down Expand Up @@ -548,11 +549,22 @@ def get_members_class(obj, typ, include_public=[],
else:
names = getattr(obj, '__dict__').keys()

# add dataclass_field names for dataclass classes
if dataclasses.is_dataclass(obj):
dataclass_fieldnames = getattr(obj, '__dataclass_fields__').keys()
names = list(set(list(names) + list(dataclass_fieldnames)))

for name in names:
try:
documenter = get_documenter(app, safe_getattr(obj, name), obj)
except AttributeError:
continue
# for dataclasses try to get the attribute from the __dataclass_fields__
if dataclasses.is_dataclass(obj):
try:
attr = obj.__dataclass_fields__[name]
documenter = get_documenter(app, attr, obj)
except KeyError:
continue
if typ is None or documenter.objtype == typ:
items.append(name)
elif typ == 'attribute' and documenter.objtype == 'property':
Expand Down
17 changes: 17 additions & 0 deletions sphinx_automodapi/tests/test_autodoc_enhancements.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,20 @@ def test_type_attrgetter():

assert type_object_attrgetter(MyClass, 'susy', 'default') == 'default'
assert type_object_attrgetter(MyClass, '__dict__') == MyClass.__dict__


def test_type_attrgetter_for_dataclass():
"""
This tests the attribute getter for non-default dataclass fields
"""
import dataclasses

@dataclasses.dataclass
class MyDataclass:
foo: int
bar: str = "bar value"

with pytest.raises(AttributeError):
getattr(MyDataclass, 'foo')
assert type_object_attrgetter(MyDataclass, 'foo') == 'foo'
assert getattr(MyDataclass, 'bar') == 'bar value'