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

Correctly set default values, fix start/end/datetime field validators #135

Closed

Conversation

thomas-maschler
Copy link
Contributor

@thomas-maschler thomas-maschler commented Feb 16, 2024

  • Require type property to be set for Catalog and Collections
  • Fix validator for Item datetime and Common MetaData start_datetime and end_datetime
  • Include datetime and license to Common MetaData
  • Make sure default values for required but unset fields are correctly parsed
  • Add support from Python 3.12
  • Lint all files
  • Increase test coverage

Keeping this as DRAFT until #131 is merged

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ No coverage uploaded for pull request base (main@c03272e). Click here to learn what that means.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #135   +/-   ##
=======================================
  Coverage        ?   96.90%           
=======================================
  Files           ?       26           
  Lines           ?      613           
  Branches        ?        0           
=======================================
  Hits            ?      594           
  Misses          ?       19           
  Partials        ?        0           
Flag Coverage Δ
unittests 96.90% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +28 to +50

def datetime_to_str(d: dt) -> str:
if d.tzinfo is None:
d = d.replace(tzinfo=timezone.utc)

timestamp = d.isoformat(timespec="auto")
zulu = "+00:00"
if timestamp.endswith(zulu):
timestamp = f"{timestamp[: -len(zulu)]}Z"

return timestamp


# Allows for some additional flexibility in the input datetime format.
# If the input value has timezone information, it will be converted to UTC timezone.
# Otherwise URT timezone will be assumed.
UtcDatetime = Annotated[
Union[str, dt],
# Input value must be in a format which has timezone information
AfterValidator(lambda d: d if isinstance(d, dt) else dateutil.parser.isoparse(d)),
# Use `isoformat` to serialize the value in an RFC3339 compatible format
PlainSerializer(lambda d: datetime_to_str(d)),
]
Copy link
Contributor Author

@thomas-maschler thomas-maschler Feb 16, 2024

Choose a reason for hiding this comment

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

This might have to go depending on outcome of #131

Comment on lines +84 to +102
@pytest.mark.parametrize(
"datetime", ["2022-01-01T00:00:00", "2022-01-01", "2022-01", "2022"]
)
def test_item_datetime_no_tz(datetime):

item_data = {
"type": "Feature",
"id": "sample-item",
"stac_version": "1.0.0",
"geometry": {"type": "Point", "coordinates": [125.6, 10.1]},
"bbox": [125.6, 10.1, 125.6, 10.1],
"properties": {"datetime": datetime},
}
item = Item(**item_data)

item_json = item.model_dump(mode="json")

# The model should fix the date and timezone for us
assert item_json["properties"]["datetime"] == "2022-01-01T00:00:00Z"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This might have to go depending on outcome of #131

Comment on lines +89 to +94
# def test_invalid_temporal_search_date():
# # Just a date, no time
# utcnow = datetime.now(timezone.utc).strftime("%Y-%m-%d")
# with pytest.raises(ValidationError):
# Search(collections=["collection1"], datetime=utcnow)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This might have stay depending on the outcome of #131

@vincentsarago
Copy link
Member

This PR has changes that have been merged in main in small PR.

For the easing the review @thomas-maschler what do you think about closing this PR, wait for #131 to be merged and then open a new PR with the proposed changes?

@thomas-maschler
Copy link
Contributor Author

Yep, that was the plan and why I kept it as draft

Comment on lines +109 to +111
# If there is only one date, insert a None for the start date
if len(values) == 1:
values.insert(0, None)
Copy link
Contributor

Choose a reason for hiding this comment

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

I ran into this MR after I created #145. I see that the above matches the current behavior, nice.

But I also think the start and end should be the same if a single value is given. (Otherwise one cannot tell the difference between a single value, and a open-ended range, without looking at the original datetime again.) So I guess this could be changed to:

# If there is only one date, duplicate to use for both start and end dates
if len(values) == 1:
    values.insert(0, values[0])

Or maybe easier to read then:

    values.append(values[0])

@vincentsarago
Copy link
Member

@thomas-maschler it might be easier to start a new PR than try to update this one 🙏

@thomas-maschler
Copy link
Contributor Author

Yes, I realized this. We are in the middle of a data release. I will be able to work on this again in early June.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants