Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while parsing wrapped attribute #218

Open
ghilesmeddour opened this issue Oct 19, 2024 · 2 comments
Open

Error while parsing wrapped attribute #218

ghilesmeddour opened this issue Oct 19, 2024 · 2 comments
Labels
question Further information is requested

Comments

@ghilesmeddour
Copy link

Hi @dapper91 again 😊,

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?

@dapper91
Copy link
Owner

@ghilesmeddour Hi,

Namespaces are not inherited by nested models, so they must be explicitly defined for all models:

from typing import Optional
from uuid import UUID

from pydantic_xml import BaseXmlModel, attr, element, wrapped

NSMAP = {"": "urn:hl7-org:v3"}

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


class Code(BaseXmlModel, tag="code", nsmap=NSMAP, 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=NSMAP, 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"))

@dapper91 dapper91 added the question Further information is requested label Oct 19, 2024
@ghilesmeddour ghilesmeddour reopened this Oct 19, 2024
@ghilesmeddour
Copy link
Author

Thank you very much @dapper91 for your help.

I'll take the opportunity to ask another question. The input files can have different namespaces ([None, "urn:hl7-org:v1", "urn:hl7-org:v2", "urn:hl7-org:v3"]) and I want to parse them the same way. Is there any way to do this transparently, to ignore namespaces completely or to specify a namespace at parsing time (as a from_xml parameter for example) and not at models definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants