Skip to content

Commit 8fdc757

Browse files
committed
Adapt to new Pydantic error
As I've upped the minimum pydantic version anyway, there's no need to support two versions - I've just updated the code to check for a `RuntimeError` instead of a `SchemaError`. I've also updated my test case, which no longer causes Pydantic to fail - I've found another combination that does cause the same error, and we now use that.
1 parent 821abbd commit 8fdc757

2 files changed

Lines changed: 5 additions & 10 deletions

File tree

src/labthings_fastapi/utilities/__init__.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from weakref import WeakSet
77
from pydantic import BaseModel, ConfigDict, Field, RootModel, create_model
88
from pydantic.dataclasses import dataclass
9-
from pydantic_core import SchemaError
109

1110
from labthings_fastapi.exceptions import UnsupportedConstraintError
1211
from .introspection import EmptyObject
@@ -129,7 +128,7 @@ def wrap_plain_types_in_rootmodel(
129128
:raises UnsupportedConstraintError: if constraints are provided for an
130129
unsuitable type, for example `allow_inf_nan` for an `int` property, or
131130
any constraints for a `BaseModel` subclass.
132-
:raises SchemaError: if other errors prevent Pydantic from creating a schema
131+
:raises RuntimeError: if other errors prevent Pydantic from creating a schema
133132
for the generated model.
134133
"""
135134
try: # This needs to be a `try` as basic types are not classes
@@ -148,13 +147,9 @@ def wrap_plain_types_in_rootmodel(
148147
root=(model, Field(**constraints)),
149148
__base__=LabThingsRootModelWrapper,
150149
)
151-
except SchemaError as e:
152-
for error in e.errors():
153-
if error["loc"][-1] in constraints:
154-
key = error["loc"][-1]
155-
raise UnsupportedConstraintError(
156-
f"Constraint {key} is not supported for type {model!r}."
157-
) from e
150+
except RuntimeError as e:
151+
if "Unable to apply constraint" in str(e):
152+
raise UnsupportedConstraintError(str(e)) from e
158153
raise e
159154

160155

tests/test_properties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def test_bad_property_constraints():
438438
"""Test that bad constraints raise errors at definition time."""
439439

440440
class BadConstraintThing(lt.Thing):
441-
bad_prop: int = lt.property(default=0, allow_inf_nan=False)
441+
bad_prop: str = lt.property(default="hello", allow_inf_nan=True)
442442

443443
# Some constraints cause errors when the model is built. So far
444444
# I believe only allow_inf_nan on int does this.

0 commit comments

Comments
 (0)