Skip to content

fix: detect pydantic dataclasses from before 2.11 when mapping aliases - #2768

Merged
tonyandrewmeyer merged 3 commits into
canonical:mainfrom
tonyandrewmeyer:fix-juju-fields-pydantic-detection
Sep 25, 2026
Merged

tonyandrewmeyer merged 3 commits into
canonical:mainfrom
tonyandrewmeyer:fix-juju-fields-pydantic-detection

Conversation

@tonyandrewmeyer

@tonyandrewmeyer tonyandrewmeyer commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

_juju_fields checks 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, and Relation.load passes the wrong keyword to the class.

This switches to checking for __pydantic_validator__ in the class's __dict__, which is what pydantic.dataclasses.is_pydantic_dataclass itself checks, and which every pydantic 2 dataclass has.

Split out of #2741.

_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>
Comment thread ops/charm.py Resolved

@james-garner-canonical james-garner-canonical left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@tonyandrewmeyer
tonyandrewmeyer merged commit 102271d into canonical:main Sep 25, 2026
60 checks passed
@tonyandrewmeyer
tonyandrewmeyer deleted the fix-juju-fields-pydantic-detection branch September 25, 2026 00:04
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>
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