Skip to content

Fix Search model to make sure _start_date and _end_date privateAttr are cleared on model initialization #172

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
Feb 28, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

## Unreleased

## 3.1.5 (2025-02-28)

- Fix `Search` model to make sure `_start_date` and `_end_date` privateAttr are cleared on model initialization

## 3.1.4 (2025-01-08)

- Fix URL comparison for Landing page conformance (#163, @gadomski)
Expand Down
25 changes: 18 additions & 7 deletions stac_pydantic/api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
Point,
Polygon,
)
from pydantic import BaseModel, Field, TypeAdapter, field_validator, model_validator
from pydantic import (
BaseModel,
Field,
PrivateAttr,
TypeAdapter,
ValidationInfo,
field_validator,
model_validator,
)

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

# Private properties to store the parsed datetime values. Not part of the model schema.
_start_date: Optional[dt] = None
_end_date: Optional[dt] = None
_start_date: Optional[dt] = PrivateAttr(default=None)
_end_date: Optional[dt] = PrivateAttr(default=None)

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

return v

@field_validator("datetime")
@field_validator("datetime", mode="after")
@classmethod
def validate_datetime(cls, value: Optional[str]) -> Optional[str]:
def validate_datetime(
cls, value: Optional[str], info: ValidationInfo
) -> Optional[str]:
# Split on "/" and replace no value or ".." with None
if value is None:
return value
Expand Down Expand Up @@ -133,8 +143,9 @@ def validate_datetime(cls, value: Optional[str]) -> Optional[str]:
)

# Store the parsed dates
cls._start_date = dates[0]
cls._end_date = dates[1]
info.data["_start_date"] = dates[0]
info.data["_end_date"] = dates[1]

# Return the original string value
return value

Expand Down
13 changes: 13 additions & 0 deletions tests/api/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ def test_temporal_search_single_tailed():
assert search.end_date == utcnow


def test_datetime_clean():
# ref: https://github.com/stac-utils/stac-pydantic/issues/170
utcnow = datetime.now(timezone.utc)
utcnow_str = utcnow.isoformat()
search = Search(datetime=utcnow_str)
assert search.start_date == utcnow
assert search.end_date == utcnow

search = Search()
assert not search.start_date
assert not search.end_date


def test_temporal_search_two_tailed():
# Test two tailed
utcnow = datetime.now(timezone.utc)
Expand Down