Repository navigation
fix: detect pydantic dataclasses from before 2.11 when mapping aliases - #2768
Merged
tonyandrewmeyer merged 3 commits intoSep 25, 2026
Merged
tonyandrewmeyer merged 3 commits into
tonyandrewmeyer merged 3 commits into
Conversation
_juju_fields decided whether a dataclass was a pydantic dataclass by checking for __is_pydantic_dataclass__, which only exists from pydantic 2.11. With an earlier pydantic 2, aliased fields on a pydantic dataclass were mapped back to their field names, so Relation.load passed the field name to a class that expects the alias. Check for __pydantic_validator__ in the class's __dict__ instead, which is what pydantic.dataclasses.is_pydantic_dataclass itself checks for, and which is present on every pydantic 2 dataclass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tonyandrewmeyer
commented
Sep 23, 2026
tonyandrewmeyer
marked this pull request as ready for review
September 23, 2026 22:41
tonyandrewmeyer
requested review from
MattPatchava and
james-garner-canonical
September 23, 2026 22:41
james-garner-canonical
approved these changes
Sep 23, 2026
james-garner-canonical
left a comment
Contributor
There was a problem hiding this comment.
Looks good, thanks for splitting this out.
tonyandrewmeyer
added a commit
to tonyandrewmeyer/operator
that referenced
this pull request
Sep 24, 2026
The __pydantic_validator__ check in _juju_fields fixes an existing bug that isn't specific to Relation.load coercion, so it's now in canonical#2768. The new guard in Relation.load keeps using __pydantic_validator__. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The comment said this was the attribute is_pydantic_dataclass() checks for, which stopped being true in pydantic 2.11. Also explain why the check looks in cls.__dict__ rather than using getattr. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MattPatchava
approved these changes
Sep 24, 2026
tonyandrewmeyer
added a commit
that referenced
this pull request
Oct 2, 2026
…nch (#2789) This cherry-picks fixes from main that have been merged since the last backport round (#2588): * #2636: `Relation.load()` only decodes the fields the data class uses, so loading from a unit databag no longer fails on the Juju-set `ingress-address` (and similar) keys. * #2768: pydantic dataclasses from before pydantic 2.11 are detected when mapping aliases. The 2.23 unit tests run against `pydantic~=2.10`, so this is more likely to bite here than on main. * #2700: converting an unknown status by name in `ops.testing` no longer raises a `TypeError`. * #2714: the tracing exporter says which destination rejected the data, and doesn't log a traceback for it. * #2628: `IdentityDict` accepts `IdentityAccess` for `access` (type annotation only). There were two small adjustments: * #2700 uses `warnings.warn()`, but `state.py` doesn't import `warnings` on this branch, so there's an extra commit adding the import. * #2714 conflicted, because main also wraps the response body read in `with e.fp:` (from a separate change) and has an unrelated SSL context test in the same module. I kept the existing body handling apart from limiting the read to 1000 bytes, and only ported the new HTTP error logging test. --------- Co-authored-by: Joel Nguemeta <joeledmond95@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
_juju_fieldschecks for__is_pydantic_dataclass__to decide whether a dataclass is a pydantic dataclass (which handles aliases itself), but that attribute only exists from pydantic 2.11. With an earlier pydantic 2, aliased fields get mapped back to their field names, andRelation.loadpasses the wrong keyword to the class.This switches to checking for
__pydantic_validator__in the class's__dict__, which is whatpydantic.dataclasses.is_pydantic_dataclassitself checks, and which every pydantic 2 dataclass has.Split out of #2741.