Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR] argilla server: using pydantic v2 #5666

Merged
merged 32 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
50b0e82
config: Update to pydantic v2
frascuchon Nov 5, 2024
2f53019
refactor: Update API schemas
frascuchon Nov 5, 2024
1a8539d
refactor: Import pydantic module
frascuchon Nov 5, 2024
34d5e30
chore: Remove unused files
frascuchon Nov 5, 2024
798db75
refactor: Convert to settingschema
frascuchon Nov 5, 2024
18e721e
chore: Remove v2 logic for APP
frascuchon Nov 5, 2024
43330fe
refactor: Review and adapt error handler
frascuchon Nov 5, 2024
a3050fa
tests: Adapt tests messages
frascuchon Nov 5, 2024
fb16594
fix: Add coerce 2 string for terms filter
frascuchon Nov 5, 2024
ff30ee1
chore: Redefine TextQuery as BaseModel with coerce
frascuchon Nov 5, 2024
79cfc93
refactor: review validator code
frascuchon Nov 5, 2024
6aec507
Merge branch 'develop' into refactor/argilla-server/using-pydanticV2
frascuchon Nov 6, 2024
d305ae4
Merge branch 'develop' into refactor/argilla-server/using-pydanticV2
frascuchon Nov 6, 2024
145ea38
Merge branch 'develop' into refactor/argilla-server/using-pydanticV2
frascuchon Nov 6, 2024
f346a84
[BUGFIX] `argilla frontend`: redirect after login (#5635)
frascuchon Nov 6, 2024
489c265
Merge branch 'develop' into refactor/argilla-server/using-pydanticV2
frascuchon Nov 6, 2024
06a666e
Merge branch 'develop' into refactor/argilla-server/using-pydanticV2
frascuchon Nov 8, 2024
9d758ab
Merge branch 'develop' into refactor/argilla-server/using-pydanticV2
frascuchon Nov 8, 2024
2c0ae2e
refactor: Migrate validatorsand deprecated methods/functions
frascuchon Nov 8, 2024
3d5f745
chore: Same with tests
frascuchon Nov 8, 2024
72975ff
update pdm.lock
frascuchon Nov 8, 2024
eb4234c
Merge branch 'develop' into refactor/argilla-server/using-pydanticV2
frascuchon Nov 11, 2024
612c721
Merge branch 'develop' into refactor/argilla-server/using-pydanticV2
frascuchon Nov 18, 2024
c92b407
Merge branch 'develop' into refactor/argilla-server/using-pydanticV2
frascuchon Nov 19, 2024
a29d185
chore: update pdm.lock hash
frascuchon Nov 19, 2024
764393d
suggestion: deb DB name
frascuchon Nov 19, 2024
e0e9538
chore: Add missing tests with empty chat values
frascuchon Nov 19, 2024
2862020
chore: Remove extra imports
frascuchon Nov 19, 2024
dd96edd
Merge branch 'develop' into refactor/argilla-server/using-pydanticV2
jfcalvo Nov 19, 2024
59d2122
fix: webhook models updated to use Pydantic v2
jfcalvo Nov 19, 2024
56d6830
fix: revert mistake
jfcalvo Nov 19, 2024
4bd1522
fix: return back model_dump for metadata properties settings
jfcalvo Nov 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions argilla-server/src/argilla_server/api/handlers/v1/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def create_webhook(
):
await authorize(current_user, WebhookPolicy.create)

return await webhooks.create_webhook(db, webhook_create.dict())
return await webhooks.create_webhook(db, webhook_create.model_dump())


@router.patch("/webhooks/{webhook_id}", response_model=WebhookSchema)
Expand All @@ -68,7 +68,7 @@ async def update_webhook(

await authorize(current_user, WebhookPolicy.update)

return await webhooks.update_webhook(db, webhook, webhook_update.dict(exclude_unset=True))
return await webhooks.update_webhook(db, webhook, webhook_update.model_dump(exclude_unset=True))


@router.delete("/webhooks/{webhook_id}", response_model=WebhookSchema)
Expand Down
57 changes: 42 additions & 15 deletions argilla-server/src/argilla_server/api/schemas/v1/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from uuid import UUID
from datetime import datetime
from typing import List, Optional
from uuid import UUID

from pydantic import BaseModel, Field, HttpUrl, ConfigDict, field_validator, field_serializer

from argilla_server.webhooks.v1.enums import WebhookEvent
from argilla_server.api.schemas.v1.commons import UpdateSchema
from argilla_server.pydantic_v1 import BaseModel, Field, HttpUrl


WEBHOOK_EVENTS_MIN_ITEMS = 1
WEBHOOK_DESCRIPTION_MIN_LENGTH = 1
Expand All @@ -31,12 +33,11 @@
secret: str
events: List[WebhookEvent]
enabled: bool
description: Optional[str]
description: Optional[str] = None
inserted_at: datetime
updated_at: datetime

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class Webhooks(BaseModel):
Expand All @@ -45,26 +46,52 @@

class WebhookCreate(BaseModel):
url: HttpUrl
events: List[WebhookEvent] = Field(
min_items=WEBHOOK_EVENTS_MIN_ITEMS,
unique_items=True,
)
events: List[WebhookEvent] = Field(min_length=WEBHOOK_EVENTS_MIN_ITEMS)
description: Optional[str] = Field(
None,
min_length=WEBHOOK_DESCRIPTION_MIN_LENGTH,
max_length=WEBHOOK_DESCRIPTION_MAX_LENGTH,
)

@field_validator("events")
@classmethod
def events_must_be_unique(cls, events: List[WebhookEvent]):
if len(set(events)) != len(events):
raise ValueError("Events must be unique")

return events

@field_serializer("url")
def serialize_url(self, url: HttpUrl):
return str(url)


class WebhookUpdate(UpdateSchema):
url: Optional[HttpUrl]
events: Optional[List[WebhookEvent]] = Field(
min_items=WEBHOOK_EVENTS_MIN_ITEMS,
unique_items=True,
)
enabled: Optional[bool]
url: Optional[HttpUrl] = None
events: Optional[List[WebhookEvent]] = Field(None, min_length=WEBHOOK_EVENTS_MIN_ITEMS)
enabled: Optional[bool] = None
description: Optional[str] = Field(
None,
min_length=WEBHOOK_DESCRIPTION_MIN_LENGTH,
max_length=WEBHOOK_DESCRIPTION_MAX_LENGTH,
)

__non_nullable_fields__ = {"url", "events", "enabled"}

@field_validator("events")
@classmethod
def events_must_be_unique(cls, events: Optional[List[WebhookEvent]]):
if events is None:
return None

Check warning on line 85 in argilla-server/src/argilla_server/api/schemas/v1/webhooks.py

View check run for this annotation

Codecov / codecov/patch

argilla-server/src/argilla_server/api/schemas/v1/webhooks.py#L85

Added line #L85 was not covered by tests

if len(set(events)) != len(events):
raise ValueError("Events must be unique")

return events

@field_serializer("url")
def serialize_url(self, url: Optional[HttpUrl]):
if url is None:
return None

Check warning on line 95 in argilla-server/src/argilla_server/api/schemas/v1/webhooks.py

View check run for this annotation

Codecov / codecov/patch

argilla-server/src/argilla_server/api/schemas/v1/webhooks.py#L95

Added line #L95 was not covered by tests

return str(url)
2 changes: 1 addition & 1 deletion argilla-server/src/argilla_server/contexts/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ async def create_metadata_property(
db,
name=metadata_property_create.name,
title=metadata_property_create.title,
settings=metadata_property_create.settings.dict(),
settings=metadata_property_create.settings.model_dump(),
jfcalvo marked this conversation as resolved.
Show resolved Hide resolved
allowed_roles=_allowed_roles_for_metadata_property_create(metadata_property_create),
dataset_id=dataset.id,
)
Expand Down
2 changes: 1 addition & 1 deletion argilla-server/src/argilla_server/webhooks/v1/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ async def build_dataset_event(db: AsyncSession, dataset_event: DatasetEvent, dat
return Event(
event=dataset_event,
timestamp=datetime.utcnow(),
data=DatasetEventSchema.from_orm(dataset).dict(),
data=DatasetEventSchema.model_validate(dataset).model_dump(),
)
2 changes: 1 addition & 1 deletion argilla-server/src/argilla_server/webhooks/v1/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ async def build_record_event(db: AsyncSession, record_event: RecordEvent, record
return Event(
event=record_event,
timestamp=datetime.utcnow(),
data=RecordEventSchema.from_orm(record).dict(),
data=RecordEventSchema.model_validate(record).model_dump(),
)
2 changes: 1 addition & 1 deletion argilla-server/src/argilla_server/webhooks/v1/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ async def build_response_event(db: AsyncSession, response_event: ResponseEvent,
return Event(
event=response_event,
timestamp=datetime.utcnow(),
data=ResponseEventSchema.from_orm(response).dict(),
data=ResponseEventSchema.model_validate(response).model_dump(),
)
39 changes: 15 additions & 24 deletions argilla-server/src/argilla_server/webhooks/v1/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@
from typing import Optional, List
from datetime import datetime

from argilla_server.pydantic_v1 import BaseModel, Field
from pydantic import BaseModel, Field, ConfigDict


class UserEventSchema(BaseModel):
id: UUID
first_name: str
last_name: Optional[str]
last_name: Optional[str] = None
username: str
role: str
inserted_at: datetime
updated_at: datetime

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class WorkspaceEventSchema(BaseModel):
Expand All @@ -38,22 +37,20 @@ class WorkspaceEventSchema(BaseModel):
inserted_at: datetime
updated_at: datetime

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class DatasetQuestionEventSchema(BaseModel):
id: UUID
name: str
title: str
description: Optional[str]
description: Optional[str] = None
required: bool
settings: dict
inserted_at: datetime
updated_at: datetime

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class DatasetFieldEventSchema(BaseModel):
Expand All @@ -65,8 +62,7 @@ class DatasetFieldEventSchema(BaseModel):
inserted_at: datetime
updated_at: datetime

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class DatasetMetadataPropertyEventSchema(BaseModel):
Expand All @@ -78,8 +74,7 @@ class DatasetMetadataPropertyEventSchema(BaseModel):
inserted_at: datetime
updated_at: datetime

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class DatasetVectorSettingsEventSchema(BaseModel):
Expand All @@ -90,14 +85,13 @@ class DatasetVectorSettingsEventSchema(BaseModel):
inserted_at: datetime
updated_at: datetime

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class DatasetEventSchema(BaseModel):
id: UUID
name: str
guidelines: Optional[str]
guidelines: Optional[str] = None
allow_extra_metadata: bool
status: str
distribution: dict
Expand All @@ -110,8 +104,7 @@ class DatasetEventSchema(BaseModel):
inserted_at: datetime
updated_at: datetime

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class RecordEventSchema(BaseModel):
Expand All @@ -121,7 +114,7 @@ class RecordEventSchema(BaseModel):
# Or find another possible solution.
fields: dict
metadata: Optional[dict] = Field(None, alias="metadata_")
external_id: Optional[str]
external_id: Optional[str] = None
# TODO:
# responses:
# - Create a new `GET /api/v1/records/{record_id}/responses` endpoint.
Expand All @@ -142,18 +135,16 @@ class RecordEventSchema(BaseModel):
inserted_at: datetime
updated_at: datetime

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class ResponseEventSchema(BaseModel):
id: UUID
values: Optional[dict]
values: Optional[dict] = None
status: str
record: RecordEventSchema
user: UserEventSchema
inserted_at: datetime
updated_at: datetime

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,8 @@ async def test_create_webhook_without_authentication(self, db: AsyncSession, asy
[
"",
"example.com",
"http:example.com",
"https:example.com",
"http://localhost/webhooks",
"http://localhost:3000/webhooks",
"http.example.com",
"https.example.com",
],
)
async def test_create_webhook_with_invalid_url(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ async def test_update_webhook_without_authentication(self, async_client: AsyncCl
assert webhook.url != "https://example.com/webhook"
assert webhook.events != [WebhookEvent.response_updated]

@pytest.mark.parametrize("invalid_url", ["", "example.com", "http:example.com", "https:example.com"])
@pytest.mark.parametrize("invalid_url", ["", "example.com", "http.example.com", "https.example.com"])
async def test_update_webhook_with_invalid_url(
self, async_client: AsyncClient, owner_auth_header: dict, invalid_url: str
):
Expand Down
Loading