Skip to content

Commit ec29d0c

Browse files
[3.11] gh-103449: Fix a bug in dataclass docstring generation (GH-103454) (#103599)
1 parent 6e25228 commit ec29d0c

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

Lib/dataclasses.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,8 +1092,13 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
10921092

10931093
if not getattr(cls, '__doc__'):
10941094
# Create a class doc-string.
1095-
cls.__doc__ = (cls.__name__ +
1096-
str(inspect.signature(cls)).replace(' -> None', ''))
1095+
try:
1096+
# In some cases fetching a signature is not possible.
1097+
# But, we surely should not fail in this case.
1098+
text_sig = str(inspect.signature(cls)).replace(' -> None', '')
1099+
except (TypeError, ValueError):
1100+
text_sig = ''
1101+
cls.__doc__ = (cls.__name__ + text_sig)
10971102

10981103
if match_args:
10991104
# I could probably compute this once

Lib/test/test_dataclasses.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,6 +2224,19 @@ class C:
22242224

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

2227+
def test_docstring_with_no_signature(self):
2228+
# See https://github.com/python/cpython/issues/103449
2229+
class Meta(type):
2230+
__call__ = dict
2231+
class Base(metaclass=Meta):
2232+
pass
2233+
2234+
@dataclass
2235+
class C(Base):
2236+
pass
2237+
2238+
self.assertDocStrEqual(C.__doc__, "C")
2239+
22272240

22282241
class TestInit(unittest.TestCase):
22292242
def test_base_has_init(self):
Lines changed: 1 addition & 0 deletions
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)