dapper91 / pydantic-xml

python xml for humans
https://pydantic-xml.readthedocs.io
The Unlicense
141 stars 14 forks source link

Parsing list[Union[...]], gets stuck if parsing the Union[] failed #164

Closed sorenhartmann closed 5 months ago

sorenhartmann commented 5 months ago

I have the following code, adapted from the Union example from the documentation

class Device(BaseXmlModel, tag='device'):
    type: str

class CPU(Device):
    type: Literal['CPU'] = attr()
    cores: int = element()

class GPU(Device):
    type: Literal['GPU'] = attr()
    cores: int = element()
    cuda: bool = attr(default=False)

class Hardware(BaseXmlModel, tag='hardware'):
    accelerators: list[Union[CPU, GPU]] 
    # accelerators: list[Union[CPU, GPU]] = Field(..., discriminator="type") # <- also stuck
    # accelerators: list[Union[CPU, GPU]] = element() # <- also stuck
    # accelerators: list[Union[CPU, GPU]] = element(discriminator="type") # <- also stuck

if __name__ == "__main__":

    hw = Hardware(
        accelerators=[
            CPU(type="CPU", cores=3),
            GPU(type="GPU", cores=2),
            GPU(type="GPU", cores=2, cuda=True),
        ]
    )
    hw.accelerators[0].type = "FOO"
    elem = hw.to_xml_tree()
    print(Hardware.from_xml_tree(elem))

I am new to this framework, so not sure if I am doing something wrong, but I would expect the code above to throw a parsing error, instead of hanging. Any help would be appreciated!

sorenhartmann commented 5 months ago

Reverting to v2.6 seems to fix the issue

dapper91 commented 5 months ago

fixed in version 2.8.1.