Skip to content
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

Unable to autoreload @dataclass(frozen=True) #12185

Open
Techcable opened this issue Mar 11, 2020 · 2 comments
Open

Unable to autoreload @dataclass(frozen=True) #12185

Techcable opened this issue Mar 11, 2020 · 2 comments

Comments

@Techcable
Copy link

Techcable commented Mar 11, 2020

I think you can bypass the FrozenInstanceError by directly invoking object.__setattr__

[autoreload of pitch_detection failed: Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/IPython/extensions/autoreload.py", line 245, in check
    superreload(m, reload, self.old_objects)
  File "/usr/lib/python3.8/site-packages/IPython/extensions/autoreload.py", line 410, in superreload
    update_generic(old_obj, new_obj)
  File "/usr/lib/python3.8/site-packages/IPython/extensions/autoreload.py", line 347, in update_generic
    update(a, b)
  File "/usr/lib/python3.8/site-packages/IPython/extensions/autoreload.py", line 317, in update_class
    update_instances(old, new)
  File "/usr/lib/python3.8/site-packages/IPython/extensions/autoreload.py", line 280, in update_instances
    ref.__class__ = new
  File "<string>", line 4, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field '__class__'
]
@Carreau
Copy link
Member

Carreau commented Mar 11, 2020

Hummm... If you know what you are doing then we'll happily take a Pull Request.

@kvjrhall
Copy link

kvjrhall commented Dec 9, 2020

I have observed this in vscode, in particular.

Let us say that we have a frozen dataclass in some user-specified module usermodule (I haven't tested with any 3rd party packages) along with other code currently in development.

from dataclasses import dataclass

@dataclass
class some_frozen_class:
    a: int

def algorithm(a: int) -> int:
    return a + 2

In a notebook cell we have the following

from usermodule import algorithm

display(algorithm(2))

Next we modify algorithm (or any code in that python file)

from dataclasses import dataclass

@dataclass
class some_frozen_class:
    a: int

def algorithm(a: int) -> int:
    return a + 3   # a change

...and re-run the cell. I've been seeing two things: (1) the autoreload error as reported by @Techcable; and (2) the cell continues to run despite the error, with unpredictable behavior (i.e., code may or may not be changed, etc). Restarting the python interpreter results in the error disappearing and code running as expected for the first run.

hhoppe added a commit to hhoppe/ipython that referenced this issue Aug 15, 2022
See Issue ipython#12411 and Issue ipython#12185.
The problem is that a frozen `dataclasses.dataclass` overrides the `__setattr__()` method, so updating its `__class__` member requires going through the base `object` class.  This seems to fix the problem.
Carreau pushed a commit to hhoppe/ipython that referenced this issue Aug 17, 2022
See Issue ipython#12411 and Issue ipython#12185.
The problem is that a frozen `dataclasses.dataclass` overrides the `__setattr__()` method, so updating its `__class__` member requires going through the base `object` class.  This seems to fix the problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants