Skip to content

Commit bf77ccc

Browse files
author
csaba.nemes
committed
add support for datalcass fields with no default value
1 parent cb8f5af commit bf77ccc

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

sphinx_automodapi/autodoc_enhancements.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Miscellaneous enhancements to help autodoc along.
33
"""
4-
4+
import dataclasses
55
import inspect
66
import sys
77
import types
@@ -68,7 +68,12 @@ def type_object_attrgetter(obj, attr, *defargs):
6868
return base.__dict__[attr]
6969
break
7070

71-
return getattr(obj, attr, *defargs)
71+
try:
72+
return getattr(obj, attr, *defargs)
73+
except AttributeError:
74+
# for dataclasses, get the attribute from the __dataclass_fields__
75+
if dataclasses.is_dataclass(obj):
76+
return obj.__dataclass_fields__[attr].name
7277

7378

7479
def setup(app):

sphinx_automodapi/automodsumm.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class members that are inherited from a base class. This value can be
8888
import os
8989
import re
9090
import io
91+
import dataclasses
9192

9293
from sphinx.util import logging
9394
from sphinx.ext.autosummary import Autosummary
@@ -551,11 +552,22 @@ def get_members_class(obj, typ, include_public=[],
551552
else:
552553
names = getattr(obj, '__dict__').keys()
553554

555+
# add dataclass_field names for dataclass classes
556+
if dataclasses.is_dataclass(obj):
557+
dataclass_fieldnames = getattr(obj, '__dataclass_fields__').keys()
558+
names = list(set(list(names) + list(dataclass_fieldnames)))
559+
554560
for name in names:
555561
try:
556562
documenter = get_documenter(app, safe_getattr(obj, name), obj)
557563
except AttributeError:
558-
continue
564+
# for dataclasses try to get the attribute from the __dataclass_fields__
565+
if dataclasses.is_dataclass(obj):
566+
try:
567+
attr = obj.__dataclass_fields__[name]
568+
documenter = get_documenter(app, attr, obj)
569+
except KeyError:
570+
continue
559571
if typ is None or documenter.objtype == typ:
560572
items.append(name)
561573
elif typ == 'attribute' and documenter.objtype == 'property':

0 commit comments

Comments
 (0)