classification
Title: Copying objects subclassed from SimpleNamespace doesn't work
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: serhiy.storchaka, stereobutter
Priority: normal Keywords:

Created on 2018-05-22 09:56 by stereobutter, last changed 2018-05-22 10:20 by serhiy.storchaka. This issue is now closed.

Messages (2)
msg317266 - (view) Author: Sascha (stereobutter) Date: 2018-05-22 09:56
Try 
from types import SimpleNamespace
import copy
class Person(SimpleNamespace):
    def __init__(self, name, **kwargs):
        self.name = name
        super().__init__(**kwargs)

bob = Person('Bob', job='tester')
clone = copy.copy(bob)

For me this results in 
TypeError: __init__() missing 1 required positional argument: 'name'
msg317269 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-22 10:20
Right, this is because your subclass is not completely compatible with SimpleNamespace. The SimpleNamespace constructor accepts only keyword arguments, but your class requires a positional argument. You have to implement the __copy__ method for supporting shallow copying and the __deepcopy__ method for supporting deep copying. Or the __reduce__ method for supporting both shallow and deep copying and pickling.
History
Date User Action Args
2018-05-22 10:20:33serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg317269

resolution: not a bug
stage: resolved
2018-05-22 09:56:10stereobuttercreate