-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-90562: Fix super()
without args calls for dataclasses
with slots
#111538
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1212,6 +1212,7 @@ def _add_slots(cls, is_frozen, weakref_slot): | |
|
||
# And finally create the class. | ||
qualname = getattr(cls, '__qualname__', None) | ||
old_cls = cls | ||
cls = type(cls)(cls.__name__, cls.__bases__, cls_dict) | ||
if qualname is not None: | ||
cls.__qualname__ = qualname | ||
|
@@ -1223,6 +1224,35 @@ def _add_slots(cls, is_frozen, weakref_slot): | |
if '__setstate__' not in cls_dict: | ||
cls.__setstate__ = _dataclass_setstate | ||
|
||
# The following is a fix for | ||
# https://github.com/python/cpython/issues/111500 | ||
# The code is copied-and-modified from https://github.com/python-attrs/attrs | ||
# All credits for it goes to the `attrs` team and contributors. | ||
# | ||
# If a method mentions `__class__` or uses the no-arg super(), the | ||
# compiler will bake a reference to the class in the method itself | ||
# as `method.__closure__`. Since we replace the class with a | ||
# clone, we rewrite these references so it keeps working. | ||
for item in cls.__dict__.values(): | ||
item = inspect.unwrap(item) | ||
if isinstance(item, (classmethod, staticmethod)): | ||
closure_cells = getattr(item.__func__, "__closure__", None) | ||
Comment on lines
+1238
to
+1239
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.
|
||
elif isinstance(item, property): | ||
closure_cells = getattr(item.fget, "__closure__", None) | ||
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. What if a getter doesn't have a closure, but a setter or deleter does? |
||
else: | ||
closure_cells = getattr(item, "__closure__", None) | ||
|
||
if not closure_cells: | ||
continue | ||
for cell in closure_cells: | ||
try: | ||
match = cell.cell_contents is old_cls | ||
except ValueError: # Cell is empty | ||
pass | ||
else: | ||
if match: | ||
cell.cell_contents = cls | ||
|
||
return cls | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix ``super()`` call without arguments for :mod:`dataclasses` with | ||
``slots=True``. |
Uh oh!
There was an error while loading. Please reload this page.