Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
from pydantic import Field

from overture.schema.system.field_constraint import UniqueItemsConstraint
from overture.schema.system.primitive import uint8
from overture.schema.system.primitive import int32

from .models import HierarchyItem

AdminLevel = NewType(
"AdminLevel",
Annotated[
uint8,
int32,
Field(
description="Integer representing the division's position in its country's administrative hierarchy, where lower numbers correspond to higher level administrative units.",
ge=0,
le=16,
),
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@
"properties": {
"admin_level": {
"description": "Integer representing the division's position in its country's administrative hierarchy, where lower numbers correspond to higher level administrative units.",
"maximum": 255,
"maximum": 16,
"minimum": 0,
"title": "Admin Level",
"type": "integer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@
"properties": {
"admin_level": {
"description": "Integer representing the division's position in its country's administrative hierarchy, where lower numbers correspond to higher level administrative units.",
"maximum": 255,
"maximum": 16,
"minimum": 0,
"title": "Admin Level",
"type": "integer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
"properties": {
"admin_level": {
"description": "Integer representing the division's position in its country's administrative hierarchy, where lower numbers correspond to higher level administrative units.",
"maximum": 255,
"maximum": 16,
"minimum": 0,
"title": "Admin Level",
"type": "integer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ def __init__(self, name: str | None = None):
)
self.__name = name

def __validate_instance(self, model_instance: BaseModel) -> BaseModel:
self.validate_instance(model_instance)
return model_instance

@property
def name(self) -> str:
"""Returns the name of the constraint, e.g. "FooConstraint" or "@foo"."""
Expand Down Expand Up @@ -126,6 +122,15 @@ def decorate(self, model_class: type[BaseModel]) -> type[BaseModel]:
metadata = Metadata.retrieve_from(model_class, Metadata()).copy() # type: ignore[union-attr]
model_constraints = (*ModelConstraint.get_model_constraints(model_class), self)
metadata[_MODEL_CONSTRAINT_KEY] = model_constraints
# Capture the constraint in a closure rather than passing a bound method.
# Some Pydantic versions unwrap bound methods passed through __validators__
# and rebind `self` to the model instance, breaking the dispatch.
constraint = self

def _after_validator(model_instance: BaseModel) -> BaseModel:
constraint.validate_instance(model_instance)
return model_instance

new_model_class = create_model(
model_class.__name__,
__config__=config,
Expand All @@ -135,7 +140,7 @@ def decorate(self, model_class: type[BaseModel]) -> type[BaseModel]:
__validators__={
self.name: cast(
Callable[..., Any],
model_validator(mode="after")(self.__validate_instance),
model_validator(mode="after")(_after_validator),
)
},
__metadata__=metadata,
Expand Down
Loading