Skip to content

fix: copy with tzinfo #567

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

Merged
merged 2 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/input/datetime.rs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please can you test with copy.copy as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. What do you think?

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