From 93cc433bd689be741d104feeaa6286a2331f4623 Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Thu, 4 Apr 2024 11:42:28 +0200 Subject: [PATCH 1/2] (feature): remove the deprecated Context Extension --- stac_pydantic/api/extensions/context.py | 35 ------------ stac_pydantic/api/item_collection.py | 4 -- tests/api/extensions/test_context.py | 75 ------------------------- tests/test_models.py | 6 -- 4 files changed, 120 deletions(-) delete mode 100644 stac_pydantic/api/extensions/context.py delete mode 100644 tests/api/extensions/test_context.py diff --git a/stac_pydantic/api/extensions/context.py b/stac_pydantic/api/extensions/context.py deleted file mode 100644 index 6d7310c..0000000 --- a/stac_pydantic/api/extensions/context.py +++ /dev/null @@ -1,35 +0,0 @@ -from typing import Any, Dict, Optional - -from pydantic import BaseModel, model_validator - -CONTEXT_VERSION = "v1.0.0-rc.2" - - -class ContextExtension(BaseModel): - """ - https://github.com/stac-api-extensions/context/tree/v1.0.0-rc.2#context-object - """ - - returned: int - limit: Optional[int] = None - matched: Optional[int] = None - - @model_validator(mode="before") - def validate_limit(cls, values: Dict[str, Any]) -> Dict[str, Any]: - from warnings import warn - - warn( - "STAC API - Context Extension is deprecated. " - "Please see https://github.com/stac-api-extensions/context for details.", - DeprecationWarning, - ) - - if values["limit"] and values["returned"] > values["limit"]: - raise ValueError( - "Number of returned items must be less than or equal to the limit" - ) - if values["matched"] and values["returned"] > values["matched"]: - raise ValueError( - "Number of returned items must be less than or equal to the matched items" - ) - return values diff --git a/stac_pydantic/api/item_collection.py b/stac_pydantic/api/item_collection.py index 621b25d..453981e 100644 --- a/stac_pydantic/api/item_collection.py +++ b/stac_pydantic/api/item_collection.py @@ -3,7 +3,6 @@ from pydantic import model_validator -from stac_pydantic.api.extensions.context import ContextExtension from stac_pydantic.api.item import Item from stac_pydantic.api.links import Links from stac_pydantic.item_collection import ItemCollection as BaseItemCollection @@ -21,9 +20,6 @@ class ItemCollection(BaseItemCollection): numberMatched: Optional[int] = None numberReturned: Optional[int] = None - # Context Extension - context: Optional[ContextExtension] = None - @model_validator(mode="after") def required_links(self) -> "ItemCollection": if self.links: diff --git a/tests/api/extensions/test_context.py b/tests/api/extensions/test_context.py deleted file mode 100644 index e148169..0000000 --- a/tests/api/extensions/test_context.py +++ /dev/null @@ -1,75 +0,0 @@ -import jsonschema -import pytest -import requests -from pydantic import ValidationError - -from stac_pydantic.api.extensions.context import CONTEXT_VERSION, ContextExtension -from stac_pydantic.api.item_collection import ItemCollection -from stac_pydantic.api.version import STAC_API_VERSION -from stac_pydantic.version import STAC_VERSION - -from ...conftest import request - -ITEM_COLLECTION = "itemcollection-sample-full.json" -PATH = ["tests", "api", "examples", f"v{STAC_API_VERSION}"] - - -valid_context = [ - (10, 10, 10), - (10, 20, 30), - (10, 10, None), - (10, None, None), -] -invalid_context = [ - (10, 1, 1), - (10, None, 1), - (20, 10, None), - (None, 10, 10), -] - - -@pytest.mark.parametrize("returned,limit,matched", valid_context) -def test_context_extension(returned, limit, matched): - context = {"returned": returned, "limit": limit, "matched": matched} - - result = ContextExtension(**context).model_dump() - assert result == context - - -@pytest.mark.parametrize("returned,limit,matched", valid_context) -def test_context_schema(returned, limit, matched): - schema_url = f"https://github.com/stac-api-extensions/context/blob/{CONTEXT_VERSION}/json-schema/schema.json" - req = requests.get(schema_url) - schema = req.json() - - context = {"returned": returned, "limit": limit, "matched": matched} - - jsonschema.validate(instance=context, schema=schema) - - -@pytest.mark.parametrize("returned,limit,matched", invalid_context) -def test_context_extension_invalid(returned, limit, matched): - context = {"returned": returned, "limit": limit, "matched": matched} - - with pytest.raises((ValueError, TypeError)): - ContextExtension(**context).model_dump() - - -def test_api_item_collection_with_context_extension(): - item_collection = request(ITEM_COLLECTION, PATH) - for feat in item_collection["features"]: - feat["stac_version"] = STAC_VERSION - - item_collection["context"] = {"returned": 10, "limit": 10, "matched": 100} - ItemCollection(**item_collection) - - -def test_api_context_extension_invalid(): - item_collection = request(ITEM_COLLECTION, PATH) - for feat in item_collection["features"]: - feat["stac_version"] = STAC_VERSION - - item_collection.update({"context": {"returned": 20, "limit": 10, "matched": 100}}) - - with pytest.raises(ValidationError): - ItemCollection(**item_collection) diff --git a/tests/test_models.py b/tests/test_models.py index 0a9a22f..c427533 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -6,7 +6,6 @@ from shapely.geometry import shape from stac_pydantic import Collection, Item, ItemProperties -from stac_pydantic.api.extensions.context import ContextExtension from stac_pydantic.extensions import validate_extensions from stac_pydantic.links import Link, Links from stac_pydantic.shared import MimeTypes @@ -161,11 +160,6 @@ def test_geo_interface() -> None: Item(**test_item) -def test_api_context_extension() -> None: - context = {"returned": 10, "limit": 10, "matched": 100} - ContextExtension(**context) - - def test_declared_model() -> None: class TestProperties(ItemProperties): foo: str From 9eaeb6d86abdb7f367bf8c146e16bac4b52ccb59 Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Thu, 4 Apr 2024 23:31:18 +0200 Subject: [PATCH 2/2] update changelog --- CHANGELOG.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 902e96e..4a1031e 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,8 @@ + +3.1.0 (TBD) +----------------- +- Remove the deprecated `Context` extension (#138, @vincentsarago) + 3.0.0 (2024-01-25) ------------------ - Support pydantic>2.0 (@huard)