Skip to content

Fix computed field inheritance with pydantic 2.13 - #328

Open
noamkush wants to merge 1 commit into
dapper91:masterfrom
noamkush:fix/pydantic-213-computed-field-copy
Open

Fix computed field inheritance with pydantic 2.13#328
noamkush wants to merge 1 commit into
dapper91:masterfrom
noamkush:fix/pydantic-213-computed-field-copy

Conversation

@noamkush

@noamkush noamkush commented Aug 3, 2026

Copy link
Copy Markdown

This fixes an issue we bumped into when trying to update to pydantic 2.13. See the tests - any use of computed_attr or computed_element in a base class results in an exception (on import). Both tests fail with current master branch. The implementation uses dataclasses.replace which differs from the base implementation to avoid repeating the many fields that need to be copied. This would probably be easier to maintain.

I used an LLM to make the code changes.

The error's stack trace looks like this

    class DerivedModel(SomeXmlBase, abc.ABC):
    ...<5 lines>...
  File "/home/noam/work/hl/django_app_be/.venv/lib/python3.14/site-packages/pydantic_xml/model.py", line 111, in __new__
    cls = typing.cast(Type['BaseXmlModel'], super().__new__(mcls, name, bases, namespace, **kwargs))
                                            ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/noam/work/hl/django_app_be/.venv/lib/python3.14/site-packages/pydantic/_internal/_model_construction.py", line 176, in __new__
    cls.__pydantic_decorators__ = DecoratorInfos.build(cls, replace_wrapped_methods=True)
                                  ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/noam/work/hl/django_app_be/.venv/lib/python3.14/site-packages/pydantic/_internal/_decorators.py", line 469, in build
    res.computed_fields.update({k: v.bind_to_cls(typ) for k, v in existing.computed_fields.items()})
                                   ~~~~~~~~~~~~~^^^^^
  File "/home/noam/work/hl/django_app_be/.venv/lib/python3.14/site-packages/pydantic/_internal/_decorators.py", line 289, in bind_to_cls
    info=copy(self.info),
         ~~~~^^^^^^^^^^^
  File "/home/noam/.local/share/uv/python/cpython-3.14.5-linux-x86_64-gnu/lib/python3.14/copy.py", line 82, in copy
    return copier(x)
  File "/home/noam/work/hl/django_app_be/.venv/lib/python3.14/site-packages/pydantic/fields.py", line 1588, in __copy__
    return type(self)(
           ~~~~~~~~~~^
        wrapped_property=self.wrapped_property,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<12 lines>...
        repr=self.repr,
        ^^^^^^^^^^^^^^^
    )
    ^
TypeError: ComputedXmlEntityInfo.__init__() missing 6 required positional arguments: 'location', 'path', 'ns', 'nsmap', 'nillable', and 'wrapped'

@codecov-commenter

codecov-commenter commented Aug 7, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.22%. Comparing base (d3c727f) to head (b9d8b4a).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #328   +/-   ##
=======================================
  Coverage   92.21%   92.22%           
=======================================
  Files          31       31           
  Lines        1786     1788    +2     
=======================================
+ Hits         1647     1649    +2     
  Misses        139      139           
Flag Coverage Δ
unittests 92.22% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants