Skip to content

A root-level field accepts a value its declaration refuses; a nested field raises #47

Open

Description

@trissim

What happens at 1fcdf12: update_parameter("ui.column_width", 4) on a nested field rebuilds the owning dataclass (_dataclass_ancestor_parameter_updates, src/objectstate/object_state.py:844), so a dataclass whose __post_init__ refuses the value raises and the state is left unchanged. For a field of the root object the function returns {} at its first line ("." not in param_name), nothing is constructed, the value is stored, subscribers are told it changed, and the refusal only appears later, at to_object() (:2607), far from the edit and after dirty_fields and every subscriber have already taken the bad value.

So whether an invalid edit is refused depends on how deep the field is.

Wanted: one rule for every depth: update_parameter constructs the object the new parameters describe before storing them, and raises without mutating when the declaration refuses it.

Workaround in Toad now: toad.forms.declared_views.DeclaredField.edit calls python_introspect.validation.validate_annotation_value on the field's annotation before update_parameter.

Acceptance: for @dataclass class Root(AnnotatedDataclassValidationMixin): count: Annotated[int, Ge(1)] = 3, ObjectState(Root()).update_parameter("count", 0) raises ValueError, parameters["count"] is still 3, no change callback fires and dirty_fields is empty.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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