From ec29d0c0916af129eaafb0ad392449ca173309ed Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Sun, 23 Apr 2023 08:40:52 -0700 Subject: [PATCH] [3.11] gh-103449: Fix a bug in dataclass docstring generation (GH-103454) (#103599) --- Lib/dataclasses.py | 9 +++++++-- Lib/test/test_dataclasses.py | 13 +++++++++++++ .../2023-04-11-21-38-39.gh-issue-103449.-nxmhb.rst | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-04-11-21-38-39.gh-issue-103449.-nxmhb.rst diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 221ea2ba3e77c3..a7e0547705ca28 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -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 diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index abe02e3b1ab6df..1aaa78db4b0af8 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -2224,6 +2224,19 @@ class C: self.assertDocStrEqual(C.__doc__, "C(x:collections.deque=)") + 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): diff --git a/Misc/NEWS.d/next/Library/2023-04-11-21-38-39.gh-issue-103449.-nxmhb.rst b/Misc/NEWS.d/next/Library/2023-04-11-21-38-39.gh-issue-103449.-nxmhb.rst new file mode 100644 index 00000000000000..0b2b47af1cbaab --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-11-21-38-39.gh-issue-103449.-nxmhb.rst @@ -0,0 +1 @@ +Fix a bug in doc string generation in :func:`dataclasses.dataclass`.