Skip to content

fix: preserve polymorphic subtypes on parent-typed attributes (#58) - #63

Open
whawk46 wants to merge 1 commit into
finos:mainfrom
whawk46:fix/issue-58-preserve-polymorphic-subtypes
Open

whawk46 wants to merge 1 commit into
finos:mainfrom
whawk46:fix/issue-58-preserve-polymorphic-subtypes

Conversation

@whawk46

@whawk46 whawk46 commented Nov 6, 2026

Copy link
Copy Markdown

Fixes #58

Summary

Preserves concrete Rune subtype instances, subtype-only fields, and @type discriminators when assigned to or deserialized into attributes declared with a parent BaseDataClass type (both scalar and list attributes):

  1. Construction & Validation (BaseDataClass.__init_subclass__ / _wrap_polymorphic_validators):
    • Attaches a WrapValidator to fields declared with a BaseDataClass type annotation so that when a concrete subclass instance (e.g. Child where class Child(Parent)) or a dict carrying "@type" is supplied for a parent-typed attribute (or list[Parent] attribute), the concrete subclass instance is preserved rather than coerced/truncated into a plain Parent instance.
  2. Condition Evaluation (BaseDataClass.validate_model):
    • Ensures parent-level and subtype-level conditions can traverse preserved subtype instances without losing subtype attributes.
  3. Serialization (BaseDataClass._serialize_polymorphic_fields & _serialize_polymorphic_value):
    • Emits all subtype attributes via the runtime instance's model_dump() and injects "@type": orig_val._fqrtn() whenever the runtime value is a strict subclass of the declared field type (isinstance(orig_val, expected_cls) and type(orig_val) is not expected_cls).
  4. Deserialization (BaseDataClass.rune_deserialize):
    • Resolves nested "@type" discriminators via _resolve_subtypes() before model_validate() so round-tripping Holder(item=Child(...)).rune_serialize() through Holder.rune_deserialize(...) reconstructs the exact Child subtype instance.

Verification

  • Added unit and round-trip tests in test/test_polymorphic_subtypes.py covering scalar and list parent-typed attributes, subtype-specific fields, @type emission, and rune_deserialize reconstruction.
  • pytest: 359 passed, 20 skipped.

…tributes

Fixes finos#58

Signed-off-by: Walter Hawkins <wdhawkins46@gmail.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A subtype stored in an attribute of its parent type is turned into the parent type, and loses its own fields

1 participant