FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-142214: Fix two regressions in dataclasses by JelleZijlstra · Pull Request #142223 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 10 additions & 4 deletions Lib/dataclasses.py
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
Expand Up @@ -550,7 +550,12 @@ def __annotate__(format, /):

new_annotations = {}
for k in annotation_fields:
new_annotations[k] = cls_annotations[k]
# gh-142214: The annotation may be missing in unusual dynamic cases.
# If so, just skip it.
try:
new_annotations[k] = cls_annotations[k]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Don't in and [] both do a lookup? Maybe it would be better as:

if ann := cls_annotations.get(k):
    new_annotations[k] = ann

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I believe that None is a valid annotation here so this would miss None values?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Correct. To save a lookup we could do a try-except KeyError instead. I don't have a preference but I can switch to that if you like.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think try/except makes sense as in most cases the key should be there. The key not being there should only be in cases where something has modified annotations and removed things after the class has been processed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Done

except KeyError:
pass

if return_type is not MISSING:
if format == Format.STRING:
Expand Down Expand Up @@ -1399,9 +1404,10 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
f.type = ann

# Fix the class reference in the __annotate__ method
init_annotate = newcls.__init__.__annotate__
if getattr(init_annotate, "__generated_by_dataclasses__", False):
_update_func_cell_for__class__(init_annotate, cls, newcls)
init = newcls.__init__
if init_annotate := getattr(init, "__annotate__", None):
if getattr(init_annotate, "__generated_by_dataclasses__", False):
_update_func_cell_for__class__(init_annotate, cls, newcls)

return newcls

Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_dataclasses/__init__.py
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
Expand Up @@ -927,6 +927,20 @@ class C:

validate_class(C)

def test_incomplete_annotations(self):
# gh-142214
@dataclass
class C:
"doc" # needed because otherwise we fetch the annotations at the wrong time
x: int

C.__annotate__ = lambda _: {}

self.assertEqual(
annotationlib.get_annotations(C.__init__),
{"return": None}
)

def test_missing_default(self):
# Test that MISSING works the same as a default not being
# specified.
Expand Down Expand Up @@ -2578,6 +2592,20 @@ def __init__(self, x: int) -> None:

self.assertFalse(hasattr(E.__init__.__annotate__, "__generated_by_dataclasses__"))

def test_slots_true_init_false(self):
# Test that slots=True and init=False work together and
# that __annotate__ is not added to __init__.

@dataclass(slots=True, init=False)
class F:
x: int

f = F()
f.x = 10
self.assertEqual(f.x, 10)

self.assertFalse(hasattr(F.__init__, "__annotate__"))

def test_init_false_forwardref(self):
# Test forward references in fields not required for __init__ annotations.

Expand Down
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,12 @@
Fix two regressions in :mod:`dataclasses` in Python 3.14.1 related to
annotations.

* An exception is no longer raised if ``slots=True`` is used and the
``__init__`` method does not have an ``__annotate__`` attribute
(likely because ``init=False`` was used).

* An exception is no longer raised if annotations are requested on the
``__init__`` method and one of the fields is not present in the class
annotations. This can occur in certain dynamic scenarios.

Patch by Jelle Zijlstra.
Loading

Back | FazBrowse Home | New Git URL