Skip to content
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

model.datatypes: fix error when copy Dates #295

Merged
merged 2 commits into from
Oct 2, 2024
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
11 changes: 11 additions & 0 deletions basyx/aas/model/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ def __eq__(self, other: object) -> bool:
other_tzinfo = other.tzinfo if hasattr(other, 'tzinfo') else None # type: ignore
return datetime.date.__eq__(self, other) and self.tzinfo == other_tzinfo

def __copy__(self):
cls = self.__class__
result = cls.__new__(cls, self.year, self.month, self.day, self.tzinfo)
return result

def __deepcopy__(self, memo):
cls = self.__class__
result = cls.__new__(cls, self.year, self.month, self.day, self.tzinfo)
memo[id(self)] = result
return result

# TODO override comparsion operators
# TODO add into_datetime function
# TODO add includes(:DateTime) -> bool function
Expand Down
8 changes: 8 additions & 0 deletions test/model/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime
import math
import unittest
import copy

import dateutil

Expand Down Expand Up @@ -177,6 +178,13 @@ def test_parse_partial_dates(self) -> None:
model.datatypes.from_xsd("10-10", model.datatypes.GYearMonth)
self.assertEqual("Value is not a valid XSD GYearMonth string", str(cm.exception))

def test_copy_date(self) -> None:
date = model.datatypes.Date(2020, 1, 24)
date_copy_shallow = copy.copy(date)
self.assertEqual(date, date_copy_shallow)
date_copy_deep = copy.deepcopy(date)
self.assertEqual(date, date_copy_deep)

def test_serialize_partial_dates(self) -> None:
self.assertEqual("2019", model.datatypes.xsd_repr(model.datatypes.GYear(2019)))
self.assertEqual("2019Z", model.datatypes.xsd_repr(model.datatypes.GYear(2019, datetime.timezone.utc)))
Expand Down
Loading