Skip to content

Commit

Permalink
Add __deepcopy__ method to TomlTz class. Fix uiri#308 (uiri#309)
Browse files Browse the repository at this point in the history
Without this method, the dict that results from parsing a datetime
with a timezone cannot be deep-copied using the stdlib
`copy.deepcopy()` function.
  • Loading branch information
jpsca authored Oct 27, 2020
1 parent 9be6458 commit 16a6441
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,12 @@ def test_comment_preserve_decoder_encoder():
encoder=toml.TomlPreserveCommentEncoder())

assert len(s) == len(test_str) and sorted(test_str) == sorted(s)


def test_deepcopy_timezone():
import copy

o = toml.loads("dob = 1979-05-24T07:32:00-08:00")
o2 = copy.deepcopy(o)
assert o2["dob"] == o["dob"]
assert o2["dob"] is not o["dob"]
3 changes: 3 additions & 0 deletions toml/tz.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def __init__(self, toml_offset):
self._hours = int(self._raw_offset[1:3])
self._minutes = int(self._raw_offset[4:6])

def __deepcopy__(self, memo):
return self.__class__(self._raw_offset)

def tzname(self, dt):
return "UTC" + self._raw_offset

Expand Down

0 comments on commit 16a6441

Please sign in to comment.