dapper91/pydantic-xml

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

Closed this issue · 2 comments

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!

Reverting to v2.6 seems to fix the issue

fixed in version 2.8.1.