Skip to content

Commit

Permalink
[BUGFIX] [TESTS] Remove custom isoformat parsing and let pydantic do …
Browse files Browse the repository at this point in the history
…the work (#5752)

# Description
<!-- Please include a summary of the changes and the related issue.
Please also include relevant motivation and context. List any
dependencies that are required for this change. -->


**Type of change**
<!-- Please delete options that are not relevant. Remember to title the
PR according to the type of change -->

- Bug fix (non-breaking change which fixes an issue)

**How Has This Been Tested**
<!-- Please add some reference about how your feature has been tested.
-->

**Checklist**
<!-- Please go over the list and make sure you've taken everything into
account -->

- 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/)
  • Loading branch information
frascuchon authored Dec 12, 2024
1 parent e7f46cc commit a74d3eb
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 26 deletions.
10 changes: 0 additions & 10 deletions argilla/src/argilla/_api/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
2 changes: 0 additions & 2 deletions argilla/src/argilla/_api/_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand Down
2 changes: 0 additions & 2 deletions argilla/src/argilla/_api/_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
2 changes: 0 additions & 2 deletions argilla/src/argilla/_api/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
2 changes: 0 additions & 2 deletions argilla/src/argilla/_api/_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
2 changes: 0 additions & 2 deletions argilla/src/argilla/_api/_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions argilla/src/argilla/_api/_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
2 changes: 0 additions & 2 deletions argilla/src/argilla/_api/_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
4 changes: 2 additions & 2 deletions argilla/src/argilla/_api/_workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
14 changes: 14 additions & 0 deletions argilla/tests/unit/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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.

35 changes: 35 additions & 0 deletions argilla/tests/unit/models/test_workspace_models.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a74d3eb

Please sign in to comment.