Skip to content

Commit 6cb67ac

Browse files
committed
Change obj method to be private and raise warning when used
1 parent 702a5f1 commit 6cb67ac

File tree

2 files changed

+71
-27
lines changed

2 files changed

+71
-27
lines changed

infrahub_sdk/timestamp.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import re
4+
import warnings
45
from datetime import UTC, datetime
56

67
from whenever import Date, Instant, Time, ZonedDateTime
@@ -16,17 +17,26 @@ class TimestampFormatError(ValueError): ...
1617

1718

1819
class Timestamp:
19-
obj: ZonedDateTime
20+
_obj: ZonedDateTime
2021

2122
def __init__(self, value: str | ZonedDateTime | Timestamp | None = None):
2223
if value and isinstance(value, ZonedDateTime):
23-
self.obj = value
24+
self._obj = value
2425
elif value and isinstance(value, self.__class__):
25-
self.obj = value.obj
26+
self._obj = value._obj
2627
elif isinstance(value, str):
27-
self.obj = self._parse_string(value)
28+
self._obj = self._parse_string(value)
2829
else:
29-
self.obj = ZonedDateTime.now("UTC")
30+
self._obj = ZonedDateTime.now("UTC")
31+
32+
@property
33+
def obj(self) -> ZonedDateTime:
34+
warnings.warn(
35+
"Direct access to obj property is deprecated. Use to_string(), to_timestamp(), or to_datetime() instead.",
36+
UserWarning,
37+
stacklevel=2,
38+
)
39+
return self._obj
3040

3141
@classmethod
3242
def _parse_string(cls, value: str) -> ZonedDateTime:
@@ -37,8 +47,8 @@ def _parse_string(cls, value: str) -> ZonedDateTime:
3747
pass
3848

3949
try:
40-
instant_date_ = Instant.parse_common_iso(value)
41-
return instant_date_.to_tz("UTC")
50+
instant_date = Instant.parse_common_iso(value)
51+
return instant_date.to_tz("UTC")
4252
except ValueError:
4353
pass
4454

@@ -65,14 +75,16 @@ def __repr__(self) -> str:
6575

6676
def to_string(self, with_z: bool = True) -> str:
6777
if with_z:
68-
return self.obj.instant().format_common_iso()
69-
iso8601_string = self.obj.format_common_iso()
70-
if iso8601_string[-1] == "Z":
71-
iso8601_string = iso8601_string[:-1] + "+00:00"
72-
return iso8601_string
78+
return self._obj.instant().format_common_iso()
79+
# iso8601_string = self._obj.format_common_iso()
80+
81+
# self.to_datetime().isoformat()
82+
# if iso8601_string[-1] == "Z":
83+
# iso8601_string = iso8601_string[:-1] + "+00:00"
84+
return self.to_datetime().isoformat()
7385

7486
def to_timestamp(self) -> int:
75-
return self.obj.timestamp()
87+
return self._obj.timestamp()
7688

7789
def to_datetime(self) -> datetime:
7890
time_str = self.to_string()
@@ -81,31 +93,31 @@ def to_datetime(self) -> datetime:
8193
def __eq__(self, other: object) -> bool:
8294
if not isinstance(other, Timestamp):
8395
return NotImplemented
84-
return self.obj == other.obj
96+
return self._obj == other._obj
8597

8698
def __lt__(self, other: object) -> bool:
8799
if not isinstance(other, Timestamp):
88100
return NotImplemented
89-
return self.obj < other.obj
101+
return self._obj < other._obj
90102

91103
def __gt__(self, other: object) -> bool:
92104
if not isinstance(other, Timestamp):
93105
return NotImplemented
94-
return self.obj > other.obj
106+
return self._obj > other._obj
95107

96108
def __le__(self, other: object) -> bool:
97109
if not isinstance(other, Timestamp):
98110
return NotImplemented
99-
return self.obj <= other.obj
111+
return self._obj <= other._obj
100112

101113
def __ge__(self, other: object) -> bool:
102114
if not isinstance(other, Timestamp):
103115
return NotImplemented
104-
return self.obj >= other.obj
116+
return self._obj >= other._obj
105117

106118
def __hash__(self) -> int:
107119
return hash(self.to_string())
108120

109121
def add_delta(self, hours: int = 0, minutes: int = 0, seconds: int = 0, microseconds: int = 0) -> Timestamp:
110-
time = self.obj.add(hours=hours, minutes=minutes, seconds=seconds, microseconds=microseconds)
122+
time = self._obj.add(hours=hours, minutes=minutes, seconds=seconds, microseconds=microseconds)
111123
return Timestamp(time)

tests/unit/sdk/test_timestamp.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
def test_init_empty():
1010
t1 = Timestamp()
1111
assert isinstance(t1, Timestamp)
12-
assert t1.to_string() == t1.obj.instant().format_common_iso()
12+
assert t1.to_string() == t1._obj.instant().format_common_iso()
1313

1414
t2 = Timestamp(None)
1515
assert isinstance(t2, Timestamp)
16-
assert t2.to_string() == t2.obj.instant().format_common_iso()
16+
assert t2.to_string() == t2._obj.instant().format_common_iso()
1717

1818

1919
def test_init_timestamp():
2020
t1 = Timestamp()
2121
t2 = Timestamp(t1)
2222
assert t1.to_string() == t2.to_string()
2323
assert isinstance(t2, Timestamp)
24-
assert t2.to_string() == t2.obj.instant().format_common_iso()
24+
assert t2.to_string() == t2._obj.instant().format_common_iso()
2525

2626

2727
def test_parse_string():
@@ -38,11 +38,43 @@ def test_parse_string():
3838
Timestamp._parse_string("notvalid")
3939

4040

41-
def test_to_datetime():
42-
ref_str = "2022-01-01T10:01:01.123000Z"
43-
ref = datetime(2022, 1, 1, 10, 1, 1, 123000, tzinfo=UTC)
44-
assert isinstance(Timestamp(ref_str).to_datetime(), datetime)
45-
assert Timestamp(ref_str).to_datetime() == ref
41+
@pytest.mark.parametrize(
42+
"input_str,expected_datetime",
43+
[
44+
pytest.param(
45+
"2022-01-01T10:01:01.123000Z", datetime(2022, 1, 1, 10, 1, 1, 123000, tzinfo=UTC), id="milliseconds"
46+
),
47+
pytest.param(
48+
"2023-12-31T23:59:59.999999Z", datetime(2023, 12, 31, 23, 59, 59, 999999, tzinfo=UTC), id="microseconds"
49+
),
50+
],
51+
)
52+
def test_to_datetime(input_str, expected_datetime):
53+
assert isinstance(Timestamp(input_str).to_datetime(), datetime)
54+
assert Timestamp(input_str).to_datetime() == expected_datetime
55+
56+
57+
@pytest.mark.parametrize(
58+
"input_str,expected_str,expected_str_no_z",
59+
[
60+
pytest.param(
61+
"2022-01-01T10:01:01.123000Z",
62+
"2022-01-01T10:01:01.123Z",
63+
"2022-01-01T10:01:01.123000+00:00",
64+
id="milliseconds",
65+
),
66+
pytest.param(
67+
"2023-12-31T23:59:59.999999Z",
68+
"2023-12-31T23:59:59.999999Z",
69+
"2023-12-31T23:59:59.999999+00:00",
70+
id="microseconds",
71+
),
72+
],
73+
)
74+
def test_to_string_default(input_str, expected_str, expected_str_no_z):
75+
assert isinstance(Timestamp(input_str).to_string(), str)
76+
assert Timestamp(input_str).to_string() == expected_str
77+
assert Timestamp(input_str).to_string(with_z=False) == expected_str_no_z
4678

4779

4880
def test_compare():

0 commit comments

Comments
 (0)