Skip to content

Subclassing a model with a different namespace map causes errors #315

Description

@seweissman

We recently upgraded from 2.14.3 to 2.20 and we are seeing some strange errors with subclasses. We subclass in our code so that we can parse XML files from a 3rd party API that are created with different versions of a namespace without having to duplicate our model definitions. Here is a simple example that demonstrates what I am seeing

doc.xml:

<Company trade-name="Beboop" xmlns:pd="http://www.company.com/prod">

    <hq:headquarters xmlns:hq="http://www.company.com/hq">
        <hq:country>US</hq:country>
        <hq:state>West Virginia</hq:state>
        <hq:city>Almost Heaven</hq:city>
    </hq:headquarters>

</Company>

model.py:

import pathlib

from pydantic_xml import BaseXmlModel, attr, element

NSMAPv1 = {
    "hq": "http://www.company.com/hq/v1",
    "pd": "http://www.company.com/prod",
}
NSMAP = {
    "hq": "http://www.company.com/hq",
    "pd": "http://www.company.com/prod",
}


class Headquarters(BaseXmlModel, ns="hq", nsmap=NSMAP):
    country: str = element()
    state: str = element()
    city: str = element()


class HeadquartersV1(Headquarters, ns="hq", nsmap=NSMAPv1):
    """V1 of headquarters"""

class Company(BaseXmlModel, tag="Company", nsmap=NSMAP):
    trade_name: str = attr(name="trade-name")
    headquarters: Headquarters


xml_doc = pathlib.Path("./doc.xml").read_text()
company = Company.from_xml(xml_doc)

HeadquartersV1 is defined as a subclass of Headquarters but it isn't used anywhere. When I run this code I get the error:

Traceback (most recent call last):
  File "/python3.14/site-packages/pydantic_xml/serializers/factories/model.py", line 234, in deserialize
    return self._model.model_validate(result, strict=False, context=context)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/python3.14/site-packages/pydantic/main.py", line 731, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        obj,
        ^^^^
    ...<5 lines>...
        by_name=by_name,
        ^^^^^^^^^^^^^^^^
    )
    ^
pydantic_core._pydantic_core.ValidationError: 1 validation error for Company
headquarters
  Field required [type=missing, input_value={'trade_name': 'Beboop'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.13/v/missing

It looks like pydantic is trying to validate the wrong section of the XML against the wrong model?

If I comment out the HeadquartersV1 class it runs fine and there are no errors. The expected behavior would be that this would run without errors, since the class is defined but not used.

I have experimented with a few different versions and it seems like this behavior started in 2.15.0.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions