Skip to content

gh-128184: Fix docstring generation in dataclasses with forward refs #128194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,13 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
# 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 NameError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just start with SOURCE format? I don't think we need to catch NameError first.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we do that, a lot of tests fail with similar messages:

    def test_docstring_two_fields(self):
        @dataclass
        class C:
            x: int
            y: int

        self.assertDocStrEqual(C.__doc__, "C(x:int, y:int)")

Produces:

======================================================================
FAIL: test_docstring_two_fields (test.test_dataclasses.TestDocString.test_docstring_two_fields)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython2/Lib/test/test_dataclasses/__init__.py", line 2294, in test_docstring_two_fields
    self.assertDocStrEqual(C.__doc__, "C(x:int, y:int)")
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython2/Lib/test/test_dataclasses/__init__.py", line 2263, in assertDocStrEqual
    self.assertEqual(a.replace(' ', ''), b.replace(' ', ''))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: "C(x:'__dataclass_type_x__',y:'__dataclas[46 chars]e__'" != 'C(x:int,y:int)'
- C(x:'__dataclass_type_x__',y:'__dataclass_type_y__')->'__dataclass___init___return_type__'
+ C(x:int,y:int)

I am not quite sure - why.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that again and it still does not work without NameError handling.

This happens because of:

locals = {**{f'__dataclass_type_{f.name}__': f.type for f in fields},
              **{'__dataclass_HAS_DEFAULT_FACTORY__': _HAS_DEFAULT_FACTORY,
                 '__dataclass_builtins_object__': object,
                 }
              }

So, I am not sure that this is totally correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JelleZijlstra what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this more and came up with a more general improvement: #130815. This also makes the docstring cleaner for dataclasses with no __init__ that contain unresolved names, and it improves other uses of inspect.signature. Please take a look.

# This means that some types where not defined, maybe due to
# forward references, etc. In this case, try different format.
text_sig = str(inspect.signature(
cls,
annotation_format=annotationlib.Format.STRING,
)).replace(" -> 'None'", '')
except (TypeError, ValueError):
text_sig = ''
cls.__doc__ = (cls.__name__ + text_sig)
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import types
import weakref
import traceback
import textwrap
import unittest
from unittest.mock import Mock
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol, DefaultDict
Expand Down Expand Up @@ -2343,6 +2344,24 @@ class C:

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

def test_docstring_with_unsolvable_forward_ref_in_init(self):
# See: https://github.com/python/cpython/issues/128184
ns = {}
exec(
textwrap.dedent(
"""
from dataclasses import dataclass

@dataclass
class C:
def __init__(self, x: X, num: int) -> None: ...
""",
),
ns,
)

self.assertDocStrEqual(ns['C'].__doc__, "C(x:'X',num:'int')")

def test_docstring_with_no_signature(self):
# See https://github.com/python/cpython/issues/103449
class Meta(type):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes :exc:`NameError` when using :func:`dataclasses.dataclass` on classes
with unresolvable forward references.
Loading