Skip to content

hide_input_in_errors is ignored by from_xml_tree #307

@vancoykendall

Description

@vancoykendall

If I set hide_input_in_errors=True on a BaseXmlModel's model_config, ValidationErrors raised by from_xml_tree (and from_xml) don't remove the input. The issue stems from the ModelSerializer and RootSerializer using these util functions to rebuild ValidationErrors.

Minimal Example:

from pydantic import ConfigDict
from pydantic_xml import BaseXmlModel, element

class MyModel(BaseXmlModel):
    model_config = ConfigDict(hide_input_in_errors=True)
    a: int = element()

MyModel.from_xml("<MyModel><a>bad input</a></MyModel>")

traceback

Traceback (most recent call last):
  File "/Users/vancoykendall/.cache/uv/archive-v0/QihtkkAxr79L-UD496U5e/lib/python3.12/site-packages/pydantic_xml/serializers/factories/model.py", line 219, in deserialize
    return self._model.model_validate(result, strict=False, context=context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vancoykendall/.cache/uv/archive-v0/QihtkkAxr79L-UD496U5e/lib/python3.12/site-packages/pydantic/main.py", line 716, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for MyModel
a
  Input should be a valid integer, unable to parse string as an integer [type=int_parsing]
    For further information visit https://errors.pydantic.dev/2.12/v/int_parsing

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/vancoykendall/.cache/uv/archive-v0/QihtkkAxr79L-UD496U5e/lib/python3.12/site-packages/pydantic_xml/model.py", line 290, in from_xml
    return cls.from_xml_tree(etree.fromstring(source, **kwargs), context=context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vancoykendall/.cache/uv/archive-v0/QihtkkAxr79L-UD496U5e/lib/python3.12/site-packages/pydantic_xml/model.py", line 264, in from_xml_tree
    ModelT, cls.__xml_serializer__.deserialize(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vancoykendall/.cache/uv/archive-v0/QihtkkAxr79L-UD496U5e/lib/python3.12/site-packages/pydantic_xml/serializers/factories/model.py", line 221, in deserialize
    raise utils.set_validation_error_sourceline(err, sourcemap)
pydantic_core._pydantic_core.ValidationError: 1 validation error for MyModel
a
  [line -1]: Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='bad input', input_type=str]

So it raises 2 ValidationErrors, one from the other and one respects the model config and one doesn't.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions