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-133956 fix bug whereClassVar string annotation in@dataclass caused incorrect__init__ generation#134073

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

Open
dzherb wants to merge3 commits intopython:main
base:main
Choose a base branch
Loading
fromdzherb:gh-133956-broken-classvar

Conversation

dzherb
Copy link

@dzherbdzherb commentedMay 15, 2025
edited by bedevere-appbot
Loading

Thanks to@JelleZijlstra for pointing out a possible fix! I added an additional check, but the initial draft was nearly complete.

Comment on lines +770 to +771
if not cls_module:
return False

Choose a reason for hiding this comment

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

It is interesting that we're paranoid about the dataclass not having its module imported on only this branch and not the branch above. Not suggesting a change, but a comment could be useful if you know why we only care in theelse branch and not theif not module_name branch.

Copy link
Author

Choose a reason for hiding this comment

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

I was following the checks that were already present in the function. In theelse branch, there was a check forif module:

module=sys.modules.get(cls.__module__)ifmoduleandmodule.__dict__.get(module_name)isa_module:ns=sys.modules.get(a_type.__module__).__dict__

I’d actually consider removing that check — it does seem unnecessary. At least, I can’t think of a case wherecls.__module__ would be missing fromsys.modules. And the tests are still passing.

emcd reacted with thumbs up emoji

Choose a reason for hiding this comment

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

Yes, I saw the original code. My suggestion would be to hoistmodule = sys.modules.get(cls.__module__) out of theif..else. If you want to preserve the rather paranoid sanity check alongside the hoisted assignment, you could. Something like:

module=sys.modules.get(cls.__module__)ifmodule:# not sure that we need to be this paranoidns=module.__dict__ifmodule_name:...

dzherb reacted with thumbs up emoji
if (
isinstance(a_type_module, types.ModuleType)
# Handle cases when a_type is not defined in
# the referenced module, e.g. 'dataclasses.ClassVar[int]'

Choose a reason for hiding this comment

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

InitVar lives indataclasses,ClassVar lives intyping. Using an example ofdataclasses.ClassVar is likely to cause confusion.

return False

ns = None
module_name = match.group(1)

Choose a reason for hiding this comment

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

There is an edge case that is not handled by this:foo.typing_filtered.ClassVar. The regex only accounts for one module level. While it is probably uncommon to importfoo.typing_filtered without aliasing it, it is possible.

I don't want to change the world for a simple bug fix, but it seems like partitioning on. is more robust (and probably more performant) than using the current regex.

Copy link
Author

Choose a reason for hiding this comment

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

While I agree that splitting on a dot would be faster, there are existing tests that cover cases likedataclasses.InitVar.[int] anddataclasses.InitVar+. If we want to support multiple module levels, we should extend the current regex pattern rather than replace it.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's worth creating a new issue to track more levels of module nesting. And I think it's a mistake to test fordataclasses.InitVar.[int] and other invalid strings.

Choose a reason for hiding this comment

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

This bug was actually caught by__.typx.ClassVar in the real world. I typically stuff all of my common imports, includingtyping_extensions astypx, into an internals subpackage (__) so that I can dofrom . import __. Reduces module namespace pollution and lets me avoid setting__all__ to define module interfaces. But, I understand that my practice may not be common.

That said, any fix to multi-level traversal here or another PR is going to affect the code that is actually being fixed. Imo, it would make more sense to fix both together holistically (to save developer effort), if there is any interest in actually fixing the multi-level traversal.

Comment on lines +774 to +780
if (
isinstance(a_type_module, types.ModuleType)
# Handle cases when a_type is not defined in
# the referenced module, e.g. 'dataclasses.ClassVar[int]'
and a_type_module.__dict__.get(type_name) is a_type
):
ns = sys.modules.get(a_type.__module__).__dict__

Choose a reason for hiding this comment

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

I think this could be replaced with:

return (isinstance(a_type_module,types.ModuleType)andis_type_predicate(a_type_module.__dict__.get(type_name),a_module))

Copy link
Author

Choose a reason for hiding this comment

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

It looks like we can indeed replace it. But if we go with this version, thea_type parameter will no longer be used in the function body.

Also, I was thinking — in the original version, this line:

sys.modules.get(a_type.__module__)

could probably be replaced witha_module? Or am I missing something? It seems likea_type might not have been necessary from the start, and we should probably remove it.

Copy link
Member

Choose a reason for hiding this comment

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

If you think the code can be simplified, please do so, then I'll review that version.

dzherb reacted with thumbs up emoji
@@ -0,0 +1 @@
Fix bug where ``ClassVar`` string annotation in :func:`@dataclass <dataclasses.dataclass>` caused incorrect __init__ generation
Copy link
Member

@ericvsmithericvsmithMay 19, 2025
edited
Loading

Choose a reason for hiding this comment

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

I'm just starting to look at this PR, and will have more to say later. For now: I'd prefer this to say something more specific, like<some specific description> of ClassVar not recognized as typing.ClassVar. I'd like this because I'm sure we won't be fixing all instances of using ClassVar as string annotations, and we should mention the exact one being fixed here. Also, I don't want to mention__init__, because that's not the only method affected by this bug.

dzherb and JelleZijlstra reacted with thumbs up emoji
@bedevere-app
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

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

@emcdemcdemcd left review comments

@ericvsmithericvsmithericvsmith requested changes

Assignees

@ericvsmithericvsmith

Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@dzherb@emcd@ericvsmith@ZeroIntensity

[8]ページ先頭

©2009-2025 Movatter.jp