This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: RFE: @dataclasses.dataclass(slots=True) doesn't support methods using closures
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, eric.smith, eric.snow, frenzy, hynek, petr.viktorin, tinchester, vstinner
Priority: normal Keywords:

Created on 2022-01-16 22:48 by tinchester, last changed 2022-04-11 14:59 by admin.

Messages (8)
msg410730 - (view) Author: Tin Tvrtković (tinchester) * Date: 2022-01-16 22:48
We've received a report over at the attrs issue tracker about our test suite failing on Python 3.11. Here's the link: https://github.com/python-attrs/attrs/issues/907

It turns out to be an issue with the no-arg `super()` calls in slotted classes. Here's a minimal reproducer example:

```
from attrs import define


@define
class A:
    pass


@define
class B(A):
    def test(self):
        super()


B().test()
```

```
Traceback (most recent call last):
  File "/Users/tintvrtkovic/pg/attrs/a01.py", line 15, in <module>
    B().test()
    ^^^^^^^^^^
  File "/Users/tintvrtkovic/pg/attrs/a01.py", line 12, in test
    super()
    ^^^^^^^
TypeError: super(type, obj): obj must be an instance or subtype of type
```
This is a known issue for which we have implemented workarounds. The workarounds aren't effective for 3.11 though. I have implemented a fix in attrs (https://github.com/python-attrs/attrs/pull/910), but I still thought I'd post this here to maybe get the core devs opinion.

Dataclasses exhibit the exact same issue when used with `slots=True`, both in 3.10 when `slots` was added and in 3.11. I guess no one reported it or tried fixing it.

A comprehensive description of the issue follows: since it's impossible to add *slotness* (i.e. set `__slots__`) to a class after it has been created, when creating a slotted class the class decorators in attrs and dataclasses actually replace the class they are applied to with a copy of it, with slots added. This works, except in the case of the no-arg `super()` being used in any of the class methods (and maybe another edge case that I can't remember). When the compiler encounters the no-arg `super()` form, it adds some state to the function `__closure__` cells. This state causes the exception shown above, since it's incorrect when the class gets replaced.

So these closure cells need to be rewritten when the class is replaced. In Python versions prior to 3.11, the closure cells were immutable so extra effort was needed to rewrite them. The functions are here: https://github.com/python-attrs/attrs/blob/9727008fd1e40bc55cdc6aee71e0f61553f33127/src/attr/_compat.py#L145.

In 3.11, our old closure cell rewriting doesn't work any more, but closure cells don't appear to be immutable either, so the fix in my attr PR linked above is simple. Still, it's another branch in the code to support a specific version.

I don't know if there's anything actionable here for Python, apart from confirming or denying if this behavior is expected.
msg410745 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-01-17 06:55
bisected to here:

631f9938b1604d4f893417ec339b9e0fa9196fb1 is the first new commit
commit 631f9938b1604d4f893417ec339b9e0fa9196fb1
Author: Eric Snow <ericsnowcurrently@gmail.com>
Date:   Mon Jun 7 16:52:00 2021 -0600

    bpo-43693: Add the MAKE_CELL opcode and interleave fast locals offsets. (gh-26396)

    This moves logic out of the frame initialization code and into the compiler and eval loop.  Doing so simplifies the runtime code and allows us to optimize it better.

    https://bugs.python.org/issue43693
msg411462 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2022-01-24 10:43
I guess at least there should be a warning about this in dataclasses docs?
The reproducer with dataclasses (which exhibits the same error on 3.10 and 3.11):

import dataclasses

@dataclasses.dataclass(slots=True)
class A:
    pass


@dataclasses.dataclass(slots=True)
class B(A):
    def test(self):
        super()

B().test()
msg415291 - (view) Author: Lumír Balhar (frenzy) * Date: 2022-03-15 20:17
In my opinion, we should keep it simple for attrs and dataclasses to fix closure cells when a class is replaced and therefore it seems to be correct to have it mutable as it currently is in 3.11.

My plan is to implement the fix for dataclasses and some tests for these use cases so the behavior should not change in the future. For attrs it means one more branch in the code now but much simpler code when the support for older releases gets dropped.

Any other opinions?
msg415300 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-15 21:44
@frenzy: I'm not sure what your fix would do. You could either describe it in rough terms (if you'd like a pre-PR opinion on the approach), or I'm happy to wait to see your PR.
msg415303 - (view) Author: Lumír Balhar (frenzy) * Date: 2022-03-15 22:50
We have the same problem reported in attrs here in dataclasses and because it's not tested the way to manipulate __closure__ cells changes frequently.

My plan is to implement something similar to this into dataclasses: https://github.com/python-attrs/attrs/blob/5c040f30e3e4b3c9c0f27c8ac6ff13d604c1818c/src/attr/_make.py#L895-L916

Basically, when a new dataclass is created (with slots=True), look for references to the original class and fix them.

This fixes the problem reported to attrs in dataclasses and when we fix it and add some tests for it, the future behavior should be more stable.

What do you think?
msg416169 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-03-28 13:53
I changed the issue title to focus this issue on enhance dataclasses to support @dataclasses.dataclass(slots=True) on methods using closures: it would be a new Python 3.11 feature.


I created https://bugs.python.org/issue47143 "Add functools.copy_class() which updates closures".
msg416174 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-03-28 15:16
See also bpo-45520: "Frozen dataclass deep copy doesn't work with __slots__" which is related but a different issue.
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90562
2022-03-28 15:16:37vstinnersetmessages: + msg416174
2022-03-28 13:53:30vstinnersettitle: 3.11a4: a small attrs regression -> RFE: @dataclasses.dataclass(slots=True) doesn't support methods using closures
nosy: + vstinner

messages: + msg416169

type: behavior -> enhancement
2022-03-15 22:50:14frenzysetmessages: + msg415303
2022-03-15 21:44:20eric.smithsetmessages: + msg415300
2022-03-15 20:17:55frenzysetmessages: + msg415291
2022-01-24 10:43:44petr.viktorinsetnosy: + petr.viktorin
messages: + msg411462
2022-01-17 06:55:06Dennis Sweeneysetnosy: + eric.snow, Dennis Sweeney
messages: + msg410745
2022-01-17 05:42:50frenzysetnosy: + frenzy
2022-01-17 05:08:26hyneksetnosy: + hynek
2022-01-16 23:44:03eric.smithsetnosy: + eric.smith
2022-01-16 22:48:06tinchestercreate