From 13a23775773fb45c56ed27e62ef9d6720641300e Mon Sep 17 00:00:00 2001 From: rhysrevans3 Date: Wed, 28 Aug 2024 15:46:25 +0100 Subject: [PATCH] Switching from attrs to basemodel for patch operations. --- .../extensions/tests/test_transaction.py | 12 +++------ stac_fastapi/types/stac_fastapi/types/core.py | 3 +-- stac_fastapi/types/stac_fastapi/types/stac.py | 27 +++++++++---------- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/stac_fastapi/extensions/tests/test_transaction.py b/stac_fastapi/extensions/tests/test_transaction.py index db2a9f1b..afac5735 100644 --- a/stac_fastapi/extensions/tests/test_transaction.py +++ b/stac_fastapi/extensions/tests/test_transaction.py @@ -145,9 +145,7 @@ def test_merge_patch_item(client: TestClient, item: Item) -> None: def test_json_patch_item(client: TestClient) -> None: - operations = [ - {"op": "add", "path": "properties.new_prop", "value": "new_prop_value"} - ] + operations = [{"op": "add", "path": "properties.new_prop", "value": "new_prop_value"}] headers = {"Content-Type": "application/json-patch+json"} response = client.patch( "/collections/a-collection/items/an-item", @@ -191,9 +189,7 @@ def test_merge_patch_collection(client: TestClient, collection: Collection) -> N def test_json_patch_collection(client: TestClient) -> None: - operations = [ - {"op": "add", "path": "summaries.new_prop", "value": "new_prop_value"} - ] + operations = [{"op": "add", "path": "summaries.new_prop", "value": "new_prop_value"}] headers = {"Content-Type": "application/json-patch+json"} response = client.patch( "/collections/a-collection/items/an-item", @@ -273,9 +269,7 @@ def collection() -> Collection: "description": "A test collection", "extent": { "spatial": {"bbox": [[-180, -90, 180, 90]]}, - "temporal": { - "interval": [["2000-01-01T00:00:00Z", "2024-01-01T00:00:00Z"]] - }, + "temporal": {"interval": [["2000-01-01T00:00:00Z", "2024-01-01T00:00:00Z"]]}, }, "links": [], "assets": {}, diff --git a/stac_fastapi/types/stac_fastapi/types/core.py b/stac_fastapi/types/stac_fastapi/types/core.py index 4595cd37..eb6c598c 100644 --- a/stac_fastapi/types/stac_fastapi/types/core.py +++ b/stac_fastapi/types/stac_fastapi/types/core.py @@ -21,8 +21,7 @@ from stac_fastapi.types.requests import get_base_url from stac_fastapi.types.rfc3339 import DateTimeType from stac_fastapi.types.search import BaseSearchPostRequest -from stac_fastapi.types.stac import (PartialCollection, PartialItem, - PatchOperation) +from stac_fastapi.types.stac import PartialCollection, PartialItem, PatchOperation __all__ = [ "NumType", diff --git a/stac_fastapi/types/stac_fastapi/types/stac.py b/stac_fastapi/types/stac_fastapi/types/stac.py index f4df87e1..9c7ec6d3 100644 --- a/stac_fastapi/types/stac_fastapi/types/stac.py +++ b/stac_fastapi/types/stac_fastapi/types/stac.py @@ -2,7 +2,7 @@ from typing import Any, Dict, List, Literal, Optional, Union -import attr +from pydantic import BaseModel from stac_pydantic.shared import BBox from typing_extensions import TypedDict @@ -110,31 +110,28 @@ class PartialItem(TypedDict, total=False): collection: Optional[str] -@attr.s -class PatchAddReplaceTest: +class PatchAddReplaceTest(BaseModel): """Add, Replace or Test Operation.""" - path: str = attr.ib() - op: Literal["add", "replace", "test"] = attr.ib() - value: Any = attr.ib() + path: str + op: Literal["add", "replace", "test"] + value: Any -@attr.s -class PatchRemove: +class PatchRemove(BaseModel): """Remove Operation.""" - path: str = attr.ib() - op: Literal["remove"] = attr.ib() + path: str + op: Literal["remove"] -@attr.s -class PatchMoveCopy: +class PatchMoveCopy(BaseModel): """Move or Copy Operation.""" - path: str = attr.ib() - op: Literal["move", "copy"] = attr.ib() + path: str + op: Literal["move", "copy"] - def __attrs_init__(self, *args, **kwargs): + def __init__(self, *args, **kwargs): """Init function to add 'from' field.""" super().__init__(*args, **kwargs) self.__setattr__("from", kwargs["from"])