Closed mscarey closed 3 months ago
@mscarey Hi,
This error is caused not by the computed field. The library uses strict parsing mode by default, which means it respects elements order. You can find more info here. So in your case it expects element name
but finds greeting
. In the second case it just ignores the last element (greeting
).
To fix the problem use ordered
search mode:
class User(BaseXmlModel, search_mode='ordered'):
num: int = element()
name: str = element()
@computed_element
@property
def greeting(self) -> str:
return f"Hello, {self.name}"
Thanks so much for the help! Sorry to raise a false alarm.
I'm trying to parse some XML that has some fields computed from other fields. The problem is that some of the computed fields in this XML come before some of the non-computed fields. Pydantic-xml (version 2.9.0) doesn't seem to be able to see all the non-computed fields in this situation.
In this example,
test_make_user
will fail because it tries to load XML with the computed fieldgreeting
before the non-computed fieldname
. Buttest_make_user_name_first
passes.The error for
test_make_user
looks like this: