Skip to content

Commit

Permalink
Merge pull request #2246 from doccano/fix/pydantic
Browse files Browse the repository at this point in the history
Update pydantic version
  • Loading branch information
Hironsan committed Jul 20, 2023
2 parents c0093f0 + 67e9caf commit a82ed72
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 103 deletions.
2 changes: 1 addition & 1 deletion backend/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def main():

# Dispatch handler.
args = parser.parse_args()
if hasattr(args, "env_file") and Path(args.env_file).is_file():
if hasattr(args, "env_file") and args.env_file and Path(args.env_file).is_file():
env.read_env(args.env_file, recurse=False, override=True)
if hasattr(args, "handler"):
django.setup()
Expand Down
20 changes: 8 additions & 12 deletions backend/data_import/pipeline/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import uuid
from typing import Any, Optional

from pydantic import UUID4, BaseModel, ConstrainedStr, NonNegativeInt, root_validator
from pydantic import UUID4, BaseModel, NonNegativeInt, constr, root_validator

from .label_types import LabelTypes
from examples.models import Example
Expand All @@ -15,10 +15,6 @@
from projects.models import Project


class NonEmptyStr(ConstrainedStr):
min_length = 1


class Label(BaseModel, abc.ABC):
id: int = -1
uuid: UUID4
Expand Down Expand Up @@ -49,14 +45,14 @@ def __hash__(self):


class CategoryLabel(Label):
label: NonEmptyStr
label: constr(min_length=1) # type: ignore

def __lt__(self, other):
return self.label < other.label

@classmethod
def parse(cls, example_uuid: UUID4, obj: Any):
return cls(example_uuid=example_uuid, label=obj)
return cls(example_uuid=example_uuid, label=obj) # type: ignore

def create_type(self, project: Project) -> Optional[LabelType]:
return CategoryType(text=self.label, project=project)
Expand All @@ -66,14 +62,14 @@ def create(self, user, example: Example, types: LabelTypes, **kwargs):


class SpanLabel(Label):
label: NonEmptyStr
label: constr(min_length=1) # type: ignore
start_offset: NonNegativeInt
end_offset: NonNegativeInt

def __lt__(self, other):
return self.start_offset < other.start_offset

@root_validator
@root_validator(skip_on_failure=True)
def check_start_offset_is_less_than_end_offset(cls, values):
start_offset, end_offset = values.get("start_offset"), values.get("end_offset")
if start_offset >= end_offset:
Expand Down Expand Up @@ -105,14 +101,14 @@ def create(self, user, example: Example, types: LabelTypes, **kwargs):


class TextLabel(Label):
text: NonEmptyStr
text: constr(min_length=1) # type: ignore

def __lt__(self, other):
return self.text < other.text

@classmethod
def parse(cls, example_uuid: UUID4, obj: Any):
return cls(example_uuid=example_uuid, text=obj)
return cls(example_uuid=example_uuid, text=obj) # type: ignore

def create_type(self, project: Project) -> Optional[LabelType]:
return None
Expand All @@ -124,7 +120,7 @@ def create(self, user, example: Example, types: LabelTypes, **kwargs):
class RelationLabel(Label):
from_id: int
to_id: int
type: NonEmptyStr
type: constr(min_length=1) # type: ignore

def __lt__(self, other):
return self.from_id < other.from_id
Expand Down

0 comments on commit a82ed72

Please sign in to comment.