Skip to content

Commit

Permalink
fix: copy with tzinfo (pydantic#567)
Browse files Browse the repository at this point in the history
* fix: copy with tzinfo

* fix: apply feedback
  • Loading branch information
JeanArhancet committed May 1, 2023
1 parent c9f45e3 commit 9f69da1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/input/datetime.rs
@@ -1,6 +1,6 @@
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::{PyDate, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTzInfo};
use pyo3::types::{PyDate, PyDateTime, PyDelta, PyDeltaAccess, PyDict, PyTime, PyTzInfo};
use speedate::{Date, DateTime, Duration, ParseError, Time};
use std::borrow::Cow;
use strum::EnumMessage;
Expand Down Expand Up @@ -455,4 +455,8 @@ impl TzInfo {
format!("{:+03}:{:02}", mins / 60, (mins % 60).abs())
}
}

fn __deepcopy__(&self, py: Python, _memo: &PyDict) -> PyResult<Py<Self>> {
Py::new(py, self.clone())
}
}
17 changes: 17 additions & 0 deletions tests/validators/test_datetime.py
@@ -1,3 +1,4 @@
import copy
import json
import platform
import re
Expand Down Expand Up @@ -185,6 +186,22 @@ def test_tz_comparison():
SchemaValidator({'type': 'datetime', 'gt': uk_3pm}).validate_python('2022-01-01T16:00:00+01:00')


def test_tz_info_deepcopy():
output = SchemaValidator({'type': 'datetime'}).validate_python('2023-02-15T16:23:44.037Z')
c = copy.deepcopy(output)
assert repr(output.tzinfo) == 'TzInfo(UTC)'
assert repr(c.tzinfo) == 'TzInfo(UTC)'
assert c == output


def test_tz_info_copy():
output = SchemaValidator({'type': 'datetime'}).validate_python('2023-02-15T16:23:44.037Z')
c = copy.copy(output)
assert repr(output.tzinfo) == 'TzInfo(UTC)'
assert repr(c.tzinfo) == 'TzInfo(UTC)'
assert c == output


def test_custom_tz():
class CustomTz(tzinfo):
def utcoffset(self, _dt):
Expand Down

0 comments on commit 9f69da1

Please sign in to comment.