Skip to content

ops.Relation.load() should perhaps be strict #2333

@dimaqq

Description

@dimaqq

Context:

In [24]: class C(BaseModel):
    ...:     foo: int
    ...:

In [25]: C(foo="42")
Out[25]: C(foo=42)

In [26]: C.model_validate({"foo": "42"})
Out[26]: C(foo=42)

In [27]: C.model_validate({"foo": "42"}, strict=True)
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[27], line 1
----> 1 C.model_validate({"foo": "42"}, strict=True)

File ~/.cache/uv/archive-v0/j9AUHBx4OV-OhSDXYcYzU/lib/python3.8/site-packages/pydantic/main.py:627, in BaseModel.model_validate(cls, obj, strict, from_attributes, context)
    625 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    626 __tracebackhide__ = True
--> 627 return cls.__pydantic_validator__.validate_python(
    628     obj, strict=strict, from_attributes=from_attributes, context=context
    629 )

ValidationError: 1 validation error for C
foo
  Input should be a valid integer [type=int_type, input_value='42', input_type=str]
    For further information visit https://errors.pydantic.dev/2.10/v/int_type

Today, we use Pydantic's "magic" .model_dump in ops.Relation.save() but we instantiate the type directly in ops.Relation.load().

If we used Pydantic's "magic" .model_validate, we could pass in strict=True and avoid type coersion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions