dapper91 / pydantic-xml

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

High verbosity in error messages #46

Closed kristiangronberg closed 1 year ago

kristiangronberg commented 1 year ago

I noticed that the error messages have changed from pydantic-xml version 0.5 to 0.6.0 in that they bring up serialisation and deserialisation.

When using the example from the text documentation but changing the type from 'str' to 'int' for the field "description" gives these two error messages depending on version of pydantic-xml .

Would it be possible to decrease the verbosity of the error messages in general? (Running the same code but with 'str' gives the expected Json output.)

from pydantic_xml import BaseXmlModel

xml_string = """<Company>
    Space Exploration Technologies Corp.
</Company>
"""

class Company(BaseXmlModel):
    class Config:
        anystr_strip_whitespace = True  # to strip text whitespaces

    description: int

data_model = Company.from_xml(xml_string)

pydantic-xml 0.5.0 Output:

Traceback (most recent call last):
  File "/Users/user/Repos/greatapp/temp/test_pydantic_xml.py", line 16, in <module>
    data_model = Company.from_xml(xml_string)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Repos/greatapp/venv/lib/python3.11/site-packages/pydantic_xml/model.py", line 241, in from_xml
    return cls.from_xml_tree(etree.fromstring(source))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Repos/greatapp/venv/lib/python3.11/site-packages/pydantic_xml/model.py", line 228, in from_xml_tree
    return cls.parse_obj(obj)
           ^^^^^^^^^^^^^^^^^^
  File "pydantic/main.py", line 527, in pydantic.main.BaseModel.parse_obj
  File "pydantic/main.py", line 342, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Company
description
  value is not a valid integer (type=type_error.integer)

pydantic-xml 0.6.0 Output:

Traceback (most recent call last):
  File "/Users/user/Repos/greatapp/temp/test_pydantic_xml.py", line 16, in <module>
    data_model = Company.from_xml(xml_string)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Repos/greatapp/venv/lib/python3.11/site-packages/pydantic_xml/model.py", line 286, in from_xml
    return cls.from_xml_tree(etree.fromstring(source))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Repos/greatapp/venv/lib/python3.11/site-packages/pydantic_xml/model.py", line 270, in from_xml_tree
    obj = cls.__xml_serializer__.deserialize(XmlElement.from_native(root))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Repos/greatapp/venv/lib/python3.11/site-packages/pydantic_xml/serializers/factories/model.py", line 86, in deserialize
    return self._model.parse_obj(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pydantic/main.py", line 527, in pydantic.main.BaseModel.parse_obj
  File "pydantic/main.py", line 342, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Company
description
  value is not a valid integer (type=type_error.integer)