Skip to content
Merged

Dev #329

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

2.21.1 (2026-08-08)
-------------------

- fix: computed field inheritance with pydantic 2.13


2.21.0 (2026-05-17)
-------------------

Expand Down
9 changes: 9 additions & 0 deletions pydantic_xml/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ def __post_init__(self) -> None:
if config.REGISTER_NS_PREFIXES and self.nsmap:
utils.register_nsmap(self.nsmap)

def __copy__(self) -> 'ComputedXmlEntityInfo':
return dc.replace(
self,
examples=self.examples.copy() if isinstance(self.examples, list) else self.examples,
json_schema_extra=self.json_schema_extra.copy()
if isinstance(self.json_schema_extra, dict)
else self.json_schema_extra,
)


PropertyT = typing.TypeVar('PropertyT')

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pydantic-xml"
version = "2.21.0"
version = "2.21.1"
description = "pydantic xml extension"
authors = ["Dmitry Pershin <dapper1291@gmail.com>"]
license = "Unlicense"
Expand Down
34 changes: 34 additions & 0 deletions tests/test_computed_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ def attr2(self) -> str:
assert_xml_equal(actual_xml, xml)


def test_inherited_computed_attr():
class BaseModel(BaseXmlModel):
@computed_attr
def attr(self) -> str:
return 'text'

class TestModel(BaseModel, tag='model'):
pass

xml = '''
<model attr="text" />
'''

assert_xml_equal(TestModel().to_xml(), xml)


def test_computed_elements():
class TestModel(BaseXmlModel, tag='model'):
@computed_element(tag='element1')
Expand All @@ -65,6 +81,24 @@ def element2(self) -> str:
assert_xml_equal(actual_xml, xml)


def test_inherited_computed_element():
class BaseModel(BaseXmlModel):
@computed_element
def element(self) -> str:
return 'text'

class TestModel(BaseModel, tag='model'):
pass

xml = '''
<model>
<element>text</element>
</model>
'''

assert_xml_equal(TestModel().to_xml(), xml)


def test_computed_nillable_elements():
class TestModel(BaseXmlModel, tag='model'):
@computed_element(tag='element1', nillable=True)
Expand Down
Loading