Skip to content

Commit 9f69da1

Browse files
authored
fix: copy with tzinfo (#567)
* fix: copy with tzinfo * fix: apply feedback
1 parent c9f45e3 commit 9f69da1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/input/datetime.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use pyo3::intern;
22
use pyo3::prelude::*;
3-
use pyo3::types::{PyDate, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTzInfo};
3+
use pyo3::types::{PyDate, PyDateTime, PyDelta, PyDeltaAccess, PyDict, PyTime, PyTzInfo};
44
use speedate::{Date, DateTime, Duration, ParseError, Time};
55
use std::borrow::Cow;
66
use strum::EnumMessage;
@@ -455,4 +455,8 @@ impl TzInfo {
455455
format!("{:+03}:{:02}", mins / 60, (mins % 60).abs())
456456
}
457457
}
458+
459+
fn __deepcopy__(&self, py: Python, _memo: &PyDict) -> PyResult<Py<Self>> {
460+
Py::new(py, self.clone())
461+
}
458462
}

tests/validators/test_datetime.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import json
23
import platform
34
import re
@@ -185,6 +186,22 @@ def test_tz_comparison():
185186
SchemaValidator({'type': 'datetime', 'gt': uk_3pm}).validate_python('2022-01-01T16:00:00+01:00')
186187

187188

189+
def test_tz_info_deepcopy():
190+
output = SchemaValidator({'type': 'datetime'}).validate_python('2023-02-15T16:23:44.037Z')
191+
c = copy.deepcopy(output)
192+
assert repr(output.tzinfo) == 'TzInfo(UTC)'
193+
assert repr(c.tzinfo) == 'TzInfo(UTC)'
194+
assert c == output
195+
196+
197+
def test_tz_info_copy():
198+
output = SchemaValidator({'type': 'datetime'}).validate_python('2023-02-15T16:23:44.037Z')
199+
c = copy.copy(output)
200+
assert repr(output.tzinfo) == 'TzInfo(UTC)'
201+
assert repr(c.tzinfo) == 'TzInfo(UTC)'
202+
assert c == output
203+
204+
188205
def test_custom_tz():
189206
class CustomTz(tzinfo):
190207
def utcoffset(self, _dt):

0 commit comments

Comments
 (0)