gh-128184: Fix docstring generation in dataclasses with forward refs#128194
Closed
sobolevn wants to merge 3 commits intopython:mainfrom
Closed
gh-128184: Fix docstring generation in dataclasses with forward refs#128194sobolevn wants to merge 3 commits intopython:mainfrom
sobolevn wants to merge 3 commits intopython:mainfrom
Conversation
| # 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: |
Member
There was a problem hiding this comment.
Why not just start with SOURCE format? I don't think we need to catch NameError first.
Member
Author
There was a problem hiding this comment.
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.
Member
Author
There was a problem hiding this comment.
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.
Member
There was a problem hiding this comment.
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
__init__annotation eagerly evaluated #128184