Skip to content

Commit

Permalink
[3.11] gh-103449: Fix a bug in dataclass docstring generation (GH-103454
Browse files Browse the repository at this point in the history
  • Loading branch information
miss-islington committed Apr 23, 2023
1 parent 6e25228 commit ec29d0c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Lib/dataclasses.py
Expand Up @@ -1092,8 +1092,13 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,

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

if match_args:
# I could probably compute this once
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_dataclasses.py
Expand Up @@ -2224,6 +2224,19 @@ class C:

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

def test_docstring_with_no_signature(self):
# See https://github.com/python/cpython/issues/103449
class Meta(type):
__call__ = dict
class Base(metaclass=Meta):
pass

@dataclass
class C(Base):
pass

self.assertDocStrEqual(C.__doc__, "C")


class TestInit(unittest.TestCase):
def test_base_has_init(self):
Expand Down
@@ -0,0 +1 @@
Fix a bug in doc string generation in :func:`dataclasses.dataclass`.

0 comments on commit ec29d0c

Please sign in to comment.