pydantic-xml icon indicating copy to clipboard operation
pydantic-xml copied to clipboard

Error while parsing wrapped attribute

Open ghilesmeddour opened this issue 1 year ago • 2 comments

Hi @dapper91 again :blush:,

I'm trying to use the lib to parse HL7 aECG files.

Let's take this file as an example.

I have the following program:

from typing import Optional
from urllib.request import urlopen
from uuid import UUID

from pydantic_xml import BaseXmlModel, attr, element, wrapped


class EffectiveTime(BaseXmlModel, tag="effectiveTime", search_mode="unordered"):
    center: str = wrapped("center", attr(name="value"))


class Code(BaseXmlModel, tag="code", search_mode="unordered"):
    code: str = attr()
    code_system: str = attr(name="codeSystem")
    code_system_name: Optional[str] = attr(name="codeSystemName", default=None)
    display_name: Optional[str] = attr(name="displayName", default=None)


class AnnotatedECG(BaseXmlModel, nsmap={"": "urn:hl7-org:v3"}, search_mode="unordered"):
    id: UUID = wrapped("id", attr(name="root"))
    code: Code
    text: Optional[str] = element(default=None)
    effective_time: EffectiveTime

    # This is working
    # center: str = wrapped("effectiveTime/center", attr(name="value"))


file_url = "https://raw.githubusercontent.com/FDA/aecg-python/refs/heads/main/src/aecg/data/hl7/2003-12%20Schema/example/Example%20aECG.xml"

with urlopen(file_url) as f:
    xml_doc = f.read()

aecg_o = AnnotatedECG.from_xml(xml_doc)

print(aecg_o)

And I have the following error:

pydantic_core._pydantic_core.ValidationError: 1 validation error for AnnotatedECG
effective_time.center
  [line -1]: Field required [type=missing, input_value={}, input_type=dict]

I can't understand why the error occurs (especially since the commented line, which is equivalent for me, works correctly). What am I doing wrong?

ghilesmeddour avatar Oct 19 '24 12:10 ghilesmeddour