Skip to content

Commit 639f2ff

Browse files
author
Val Brodsky
committed
Dealing with BaseModel
1 parent e090884 commit 639f2ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+160
-143
lines changed

libs/labelbox/src/labelbox/data/annotation_types/base_annotation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import abc
22
from uuid import UUID, uuid4
33
from typing import Any, Dict, Optional
4-
from labelbox import pydantic_compat
4+
from pydantic import BaseModel
55

66
from .feature import FeatureSchema
77

libs/labelbox/src/labelbox/data/annotation_types/classification/classification.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
except:
1010
from typing_extensions import Literal
1111

12-
from labelbox import pydantic_compat
12+
from pydantic import BaseModel
1313
from ..feature import FeatureSchema
1414

1515

1616
# TODO: Replace when pydantic adds support for unions that don't coerce types
17-
class _TempName(ConfidenceMixin, pydantic_compat.BaseModel):
17+
class _TempName(ConfidenceMixin, BaseModel):
1818
name: str
1919

2020
def dict(self, *args, **kwargs):
@@ -47,7 +47,7 @@ def dict(self, *args, **kwargs) -> Dict[str, str]:
4747
return res
4848

4949

50-
class Radio(ConfidenceMixin, CustomMetricsMixin, pydantic_compat.BaseModel):
50+
class Radio(ConfidenceMixin, CustomMetricsMixin, BaseModel):
5151
""" A classification with only one selected option allowed
5252
5353
>>> Radio(answer = ClassificationAnswer(name = "dog"))
@@ -66,7 +66,7 @@ class Checklist(_TempName):
6666
answer: List[ClassificationAnswer]
6767

6868

69-
class Text(ConfidenceMixin, CustomMetricsMixin, pydantic_compat.BaseModel):
69+
class Text(ConfidenceMixin, CustomMetricsMixin, BaseModel):
7070
""" Free form text
7171
7272
>>> Text(answer = "some text answer")

libs/labelbox/src/labelbox/data/annotation_types/data/base_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from abc import ABC
22
from typing import Optional, Dict, List, Any
33

4-
from labelbox import pydantic_compat
4+
from pydantic import BaseModel
55

66

7-
class BaseData(pydantic_compat.BaseModel, ABC):
7+
class BaseData(BaseModel, ABC):
88
"""
99
Base class for objects representing data.
1010
This class shouldn't directly be used

libs/labelbox/src/labelbox/data/annotation_types/data/generic_data_row_data.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Callable, Literal, Optional
22

3-
from labelbox import pydantic_compat
3+
from pydantic import BaseModel, model_validator
44
from labelbox.data.annotation_types.data.base_data import BaseData
55
from labelbox.utils import _NoCoercionMixin
66

@@ -14,7 +14,8 @@ class GenericDataRowData(BaseData, _NoCoercionMixin):
1414
def create_url(self, signer: Callable[[bytes], str]) -> Optional[str]:
1515
return self.url
1616

17-
@pydantic_compat.root_validator(pre=True)
17+
@model_validator(mode='before')
18+
@classmethod(pre=True)
1819
def validate_one_datarow_key_present(cls, data):
1920
keys = ['external_id', 'global_key', 'uid']
2021
count = sum([key in data for key in keys])

libs/labelbox/src/labelbox/data/annotation_types/data/raster.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
import requests
1010
import numpy as np
1111

12-
from labelbox import pydantic_compat
12+
from pydantic import BaseModel, model_validator
1313
from labelbox.exceptions import InternalServerError
1414
from .base_data import BaseData
1515
from ..types import TypedArray
1616

1717

18-
class RasterData(pydantic_compat.BaseModel, ABC):
18+
class RasterData(BaseModel, ABC):
1919
"""Represents an image or segmentation mask.
2020
"""
2121
im_bytes: Optional[bytes] = None
@@ -155,7 +155,8 @@ def create_url(self, signer: Callable[[bytes], str]) -> str:
155155
"One of url, im_bytes, file_path, arr must not be None.")
156156
return self.url
157157

158-
@pydantic_compat.root_validator()
158+
@model_validator(mode='before')
159+
@classmethod()
159160
def validate_args(cls, values):
160161
file_path = values.get("file_path")
161162
im_bytes = values.get("im_bytes")

libs/labelbox/src/labelbox/data/annotation_types/data/text.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from requests.exceptions import ConnectTimeout
55
from google.api_core import retry
66

7-
from labelbox import pydantic_compat
7+
from pydantic import BaseModel, model_validator
88
from labelbox.exceptions import InternalServerError
99
from labelbox.typing_imports import Literal
1010
from labelbox.utils import _NoCoercionMixin
@@ -90,7 +90,8 @@ def create_url(self, signer: Callable[[bytes], str]) -> None:
9090
"One of url, im_bytes, file_path, numpy must not be None.")
9191
return self.url
9292

93-
@pydantic_compat.root_validator
93+
@model_validator(mode='before')
94+
@classmethod
9495
def validate_date(cls, values):
9596
file_path = values.get("file_path")
9697
text = values.get("text")

libs/labelbox/src/labelbox/data/annotation_types/data/tiled_image.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from PIL import Image
1313
from pyproj import Transformer
1414
from pygeotile.point import Point as PygeoPoint
15-
from labelbox import pydantic_compat
15+
from pydantic import BaseModel, model_validator
1616

1717
from labelbox.data.annotation_types import Rectangle, Point, Line, Polygon
1818
from .base_data import BaseData
@@ -40,7 +40,7 @@ class EPSG(Enum):
4040
EPSG3857 = 3857
4141

4242

43-
class TiledBounds(pydantic_compat.BaseModel):
43+
class TiledBounds(BaseModel):
4444
""" Bounds for a tiled image asset related to the relevant epsg.
4545
4646
Bounds should be Point objects.
@@ -66,7 +66,8 @@ def validate_bounds_not_equal(cls, bounds):
6666
return bounds
6767

6868
#validate bounds are within lat,lng range if they are EPSG4326
69-
@pydantic_compat.root_validator
69+
@model_validator(mode='before')
70+
@classmethod
7071
def validate_bounds_lat_lng(cls, values):
7172
epsg = values.get('epsg')
7273
bounds = values.get('bounds')
@@ -82,7 +83,7 @@ def validate_bounds_lat_lng(cls, values):
8283
return values
8384

8485

85-
class TileLayer(pydantic_compat.BaseModel):
86+
class TileLayer(BaseModel):
8687
""" Url that contains the tile layer. Must be in the format:
8788
8889
https://c.tile.openstreetmap.org/{z}/{x}/{y}.png
@@ -352,7 +353,7 @@ def validate_zoom_levels(cls, zoom_levels):
352353
return zoom_levels
353354

354355

355-
class EPSGTransformer(pydantic_compat.BaseModel):
356+
class EPSGTransformer(BaseModel):
356357
"""Transformer class between different EPSG's. Useful when wanting to project
357358
in different formats.
358359
"""

libs/labelbox/src/labelbox/data/annotation_types/data/video.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .base_data import BaseData
1313
from ..types import TypedArray
1414

15-
from labelbox import pydantic_compat
15+
from pydantic import BaseModel, model_validator
1616

1717
logger = logging.getLogger(__name__)
1818

@@ -148,7 +148,8 @@ def frames_to_video(self,
148148
out.release()
149149
return file_path
150150

151-
@pydantic_compat.root_validator
151+
@model_validator(mode='before')
152+
@classmethod
152153
def validate_data(cls, values):
153154
file_path = values.get("file_path")
154155
url = values.get("url")

libs/labelbox/src/labelbox/data/annotation_types/feature.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from typing import Optional
22

3-
from labelbox import pydantic_compat
3+
from pydantic import BaseModel, model_validator
44

55
from .types import Cuid
66

77

8-
class FeatureSchema(pydantic_compat.BaseModel):
8+
class FeatureSchema(BaseModel):
99
"""
1010
Class that represents a feature schema.
1111
Could be a annotation, a subclass, or an option.
@@ -14,7 +14,8 @@ class FeatureSchema(pydantic_compat.BaseModel):
1414
name: Optional[str] = None
1515
feature_schema_id: Optional[Cuid] = None
1616

17-
@pydantic_compat.root_validator
17+
@model_validator(mode='before')
18+
@classmethod
1819
def must_set_one(cls, values):
1920
if values['feature_schema_id'] is None and values['name'] is None:
2021
raise ValueError(

libs/labelbox/src/labelbox/data/annotation_types/geometry/geometry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
import geojson
55
import numpy as np
6-
from labelbox import pydantic_compat
6+
from pydantic import BaseModel
77

88
from shapely import geometry as geom
99

1010

11-
class Geometry(pydantic_compat.BaseModel, ABC):
11+
class Geometry(BaseModel, ABC):
1212
"""Abstract base class for geometry objects
1313
"""
1414
extra: Dict[str, Any] = {}

0 commit comments

Comments
 (0)