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

copy.deepcopy() with datetime objects fails with "TypeError: __init__() takes exactly 2 arguments (1 given)" #387

Closed
ralt opened this issue Mar 13, 2020 · 8 comments · Fixed by #392
Labels

Comments

@ralt
Copy link

ralt commented Mar 13, 2020

Apparently since this commit: 96d65f3

@ralt
Copy link
Author

ralt commented Mar 13, 2020

Sorry, I did this ticket a bit too hastily. Here is a reproducer:

import copy
import datetime
import yaml

copy.deepcopy(yaml.constructor.timezone(datetime.timedelta(seconds=0)))
[ralt@tag ~]$ python2 /tmp/r.py
Traceback (most recent call last):
  File "/tmp/r.py", line 6, in <module>
    copy.deepcopy(yaml.constructor.timezone(datetime.timedelta(seconds=0)))
  File "/usr/lib64/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib64/python2.7/copy.py", line 329, in _reconstruct
    y = callable(*args)
TypeError: __init__() takes exactly 2 arguments (1 given)

It is when dealing with datetime objects.

@ralt ralt changed the title copy.deepcopy() fails with "TypeError: __init__() takes exactly 2 arguments (1 given)" copy.deepcopy() with datetime objects fails with "TypeError: __init__() takes exactly 2 arguments (1 given)" Mar 13, 2020
@cognifloyd
Copy link

cognifloyd commented Apr 4, 2020

This only applies to python2. In python3, it uses the stdlib timezone objects.

So the minimal timezone object (yaml.constructor.timezone) needs __copy__ and __deepcopy__ methods.

@cognifloyd
Copy link

Just to add a slightly more verbose description of this issue:
This bug occurs in python 2 + pyyaml 5.3+ when:

  1. parse a document with a yaml timestamp
  2. pyyaml converts that timestamp to a timezone aware datetime object where tzinfo is an instance of yaml.constructor.timezone
  3. use copy.deepcopy to copy the python object (typically a dict) that resulted from parsing the yaml
  4. copy.deepcopy fails miserably with the TypeError as described above because it can't copy yaml.constructor.timezone.

@ralt
Copy link
Author

ralt commented Apr 4, 2020

Thank you, you are much better at summarizing usefully the state of things :)

As of right now, we have reverted pyyaml to older than 5.3. Should we implement those methods or do you plan on fixing it in pyyaml?

@cognifloyd
Copy link

cognifloyd commented Apr 4, 2020

This broke one of my transient dependencies, and pyyaml is a transient dep of yet another transient dep... I don't have resources to fix it right now so I also pyyaml to <5.3 on py2.7.

But pyyaml is the proper place to fix this instead of pinning transient deps.

@ingydotnet
Copy link
Member

If someone wants to submit a PR (extra credit if it has tests) I can get this out soon.

ovv added a commit to ovv/pyyaml that referenced this issue Apr 6, 2020
@ralt
Copy link
Author

ralt commented Apr 6, 2020

@ingydotnet there you go (not from me)

@ingydotnet
Copy link
Member

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants