Skip to content

Commit 567f342

Browse files
authored
Allow copy of date objects (#524)
Fixes #523
1 parent fad6357 commit 567f342

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

neo4j/time/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,12 @@ def __repr__(self):
389389
def __str__(self):
390390
return self.iso_format()
391391

392+
def __copy__(self):
393+
return self.__new(self.ticks, self.hour, self.minute, self.second, self.tzinfo)
394+
395+
def __deepcopy__(self, memodict={}):
396+
return self.__copy__()
397+
392398
@classmethod
393399
def from_iso_format(cls, s):
394400
m = DURATION_ISO_PATTERN.match(s)
@@ -845,6 +851,12 @@ def __sub__(self, other):
845851
except TypeError:
846852
return NotImplemented
847853

854+
def __copy__(self):
855+
return self.__new(self.__ordinal, self.__year, self.__month, self.__day)
856+
857+
def __deepcopy__(self, *args, **kwargs):
858+
return self.__copy__()
859+
848860
# INSTANCE METHODS #
849861

850862
def replace(self, **kwargs):
@@ -1133,6 +1145,12 @@ def __add__(self, other):
11331145
def __sub__(self, other):
11341146
return NotImplemented
11351147

1148+
def __copy__(self):
1149+
return self.__new(self.__ticks, self.__hour, self.__minute, self.__second, self.__tzinfo)
1150+
1151+
def __deepcopy__(self, *args, **kwargs):
1152+
return self.__copy__()
1153+
11361154
# INSTANCE METHODS #
11371155

11381156
def replace(self, **kwargs):
@@ -1474,6 +1492,12 @@ def __sub__(self, other):
14741492
return self.__add__(-other)
14751493
return NotImplemented
14761494

1495+
def __copy__(self):
1496+
return self.combine(self.__date, self.__time)
1497+
1498+
def __deepcopy__(self, *args, **kwargs):
1499+
return self.__copy__()
1500+
14771501
# INSTANCE METHODS #
14781502

14791503
def date(self):

tests/unit/time/test_date.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from unittest import TestCase
2525

2626
import pytz
27+
import copy
2728

2829
from neo4j.time import Duration, Date, UnixEpoch, ZeroDate
2930

@@ -532,3 +533,15 @@ def test_from_iso_format(self):
532533
expected = Date(2018, 10, 1)
533534
actual = Date.from_iso_format("2018-10-01")
534535
self.assertEqual(expected, actual)
536+
537+
def test_date_copy(self):
538+
d = Date(2010, 10, 1)
539+
d2 = copy.copy(d)
540+
self.assertIsNot(d, d2)
541+
self.assertEqual(d, d2)
542+
543+
def test_date_deep_copy(self):
544+
d = Date(2010, 10, 1)
545+
d2 = copy.deepcopy(d)
546+
self.assertIsNot(d, d2)
547+
self.assertEqual(d, d2)

tests/unit/time/test_datetime.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21-
21+
import copy
2222
from unittest import TestCase
2323
from datetime import (
2424
datetime,
@@ -337,6 +337,18 @@ def test_from_iso_format_with_negative_long_tz(self):
337337
actual = DateTime.from_iso_format("2018-10-01T12:34:56.123456789-12:34:56.123456")
338338
self.assertEqual(expected, actual)
339339

340+
def test_datetime_copy(self):
341+
d = DateTime(2010, 10, 1, 10, 0, 10)
342+
d2 = copy.copy(d)
343+
self.assertIsNot(d, d2)
344+
self.assertEqual(d, d2)
345+
346+
def test_datetime_deep_copy(self):
347+
d = DateTime(2010, 10, 1, 10, 0, 12)
348+
d2 = copy.deepcopy(d)
349+
self.assertIsNot(d, d2)
350+
self.assertEqual(d, d2)
351+
340352

341353
def test_iso_format_with_time_zone_case_1():
342354
# python -m pytest tests/unit/time/test_datetime.py -s -v -k test_iso_format_with_time_zone_case_1

0 commit comments

Comments
 (0)