Skip to content

Commit b57f55c

Browse files
authored
gh-103449: Fix a bug in dataclass docstring generation (#103454)
1 parent d83faf7 commit b57f55c

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

Lib/dataclasses.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1128,8 +1128,13 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
11281128

11291129
if not getattr(cls, '__doc__'):
11301130
# Create a class doc-string.
1131-
cls.__doc__ = (cls.__name__ +
1132-
str(inspect.signature(cls)).replace(' -> None', ''))
1131+
try:
1132+
# In some cases fetching a signature is not possible.
1133+
# But, we surely should not fail in this case.
1134+
text_sig = str(inspect.signature(cls)).replace(' -> None', '')
1135+
except (TypeError, ValueError):
1136+
text_sig = ''
1137+
cls.__doc__ = (cls.__name__ + text_sig)
11331138

11341139
if match_args:
11351140
# I could probably compute this once

Lib/test/test_dataclasses.py

+13
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,19 @@ class C:
22972297

22982298
self.assertDocStrEqual(C.__doc__, "C(x:collections.deque=<factory>)")
22992299

2300+
def test_docstring_with_no_signature(self):
2301+
# See https://github.com/python/cpython/issues/103449
2302+
class Meta(type):
2303+
__call__ = dict
2304+
class Base(metaclass=Meta):
2305+
pass
2306+
2307+
@dataclass
2308+
class C(Base):
2309+
pass
2310+
2311+
self.assertDocStrEqual(C.__doc__, "C")
2312+
23002313

23012314
class TestInit(unittest.TestCase):
23022315
def test_base_has_init(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug in doc string generation in :func:`dataclasses.dataclass`.

0 commit comments

Comments
 (0)