You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After #253 (comment), I started examinating these lines. I think the original use-case was to handle classes like namedtuple overloading __dict__.
(even though the test suite passes for all python versions if simply drop the __dict__ item out of clsdict).
The first one line is necessary because updating a __dict__ field in the __dict__ of a class is simply not possible. Indeed, __dict__ attributes are proxies objects and do not support direct item assigment like __dict__[k] = v. Instead, the canonical way to update a class __dict__ is to do setattr(cls, key, value). However, here, key is __dict__, so python thinks we actually want to override the __dict__ attribute of cls, and not a __dict__ field of the __dict__.
However, including __dict__ in the type_kwargs only if it is a property object is kind of arbitrary (probably related to namedtuples). Take this example for example, where we loose the __dict__ attribute:
In [1]: importgc
...: importcloudpickle
...:
...: classA:
...: __dict__= {'some_attribute':1}
...: a=A()
...: print(f'calling __dict__ on the original A instance: {a.__dict__}')
...:
...: s=cloudpickle.dumps(a)
...: delA
...: dela
...: gc.collect()
...: depickled_a=cloudpickle.loads(s)
...: print(f'calling __dict__ on the depickled A instance: {depickled_a.__dict__}')
calling__dict__ontheoriginalAinstance: {'some_attribute': 1}
calling__dict__onthedepickledAinstance: {}
The text was updated successfully, but these errors were encountered:
After #253 (comment), I started examinating these lines. I think the original use-case was to handle classes like
namedtuple
overloading__dict__
.(even though the test suite passes for all python versions if simply drop the
__dict__
item out ofclsdict
).cloudpickle/cloudpickle/cloudpickle.py
Lines 633 to 635 in 167e163
In more detail:
__dict__
field in the__dict__
of a class is simply not possible. Indeed,__dict__
attributes are proxies objects and do not support direct item assigment like__dict__[k] = v
. Instead, the canonical way to update a class__dict__
is to dosetattr(cls, key, value)
. However, here,key
is__dict__
, so python thinks we actually want to override the__dict__
attribute ofcls
, and not a__dict__
field of the__dict__
.__dict__
in thetype_kwargs
only if it is aproperty
object is kind of arbitrary (probably related to namedtuples). Take this example for example, where we loose the__dict__
attribute:The text was updated successfully, but these errors were encountered: