Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add round_trip=True as a default json serialization. #65

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

pesap
Copy link
Collaborator

@pesap pesap commented Jan 22, 2025

Some Pydantic models have computed fields (see example below) that should not be serialized. By default, .model_dump() includes these fields, which can cause deserialization errors since Component does not accept extra arguments.

The example below shows an example of a computed field that it is only meant to be used at run-time

from infrasys.component import Component

class TestComponent(Component):
    a : int
    b : int

    @computed_field
    @property
    def sum_a_b(self) -> int:
        """Create attribute that holds the class name."""
        return self.a + self.b

If we serialize using TestComponent using .model_dump(), sum_a_b is included, leading to errors when loading JSON. Setting round_trip=True in .model_dump() ensures computed fields are excluded from serialization.

@pesap pesap requested a review from daniel-thom January 22, 2025 23:51
@codecov-commenter
Copy link

codecov-commenter commented Jan 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.11%. Comparing base (91584d2) to head (bd6e2f9).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #65   +/-   ##
=======================================
  Coverage   96.11%   96.11%           
=======================================
  Files          36       36           
  Lines        2988     2988           
=======================================
  Hits         2872     2872           
  Misses        116      116           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pesap pesap marked this pull request as ready for review January 23, 2025 00:12
@pesap pesap merged commit df0bd49 into main Jan 23, 2025
6 checks passed
@pesap pesap deleted the ps/round_trip branch January 23, 2025 00:13
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.

3 participants