-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-89687: fix get_type_hints with dataclasses __init__ generation #137168
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
Open
Tishka17
wants to merge
2
commits into
python:main
Choose a base branch
from
Tishka17:issue-45524
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -536,10 +536,25 @@ def _field_assign(frozen, name, value, self_name): | |
return f' {self_name}.{name}={value}' | ||
|
||
|
||
def _field_init(f, frozen, globals, self_name, slots): | ||
def _field_init(f, frozen, globals, self_name, slots, module): | ||
# Return the text of the line in the body of __init__ that will | ||
# initialize this field. | ||
|
||
if f.init and isinstance(f.type, str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like this is good, but I'd do it like this:
ForwardRef now lives in annotationlib, not typing. There's no need to catch SyntaxError as it doesn't parse its input in the constructor. |
||
from typing import ForwardRef # `typing` is a heavy import | ||
# We need to resolve this string type into a real `ForwardRef` object, | ||
# because otherwise we might end up with unsolvable annotations. | ||
# For example: | ||
# def __init__(self, d: collections.OrderedDict) -> None: | ||
# We won't be able to resolve `collections.OrderedDict` | ||
# with wrong `module` param, when placed in a different module. #45524 | ||
try: | ||
f.type = ForwardRef(f.type, module=module, is_class=True) | ||
except SyntaxError: | ||
# We don't want to fail class creation | ||
# when `ForwardRef` cannot be constructed. | ||
pass | ||
|
||
default_name = f'__dataclass_dflt_{f.name}__' | ||
if f.default_factory is not MISSING: | ||
if f.init: | ||
|
@@ -616,7 +631,7 @@ def _init_param(f): | |
|
||
|
||
def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init, | ||
self_name, func_builder, slots): | ||
self_name, func_builder, slots, module): | ||
# fields contains both real fields and InitVar pseudo-fields. | ||
|
||
# Make sure we don't have fields without defaults following fields | ||
|
@@ -643,7 +658,7 @@ def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init, | |
|
||
body_lines = [] | ||
for f in fields: | ||
line = _field_init(f, frozen, locals, self_name, slots) | ||
line = _field_init(f, frozen, locals, self_name, slots, module) | ||
# line is None means that this field doesn't require | ||
# initialization (it's a pseudo-field). Just skip it. | ||
if line: | ||
|
@@ -1093,6 +1108,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, | |
else 'self', | ||
func_builder, | ||
slots, | ||
cls.__module__, | ||
) | ||
|
||
_set_new_attribute(cls, '__replace__', _replace) | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from __future__ import annotations | ||
|
||
import dataclasses | ||
|
||
# We need to be sure that `Foo` is not in scope | ||
from test.test_dataclasses import dataclass_textanno | ||
|
||
|
||
class Custom: | ||
pass | ||
|
||
|
||
@dataclasses.dataclass | ||
class Child(dataclass_textanno.Bar): | ||
custom: Custom | ||
|
||
|
||
class Foo: # matching name with `dataclass_testanno.Foo` | ||
pass | ||
|
||
|
||
@dataclasses.dataclass | ||
class WithMatchingNameOverride(dataclass_textanno.Bar): | ||
foo: Foo # Existing `foo` annotation should be overridden | ||
|
||
|
||
@dataclasses.dataclass(init=False) | ||
class WithFutureInit(Child): | ||
def __init__(self, foo: dataclass_textanno.Foo, custom: Custom) -> None: | ||
pass |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2025-07-28-14-24-07.gh-issue-89687.7uW1c8.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix ``typing.get_type_hints()`` failure on ``@dataclass`` hierarchies in different modules. |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is correct, since we infer globals from
__forward_module__
if no explicit globals are given. And if explicit globals are given, we shouldn't be using__forward_module__
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think current behavior is correct. Was there any discussion?