Skip to content

Commit

Permalink
[IO-1931][IO-1929] updates to versioning: PIL, Torch and python (#692)
Browse files Browse the repository at this point in the history
* updates to versioning: PIL, Torch and python

* testing matrix

* scikit

* updates to torch

* updating for torch poetry issue

* pyrpoject updates

* WIP: pydantic 2.0 updates

* WIP: everything but advanced filters

* advanced filter fixes

* removal of dict + parse_obj functions

* removal of parse_obj_as

* linting

* model_validate method instead of Model(...)
  • Loading branch information
Nathanjp91 authored Dec 19, 2023
1 parent a5294fc commit d0d0cc9
Show file tree
Hide file tree
Showing 46 changed files with 1,638 additions and 1,487 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/JOB_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand Down
24 changes: 13 additions & 11 deletions darwin/future/core/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations

from pathlib import Path
from typing import Callable, Dict, Optional
from typing import Any, Callable, Dict, Optional
from urllib.parse import urlparse

import requests
import yaml
from pydantic import BaseModel, root_validator, validator
from pydantic import BaseModel, ConfigDict, field_validator, model_validator
from requests.adapters import HTTPAdapter, Retry

from darwin.config import Config as OldConfig
Expand Down Expand Up @@ -34,14 +34,15 @@ class DarwinConfig(BaseModel):
default_team: Optional[Team], default team to make requests to
"""

api_key: Optional[str]
datasets_dir: Optional[Path]
api_key: Optional[str] = None
datasets_dir: Optional[Path] = None
api_endpoint: str
base_url: str
default_team: str
teams: Dict[str, TeamsConfig]

@validator("base_url")
@field_validator("base_url")
@classmethod
def validate_base_url(cls, v: str) -> str:
v = v.strip()
if not v.endswith("/"):
Expand All @@ -54,7 +55,8 @@ def validate_base_url(cls, v: str) -> str:
assert check.netloc, "base_url must contain a domain"
return v

@root_validator(pre=True)
@model_validator(mode="before")
@classmethod
def remove_global(cls, values: dict) -> dict:
if "global" not in values:
return values
Expand All @@ -63,8 +65,9 @@ def remove_global(cls, values: dict) -> dict:
values.update(global_conf)
return values

@root_validator()
def validate_defaults(cls, values: dict) -> dict:
@model_validator(mode="before")
@classmethod
def validate_defaults(cls, values: Any) -> Any:
if values["api_key"]:
return values
assert values["default_team"] in values["teams"]
Expand Down Expand Up @@ -101,7 +104,7 @@ def _default_api_endpoint() -> str:
def from_file(path: Path) -> DarwinConfig:
if path.suffix.lower() == ".yaml":
data = DarwinConfig._parse_yaml(path)
return DarwinConfig.parse_obj(data)
return DarwinConfig.model_validate(data)
else:
return DarwinConfig.parse_file(path)

Expand Down Expand Up @@ -141,8 +144,7 @@ def from_old(old_config: OldConfig) -> DarwinConfig:
datasets_dir=teams[default_team]["datasets_dir"],
)

class Config:
validate_assignment = True
model_config = ConfigDict(validate_assignment=True)


class Result(BaseModel):
Expand Down
6 changes: 2 additions & 4 deletions darwin/future/core/datasets/create_dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from pydantic import parse_obj_as

from darwin.future.core.client import ClientCore
from darwin.future.data_objects.dataset import DatasetCore

Expand Down Expand Up @@ -32,5 +30,5 @@ def create_dataset(api_client: ClientCore, name: str) -> DatasetCore:
"name": name,
},
)

return parse_obj_as(DatasetCore, response)
assert isinstance(response, dict)
return DatasetCore(**response)
6 changes: 2 additions & 4 deletions darwin/future/core/datasets/get_dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from pydantic import parse_obj_as

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import QueryString
from darwin.future.data_objects.dataset import DatasetCore
Expand Down Expand Up @@ -29,5 +27,5 @@ def get_dataset(api_client: ClientCore, dataset_id: str) -> DatasetCore:
"""

response = api_client.get("/datasets", QueryString({"id": str(dataset_id)}))

return parse_obj_as(DatasetCore, response)
assert isinstance(response, dict)
return DatasetCore.model_validate(response)
5 changes: 3 additions & 2 deletions darwin/future/core/datasets/list_datasets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List, Tuple

from pydantic import ValidationError, parse_obj_as
from pydantic import ValidationError

from darwin.future.core.client import ClientCore
from darwin.future.data_objects.dataset import DatasetCore
Expand Down Expand Up @@ -32,7 +32,8 @@ def list_datasets(
response = api_client.get("/datasets")
try:
for item in response:
datasets.append(parse_obj_as(DatasetCore, item))
assert isinstance(item, dict)
datasets.append(DatasetCore.model_validate(item))
except ValidationError as e:
errors.append(e)

Expand Down
8 changes: 4 additions & 4 deletions darwin/future/core/items/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Literal, Tuple, Union
from uuid import UUID

from pydantic import ValidationError, parse_obj_as
from pydantic import ValidationError

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import QueryString
Expand Down Expand Up @@ -108,7 +108,7 @@ def get_item(
"""
response = api_client.get(f"/v2/teams/{team_slug}/items/{item_id}", params)
assert isinstance(response, dict)
return parse_obj_as(ItemCore, response)
return ItemCore.model_validate(response)


def list_items(
Expand Down Expand Up @@ -149,7 +149,7 @@ def list_items(
for item in response["items"]:
assert isinstance(item, dict)
try:
items.append(parse_obj_as(ItemCore, item))
items.append(ItemCore.model_validate(item))
except ValidationError as e:
exceptions.append(e)
return items, exceptions
Expand Down Expand Up @@ -187,7 +187,7 @@ def list_folders(
folders: List[Folder] = []
for item in response["folders"]:
try:
folders.append(parse_obj_as(Folder, item))
folders.append(Folder.model_validate(item))
except ValidationError as e:
exceptions.append(e)
return folders, exceptions
2 changes: 1 addition & 1 deletion darwin/future/core/items/set_item_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def set_item_layout(
"""
if not isinstance(layout, ItemLayout):
try:
layout = ItemLayout(**layout)
layout = ItemLayout.model_validate(layout)
except (ValueError, ValidationError):
raise BadRequest("Invalid layout provided")

Expand Down
4 changes: 1 addition & 3 deletions darwin/future/core/properties/create.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import Optional, Union

from pydantic import parse_obj_as

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import JSONDict
from darwin.future.data_objects.properties import FullProperty
Expand Down Expand Up @@ -33,4 +31,4 @@ def create_property(
params = params.to_create_endpoint()
response = client.post(f"/v2/teams/{team_slug}/properties", data=params)
assert isinstance(response, dict)
return parse_obj_as(FullProperty, response)
return FullProperty.model_validate(response)
6 changes: 2 additions & 4 deletions darwin/future/core/properties/get.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import List, Optional, Union
from uuid import UUID

from pydantic import parse_obj_as

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import QueryString
from darwin.future.data_objects.properties import FullProperty
Expand Down Expand Up @@ -31,7 +29,7 @@ def get_team_properties(
team_slug = client.config.default_team
response = client.get(f"/v2/teams/{team_slug}/properties", query_string=params)
assert isinstance(response, dict)
return parse_obj_as(List[FullProperty], response.get("properties"))
return [FullProperty.model_validate(item) for item in response["properties"]]


def get_team_full_properties(
Expand Down Expand Up @@ -69,4 +67,4 @@ def get_property_by_id(
team_slug = client.config.default_team
response = client.get(f"/v2/teams/{team_slug}/properties/{str(property_id)}")
assert isinstance(response, dict)
return parse_obj_as(FullProperty, response)
return FullProperty.model_validate(response)
6 changes: 2 additions & 4 deletions darwin/future/core/properties/update.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import Optional, Union

from pydantic import parse_obj_as

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import JSONDict
from darwin.future.data_objects.properties import FullProperty, PropertyValue
Expand Down Expand Up @@ -36,7 +34,7 @@ def update_property(
del params["id"]
response = client.put(f"/v2/teams/{team_slug}/properties/{id}", data=params)
assert isinstance(response, dict)
return parse_obj_as(FullProperty, response)
return FullProperty.model_validate(response)


def update_property_value(
Expand Down Expand Up @@ -71,4 +69,4 @@ def update_property_value(
f"/v2/teams/{team_slug}/properties/{item_id}/property_values/{id}", data=params
)
assert isinstance(response, dict)
return parse_obj_as(PropertyValue, response)
return PropertyValue.model_validate(response)
4 changes: 2 additions & 2 deletions darwin/future/core/team/get_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_team(client: ClientCore, team_slug: Optional[str] = None) -> TeamCore:
if not team_slug:
team_slug = client.config.default_team
response = client.get(f"/teams/{team_slug}/")
return TeamCore.parse_obj(response)
return TeamCore.model_validate(response)


def get_team_members(
Expand All @@ -51,7 +51,7 @@ def get_team_members(
errors = []
for item in response:
try:
members.append(TeamMemberCore.parse_obj(item))
members.append(TeamMemberCore.model_validate(item))
except ValidationError as e:
errors.append(e)
return members, errors
4 changes: 0 additions & 4 deletions darwin/future/core/types/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ class TeamSlug(str):
min_length = 1
max_length = 256

@classmethod
def __get_validators__(cls): # type: ignore
yield cls.validate

@classmethod
def validate(cls, v: str) -> "TeamSlug":
assert (
Expand Down
7 changes: 7 additions & 0 deletions darwin/future/core/types/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from enum import Enum
from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar

from pydantic import field_validator

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import Stringable
from darwin.future.data_objects.page import Page
from darwin.future.exceptions import (
InvalidQueryFilter,
Expand Down Expand Up @@ -44,6 +47,10 @@ class QueryFilter(DefaultDarwin):
param: str
modifier: Optional[Modifier] = None

@field_validator("param")
def validate_param(cls, v: Stringable) -> str:
return str(v)

def filter_attr(self, attr: Any) -> bool: # type: ignore
caster: Callable[[str], Any] = type(attr) # type: ignore
param = caster(
Expand Down
8 changes: 2 additions & 6 deletions darwin/future/core/workflows/get_workflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import Optional

from pydantic import parse_obj_as

from darwin.future.core.client import ClientCore
from darwin.future.data_objects.workflow import WorkflowCore

Expand Down Expand Up @@ -36,7 +34,5 @@ def get_workflow(
"""
team_slug = team_slug or client.config.default_team
response = client.get(f"/v2/teams/{team_slug}/workflows/{workflow_id}")

workflow = parse_obj_as(WorkflowCore, response)

return workflow
assert isinstance(response, dict)
return WorkflowCore.model_validate(response)
8 changes: 4 additions & 4 deletions darwin/future/core/workflows/get_workflows.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import List, Optional

from pydantic import parse_obj_as

from darwin.future.core.client import ClientCore
from darwin.future.data_objects.workflow import WorkflowCore

Expand All @@ -11,5 +9,7 @@ def get_workflows(
) -> List[WorkflowCore]:
team_slug = team_slug or client.config.default_team
response = client.get(f"/v2/teams/{team_slug}/workflows?worker=false")

return [parse_obj_as(WorkflowCore, workflow) for workflow in response]
assert isinstance(response, list)
assert all(isinstance(workflow, dict) for workflow in response)
assert len(response) > 0, "No workflows found"
return [WorkflowCore.model_validate(workflow) for workflow in response]
2 changes: 1 addition & 1 deletion darwin/future/core/workflows/list_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def list_workflows(
response = client.get(f"/v2/teams/{team_slug}/workflows?worker=false")
list_of_workflows = WorkflowListValidator(list=response) # type: ignore
workflows = [
WorkflowCore.parse_obj(workflow) for workflow in list_of_workflows.list
WorkflowCore.model_validate(workflow) for workflow in list_of_workflows.list
]
except Exception as e:
exceptions.append(e)
Expand Down
Loading

0 comments on commit d0d0cc9

Please sign in to comment.