Skip to content

Commit cfb417b

Browse files
Merge pull request #172 from stac-utils/patch/clear-datetime-privateAttr
Fix `Search` model to make sure `_start_date` and `_end_date` privateAttr are cleared on model initialization
2 parents fa2df27 + b200188 commit cfb417b

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
## Unreleased
33

4+
## 3.1.5 (2025-02-28)
5+
6+
- Fix `Search` model to make sure `_start_date` and `_end_date` privateAttr are cleared on model initialization
7+
48
## 3.1.4 (2025-01-08)
59

610
- Fix URL comparison for Landing page conformance (#163, @gadomski)

Diff for: stac_pydantic/api/search.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@
1010
Point,
1111
Polygon,
1212
)
13-
from pydantic import BaseModel, Field, TypeAdapter, field_validator, model_validator
13+
from pydantic import (
14+
BaseModel,
15+
Field,
16+
PrivateAttr,
17+
TypeAdapter,
18+
ValidationInfo,
19+
field_validator,
20+
model_validator,
21+
)
1422

1523
from stac_pydantic.api.extensions.fields import FieldsExtension
1624
from stac_pydantic.api.extensions.query import Operator
@@ -45,8 +53,8 @@ class Search(BaseModel):
4553
limit: Optional[int] = 10
4654

4755
# Private properties to store the parsed datetime values. Not part of the model schema.
48-
_start_date: Optional[dt] = None
49-
_end_date: Optional[dt] = None
56+
_start_date: Optional[dt] = PrivateAttr(default=None)
57+
_end_date: Optional[dt] = PrivateAttr(default=None)
5058

5159
# Properties to return the private values
5260
@property
@@ -96,9 +104,11 @@ def validate_bbox(cls, v: BBox) -> BBox:
96104

97105
return v
98106

99-
@field_validator("datetime")
107+
@field_validator("datetime", mode="after")
100108
@classmethod
101-
def validate_datetime(cls, value: Optional[str]) -> Optional[str]:
109+
def validate_datetime(
110+
cls, value: Optional[str], info: ValidationInfo
111+
) -> Optional[str]:
102112
# Split on "/" and replace no value or ".." with None
103113
if value is None:
104114
return value
@@ -133,8 +143,9 @@ def validate_datetime(cls, value: Optional[str]) -> Optional[str]:
133143
)
134144

135145
# Store the parsed dates
136-
cls._start_date = dates[0]
137-
cls._end_date = dates[1]
146+
info.data["_start_date"] = dates[0]
147+
info.data["_end_date"] = dates[1]
148+
138149
# Return the original string value
139150
return value
140151

Diff for: tests/api/test_search.py

+13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ def test_temporal_search_single_tailed():
6262
assert search.end_date == utcnow
6363

6464

65+
def test_datetime_clean():
66+
# ref: https://github.com/stac-utils/stac-pydantic/issues/170
67+
utcnow = datetime.now(timezone.utc)
68+
utcnow_str = utcnow.isoformat()
69+
search = Search(datetime=utcnow_str)
70+
assert search.start_date == utcnow
71+
assert search.end_date == utcnow
72+
73+
search = Search()
74+
assert not search.start_date
75+
assert not search.end_date
76+
77+
6578
def test_temporal_search_two_tailed():
6679
# Test two tailed
6780
utcnow = datetime.now(timezone.utc)

0 commit comments

Comments
 (0)