Skip to content

Commit

Permalink
Make BaseConstraint generic
Browse files Browse the repository at this point in the history
  • Loading branch information
roll committed Apr 18, 2024
1 parent 4a423b1 commit 02340a7
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 15 deletions.
8 changes: 5 additions & 3 deletions dplib/models/field/constraints/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

from typing import Any, List, Optional
from typing import Generic, List, Optional, TypeVar, Union

from ....system import Model

NativeType = TypeVar("NativeType")

class BaseConstraints(Model):

class BaseConstraints(Model, Generic[NativeType]):
required: Optional[bool] = None
unique: Optional[bool] = None
enum: Optional[List[Any]] = None
enum: Optional[List[Union[str, NativeType]]] = None
2 changes: 1 addition & 1 deletion dplib/models/field/constraints/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
from .base import BaseConstraints


class CollectionConstraints(BaseConstraints):
class CollectionConstraints(BaseConstraints[str]):
minLength: Optional[int] = None
maxLength: Optional[int] = None
12 changes: 6 additions & 6 deletions dplib/models/field/constraints/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

from .base import BaseConstraints

ValueType = TypeVar("ValueType")
NativeType = TypeVar("NativeType")


# TODO: tweak serialization if needed
class ValueConstraints(BaseConstraints, Generic[ValueType]):
minimum: Optional[Union[str, ValueType]] = None
maximum: Optional[Union[str, ValueType]] = None
exclusiveMinimum: Optional[Union[str, ValueType]] = None
exclusiveMaximum: Optional[Union[str, ValueType]] = None
class ValueConstraints(BaseConstraints[NativeType], Generic[NativeType]):
minimum: Optional[Union[str, NativeType]] = None
maximum: Optional[Union[str, NativeType]] = None
exclusiveMinimum: Optional[Union[str, NativeType]] = None
exclusiveMaximum: Optional[Union[str, NativeType]] = None
2 changes: 1 addition & 1 deletion dplib/models/field/datatypes/any.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class AnyField(BaseField):

type: Literal["any"] = "any"
format: Optional[Literal["default"]] = None
constraints: BaseConstraints = pydantic.Field(default_factory=BaseConstraints)
constraints: BaseConstraints[str] = pydantic.Field(default_factory=BaseConstraints)
2 changes: 1 addition & 1 deletion dplib/models/field/datatypes/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BooleanField(BaseField):

type: Literal["boolean"] = "boolean"
format: Optional[Literal["default"]] = None
constraints: BaseConstraints = pydantic.Field(default_factory=BaseConstraints)
constraints: BaseConstraints[bool] = pydantic.Field(default_factory=BaseConstraints)

trueValues: List[str] = ["true", "True", "TRUE", "1"]
"""
Expand Down
2 changes: 1 addition & 1 deletion dplib/models/field/datatypes/geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class GeojsonField(BaseField):

type: Literal["geojson"] = "geojson"
format: Optional[IGeojsonFormat] = None
constraints: BaseConstraints = pydantic.Field(default_factory=BaseConstraints)
constraints: BaseConstraints[str] = pydantic.Field(default_factory=BaseConstraints)
2 changes: 1 addition & 1 deletion dplib/models/field/datatypes/geopoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class GeopointField(BaseField):

type: Literal["geopoint"] = "geopoint"
format: Optional[IGeojsonFormat] = None
constraints: BaseConstraints = pydantic.Field(default_factory=BaseConstraints)
constraints: BaseConstraints[str] = pydantic.Field(default_factory=BaseConstraints)
2 changes: 1 addition & 1 deletion dplib/models/field/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class Field(BaseField):

type: Literal[None] = None
format: Optional[Literal["default"]] = None
constraints: BaseConstraints = pydantic.Field(default_factory=BaseConstraints)
constraints: BaseConstraints[str] = pydantic.Field(default_factory=BaseConstraints)

0 comments on commit 02340a7

Please sign in to comment.