I've just run into this error with copy.deepcopy
:
import copy
import datetime
class Hours(datetime.timedelta):
# Using __new__ because timedelta is immutable
# See https://stackoverflow.com/a/22531773/3358488.
def __new__(cls, hours):
return datetime.timedelta.__new__(cls, hours=hours)
h1 = Hours(2)
h2 = copy.deepcopy(h1) # TypeError: __new__() takes 2 positional arguments but 4 were given
Here's the full traceback:
Traceback (most recent call last):
File "/Users/xxxx/Documents/PyCharmProjects/Flow/backend/deepcopy_test.py", line 11, in <module>
h2 = copy.deepcopy(h1) # TypeError: __new__() takes 2 positional arguments but 4 were given
File "/Users/xxxx/.conda/envs/qtdesktopapp/lib/python3.8/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/Users/xxxx/.conda/envs/qtdesktopapp/lib/python3.8/copy.py", line 264, in _reconstruct
y = func(*args)
TypeError: __new__() takes 2 positional arguments but 4 were given
Note that timedelta initialization's takes anywhere from 0 to 6 arguments:
class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
Have I used copy.deepcopy
incorrectly or have I found a bug in it?