Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
sobolevn wants to merge3 commits intopython:mainfromsobolevn:issue-128184

Conversation

@sobolevn
Copy link
Member

@sobolevnsobolevn commentedDec 23, 2024
edited by bedevere-appbot
Loading

# 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','')
exceptNameError:

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
MemberAuthor

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:

deftest_docstring_two_fields(self):@dataclassclassC:x:inty:intself.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
MemberAuthor

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 withoutNameError 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
MemberAuthor

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 ofinspect.signature. Please take a look.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@ericvsmithericvsmithAwaiting requested review from ericvsmithericvsmith is a code owner

@JelleZijlstraJelleZijlstraAwaiting requested review from JelleZijlstra

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@sobolevn@JelleZijlstra

[8]ページ先頭

©2009-2025 Movatter.jp