From a74d3eb3670e3e578f2b064c6f6dddf53d6cf267 Mon Sep 17 00:00:00 2001 From: Paco Aranda Date: Thu, 12 Dec 2024 11:20:59 +0100 Subject: [PATCH] [BUGFIX] [TESTS] Remove custom isoformat parsing and let pydantic do the work (#5752) # Description **Type of change** - Bug fix (non-breaking change which fixes an issue) **How Has This Been Tested** **Checklist** - I added relevant documentation - I followed the style guidelines of this project - I did a self-review of my code - I made corresponding changes to the documentation - I confirm My changes generate no new warnings - I have added tests that prove my fix is effective or that my feature works - I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/) --- argilla/src/argilla/_api/_base.py | 10 ------ argilla/src/argilla/_api/_datasets.py | 2 -- argilla/src/argilla/_api/_fields.py | 2 -- argilla/src/argilla/_api/_metadata.py | 2 -- argilla/src/argilla/_api/_questions.py | 2 -- argilla/src/argilla/_api/_records.py | 2 -- argilla/src/argilla/_api/_users.py | 2 -- argilla/src/argilla/_api/_vectors.py | 2 -- argilla/src/argilla/_api/_workspaces.py | 4 +-- argilla/tests/unit/models/__init__.py | 14 ++++++++ .../unit/models/test_workspace_models.py | 35 +++++++++++++++++++ 11 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 argilla/tests/unit/models/__init__.py create mode 100644 argilla/tests/unit/models/test_workspace_models.py diff --git a/argilla/src/argilla/_api/_base.py b/argilla/src/argilla/_api/_base.py index 1ec6362807..8c8cc8cf4e 100644 --- a/argilla/src/argilla/_api/_base.py +++ b/argilla/src/argilla/_api/_base.py @@ -12,17 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -from datetime import datetime from typing import Generic, TYPE_CHECKING, TypeVar from uuid import UUID from argilla._helpers import LoggingMixin - if TYPE_CHECKING: from httpx import Client - __all__ = ["ResourceAPI"] T = TypeVar("T") @@ -51,10 +48,3 @@ def delete(self, id: UUID) -> None: def update(self, resource: T) -> T: return resource - - #################### - # Utility methods # - #################### - - def _date_from_iso_format(self, date: str) -> datetime: - return datetime.fromisoformat(date) diff --git a/argilla/src/argilla/_api/_datasets.py b/argilla/src/argilla/_api/_datasets.py index 64b8a7b64c..b6ccdca261 100644 --- a/argilla/src/argilla/_api/_datasets.py +++ b/argilla/src/argilla/_api/_datasets.py @@ -134,8 +134,6 @@ def name_exists(self, name: str, workspace_id: UUID) -> bool: #################### def _model_from_json(self, response_json: Dict) -> "DatasetModel": - response_json["inserted_at"] = self._date_from_iso_format(date=response_json["inserted_at"]) - response_json["updated_at"] = self._date_from_iso_format(date=response_json["updated_at"]) return DatasetModel(**response_json) def _model_from_jsons(self, response_jsons: List[Dict]) -> List["DatasetModel"]: diff --git a/argilla/src/argilla/_api/_fields.py b/argilla/src/argilla/_api/_fields.py index c101e80d7d..dcf6a0394f 100644 --- a/argilla/src/argilla/_api/_fields.py +++ b/argilla/src/argilla/_api/_fields.py @@ -80,8 +80,6 @@ def list(self, dataset_id: UUID) -> List[FieldModel]: #################### def _model_from_json(self, response_json: Dict) -> FieldModel: - response_json["inserted_at"] = self._date_from_iso_format(date=response_json["inserted_at"]) - response_json["updated_at"] = self._date_from_iso_format(date=response_json["updated_at"]) return FieldModel(**response_json) def _model_from_jsons(self, response_jsons: List[Dict]) -> List[FieldModel]: diff --git a/argilla/src/argilla/_api/_metadata.py b/argilla/src/argilla/_api/_metadata.py index 4cdaad543d..4316f7e0cc 100644 --- a/argilla/src/argilla/_api/_metadata.py +++ b/argilla/src/argilla/_api/_metadata.py @@ -78,8 +78,6 @@ def list(self, dataset_id: UUID) -> List[MetadataFieldModel]: #################### def _model_from_json(self, response_json: Dict) -> MetadataFieldModel: - response_json["inserted_at"] = self._date_from_iso_format(date=response_json["inserted_at"]) - response_json["updated_at"] = self._date_from_iso_format(date=response_json["updated_at"]) return MetadataFieldModel(**response_json) def _model_from_jsons(self, response_jsons: List[Dict]) -> List[MetadataFieldModel]: diff --git a/argilla/src/argilla/_api/_questions.py b/argilla/src/argilla/_api/_questions.py index 98d67eb6da..8d930c410c 100644 --- a/argilla/src/argilla/_api/_questions.py +++ b/argilla/src/argilla/_api/_questions.py @@ -81,8 +81,6 @@ def list(self, dataset_id: UUID) -> List[QuestionModel]: #################### def _model_from_json(self, response_json: Dict) -> QuestionModel: - response_json["inserted_at"] = self._date_from_iso_format(date=response_json["inserted_at"]) - response_json["updated_at"] = self._date_from_iso_format(date=response_json["updated_at"]) return QuestionModel(**response_json) def _model_from_jsons(self, response_jsons: List[Dict]) -> List[QuestionModel]: diff --git a/argilla/src/argilla/_api/_records.py b/argilla/src/argilla/_api/_records.py index 6581655839..6f29576d79 100644 --- a/argilla/src/argilla/_api/_records.py +++ b/argilla/src/argilla/_api/_records.py @@ -225,8 +225,6 @@ def create_record_responses(self, record: RecordModel) -> None: #################### def _model_from_json(self, response_json: Dict) -> RecordModel: - response_json["inserted_at"] = self._date_from_iso_format(date=response_json["inserted_at"]) - response_json["updated_at"] = self._date_from_iso_format(date=response_json["updated_at"]) if "vectors" in response_json: response_json["vectors"] = [ {"name": key, "vector_values": value} for key, value in response_json["vectors"].items() diff --git a/argilla/src/argilla/_api/_users.py b/argilla/src/argilla/_api/_users.py index e0fcb2c604..d5d8c3a369 100644 --- a/argilla/src/argilla/_api/_users.py +++ b/argilla/src/argilla/_api/_users.py @@ -121,8 +121,6 @@ def delete_from_workspace(self, workspace_id: UUID, user_id: UUID) -> "UserModel #################### def _model_from_json(self, response_json) -> UserModel: - response_json["inserted_at"] = self._date_from_iso_format(date=response_json["inserted_at"]) - response_json["updated_at"] = self._date_from_iso_format(date=response_json["updated_at"]) return UserModel(**response_json) def _model_from_jsons(self, response_jsons) -> List[UserModel]: diff --git a/argilla/src/argilla/_api/_vectors.py b/argilla/src/argilla/_api/_vectors.py index 6a24f3d6d0..d6e5d1478a 100644 --- a/argilla/src/argilla/_api/_vectors.py +++ b/argilla/src/argilla/_api/_vectors.py @@ -77,8 +77,6 @@ def list(self, dataset_id: UUID) -> List[VectorFieldModel]: #################### def _model_from_json(self, response_json: Dict) -> VectorFieldModel: - response_json["inserted_at"] = self._date_from_iso_format(date=response_json["inserted_at"]) - response_json["updated_at"] = self._date_from_iso_format(date=response_json["updated_at"]) return VectorFieldModel(**response_json) def _model_from_jsons(self, response_jsons: List[Dict]) -> List[VectorFieldModel]: diff --git a/argilla/src/argilla/_api/_workspaces.py b/argilla/src/argilla/_api/_workspaces.py index b657963dac..1bf80020c6 100644 --- a/argilla/src/argilla/_api/_workspaces.py +++ b/argilla/src/argilla/_api/_workspaces.py @@ -112,8 +112,8 @@ def _model_from_json(self, json_workspace: Dict) -> WorkspaceModel: return WorkspaceModel( id=UUID(json_workspace["id"]), name=json_workspace["name"], - inserted_at=self._date_from_iso_format(date=json_workspace["inserted_at"]), - updated_at=self._date_from_iso_format(date=json_workspace["updated_at"]), + inserted_at=json_workspace["inserted_at"], + updated_at=json_workspace["updated_at"], ) def _model_from_jsons(self, json_workspaces: List[Dict]) -> List[WorkspaceModel]: diff --git a/argilla/tests/unit/models/__init__.py b/argilla/tests/unit/models/__init__.py new file mode 100644 index 0000000000..749d13b87d --- /dev/null +++ b/argilla/tests/unit/models/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2024-present, Argilla, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + diff --git a/argilla/tests/unit/models/test_workspace_models.py b/argilla/tests/unit/models/test_workspace_models.py new file mode 100644 index 0000000000..2eb3aa1eec --- /dev/null +++ b/argilla/tests/unit/models/test_workspace_models.py @@ -0,0 +1,35 @@ +# Copyright 2024-present, Argilla, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from datetime import datetime + +from dateutil import tz + +from argilla._models import WorkspaceModel + + +class TestWorkspaceModels: + def test_create_workspace_with_isoformat_string(self): + workspace = WorkspaceModel( + name="workspace", + inserted_at="2024-12-12T09:44:24.989000Z", + updated_at="2024-12-12T09:44:24.989000Z", + ) + + expected_datetime = datetime(2024, 12, 12, 9, 44, 24, 989000, tzinfo=tz.tzutc()) + + assert workspace.name == "workspace" + + assert workspace.inserted_at == expected_datetime + assert workspace.updated_at == expected_datetime