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

[DRAFT] [REFACTOR] Upgrade Pydantic to v2 #5508

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
136 changes: 106 additions & 30 deletions argilla-server/pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion argilla-server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ maintainers = [{ name = "argilla", email = "[email protected]" }]
dependencies = [
# Basic dependencies
"fastapi >= 0.103.1,< 1.0.0",
"pydantic >= 1.10.7,< 2.0",
"pydantic >= 2.9.1",
"pydantic-settings >= 2.5.2",
"uvicorn[standard] >= 0.15.0,< 0.25.0",
"opensearch-py ~= 2.0.0",
"elasticsearch8[async] ~= 8.7.0",
Expand Down
8 changes: 1 addition & 7 deletions argilla-server/src/argilla_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,5 @@
# 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.
import warnings

from argilla_server.pydantic_v1 import PYDANTIC_MAJOR_VERSION

if PYDANTIC_MAJOR_VERSION >= 2:
warnings.warn("The argilla_server package is not compatible with Pydantic 2. " "Please use Pydantic 1.x instead.")
else:
from argilla_server._app import app # noqa
from argilla_server._app import app # noqa
4 changes: 3 additions & 1 deletion argilla-server/src/argilla_server/api/handlers/v1/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
from argilla_server.enums import UserRole
from argilla_server.errors.future import NotFoundError
from argilla_server.models import User
from argilla_server.pydantic_v1 import Field

# from argilla_server.pydantic_v1 import Field
from pydantic import Field
from argilla_server.security.authentication.oauth2 import OAuth2ClientProvider
from argilla_server.security.authentication.userinfo import UserInfo
from argilla_server.security.settings import settings
Expand Down
6 changes: 4 additions & 2 deletions argilla-server/src/argilla_server/api/schemas/v1/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

from typing import Any, Dict, Set, Union

from argilla_server.pydantic_v1 import BaseModel, root_validator
# from argilla_server.pydantic_v1 import BaseModel, root_validator
from pydantic import model_validator, BaseModel


class UpdateSchema(BaseModel):
Expand All @@ -25,7 +26,8 @@ class UpdateSchema(BaseModel):

__non_nullable_fields__: Union[Set[str], None] = None

@root_validator(pre=True)
@model_validator(mode="before")
@classmethod
def validate_non_nullable_fields(cls, values: Dict[str, Any]) -> Dict[str, Any]:
if cls.__non_nullable_fields__ is None:
return values
Expand Down
37 changes: 24 additions & 13 deletions argilla-server/src/argilla_server/api/schemas/v1/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,44 @@
# 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, Literal, Optional, Union
from uuid import UUID
from typing_extensions import Annotated

from argilla_server.api.schemas.v1.commons import UpdateSchema
from argilla_server.enums import DatasetDistributionStrategy, DatasetStatus
from argilla_server.pydantic_v1 import BaseModel, Field, constr

# from argilla_server.pydantic_v1 import BaseModel, Field, constr
from pydantic import StringConstraints, ConfigDict, BaseModel, Field

try:
from typing import Annotated
except ImportError:
from typing_extensions import Annotated

DATASET_NAME_REGEX = r"^(?!-|_)[a-zA-Z0-9-_ ]+$"
# Pydantic v2 error: look-around, including look-ahead and look-behind, is not supported so rewriting it:
# DATASET_NAME_REGEX = r"^(?!-|_)[a-zA-Z0-9-_ ]+$"
DATASET_NAME_REGEX = r"^[a-zA-Z0-9 ][a-zA-Z0-9-_ ]*$"
DATASET_NAME_MIN_LENGTH = 1
DATASET_NAME_MAX_LENGTH = 200
DATASET_GUIDELINES_MIN_LENGTH = 1
DATASET_GUIDELINES_MAX_LENGTH = 10000

DatasetName = Annotated[
constr(regex=DATASET_NAME_REGEX, min_length=DATASET_NAME_MIN_LENGTH, max_length=DATASET_NAME_MAX_LENGTH),
Annotated[
str,
StringConstraints(
pattern=DATASET_NAME_REGEX, min_length=DATASET_NAME_MIN_LENGTH, max_length=DATASET_NAME_MAX_LENGTH
),
],
Field(..., description="Dataset name"),
]

DatasetGuidelines = Annotated[
constr(min_length=DATASET_GUIDELINES_MIN_LENGTH, max_length=DATASET_GUIDELINES_MAX_LENGTH),
Annotated[
str, StringConstraints(min_length=DATASET_GUIDELINES_MIN_LENGTH, max_length=DATASET_GUIDELINES_MAX_LENGTH)
],
Field(..., description="Dataset guidelines"),
]

Expand Down Expand Up @@ -105,7 +117,7 @@ class UsersProgress(BaseModel):
class Dataset(BaseModel):
id: UUID
name: str
guidelines: Optional[str]
guidelines: Optional[str] = None
allow_extra_metadata: bool
status: DatasetStatus
distribution: DatasetDistribution
Expand All @@ -114,8 +126,7 @@ class Dataset(BaseModel):
inserted_at: datetime
updated_at: datetime

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


class Datasets(BaseModel):
Expand All @@ -124,7 +135,7 @@ class Datasets(BaseModel):

class DatasetCreate(BaseModel):
name: DatasetName
guidelines: Optional[DatasetGuidelines]
guidelines: Optional[DatasetGuidelines] = None
allow_extra_metadata: bool = True
distribution: DatasetDistributionCreate = DatasetOverlapDistributionCreate(
strategy=DatasetDistributionStrategy.overlap,
Expand All @@ -134,9 +145,9 @@ class DatasetCreate(BaseModel):


class DatasetUpdate(UpdateSchema):
name: Optional[DatasetName]
guidelines: Optional[DatasetGuidelines]
allow_extra_metadata: Optional[bool]
distribution: Optional[DatasetDistributionUpdate]
name: Optional[DatasetName] = None
guidelines: Optional[DatasetGuidelines] = None
allow_extra_metadata: Optional[bool] = None
distribution: Optional[DatasetDistributionUpdate] = None

__non_nullable_fields__ = {"name", "allow_extra_metadata", "distribution"}
Loading
Loading