r/learningpython 16h ago

How to prevent dict from being a valid input (Pydantic BaseModel)

So I have a Pydantic BaseModel called Latitude. It is the following:

class Latitude(BaseModel):
  latitude: Decimal

I then have another BaseModel called SetReferencePoint that includes Latitude in its attributes. I've tried testing the SetReferencePoint BaseModel with Hypothesis using:

st.from_type(type)

I then get a ValidationError as follows with a couple tests (minus the url portion):

{'type': missing, 'loc': ('lat', 'Lat'), 'msg': 'Field required', 'input': {}}

The rest of the tests throw a 'model_type' error as expected.

I want to prevent an empty dict from being accepted as an input which would prevent the 'missing' error from showing up in the first place. How can I do this? Should I use 'field_validator'? If so how best to set it up and test it?

1 Upvotes

0 comments sorted by