Skip to content

Commit 443aea8

Browse files
Update src/labthings_fastapi/properties.py
Swap the order of the two cases for clarity. Co-authored-by: Julian Stirling <julian@julianstirling.co.uk>
1 parent 8e5ba95 commit 443aea8

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/labthings_fastapi/properties.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -882,23 +882,23 @@ def validate(self, value: Any) -> Value:
882882
with its value type. This should never happen.
883883
"""
884884
try:
885-
if self.model is self.value_type and issubclass(self.value_type, BaseModel):
886-
# If there's no RootModel wrapper, the value_type is a model and may be
887-
# used directly for validation. The `issubclass` should not be
888-
# necessary, but it's helpful for `mypy` and it does no harm to check.
889-
return self.value_type.model_validate(value)
890-
elif issubclass(self.model, LabThingsRootModelWrapper):
885+
if issubclass(self.model, LabThingsRootModelWrapper):
891886
# If a plain type has been wrapped in a RootModel, use that to validate
892887
# and then set the property to the root value.
893888
model = self.model.model_validate(value)
894889
return model.root
895-
else:
896-
# This should be unreachable, because either `self.value_type` is
897-
# a BaseModel and so `value_type` and `model` are identical, or
898-
# `model` is a `LabThingsRootModelWrapper` wrapping the value type.
899-
msg = f"Property {self.name} has an inconsistent model. This is "
900-
msg += f"most likely a LabThings bug. {self.model=}, {self.value_type=}"
901-
raise TypeError(msg)
890+
891+
if issubclass(self.value_type, BaseModel) and self.model is self.value_type:
892+
# If there's no RootModel wrapper, the value was defined in code as a
893+
# Pydantic model. This means `value_type` and `model` should both
894+
# be that same class.
895+
return self.value_type.model_validate(value)
896+
897+
# This should be unreachable, because `model` is a `LabThingsRootModelWrapper`
898+
# wrapping the value type, or the values' type should be a BaseModel.
899+
msg = f"Property {self.name} has an inconsistent model. This is "
900+
msg += f"most likely a LabThings bug. {self.model=}, {self.value_type=}"
901+
raise TypeError(msg)
902902
except ValidationError:
903903
raise # This is needed for flake8 to be happy with the docstring
904904

0 commit comments

Comments
 (0)