dapper91 / pydantic-xml

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

Decimal fields raise `ModelError` with pydantic version 2.2 #93

Closed PoByBolek closed 1 year ago

PoByBolek commented 1 year ago

Decimal fields break in the current beta release (2.0.0b2) when using pydantic version 2.2 (released on 2023-08-17).

Steps to reproduce:

  1. Install pydantic-xml==2.0.0b2
    user@host:~$ pip install pydantic-xml==2.0.0b2
    Collecting pydantic-xml==2.0.0b2
      Downloading pydantic_xml-2.0.0b2-py3-none-any.whl (29 kB)
    Collecting pydantic>=2.0.0
      Downloading pydantic-2.2.0-py3-none-any.whl (373 kB)
    Collecting pydantic-core==2.6.0
      Downloading pydantic_core-2.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.9 MB)
    Collecting annotated-types>=0.4.0
      Downloading annotated_types-0.5.0-py3-none-any.whl (11 kB)
    Collecting typing-extensions>=4.6.1
      Downloading typing_extensions-4.7.1-py3-none-any.whl (33 kB)
    Installing collected packages: typing-extensions, annotated-types, pydantic-core, pydantic, pydantic-xml
    Successfully installed annotated-types-0.5.0 pydantic-2.2.0 pydantic-core-2.6.0 pydantic-xml-2.0.0b2 typing-extensions-4.7.1
  2. Create a test.py file with these contents:

    from decimal import Decimal
    from pydantic_xml import BaseXmlModel, element
    
    class SomeModel(BaseXmlModel):
        value: Decimal = element('Value')
    
  3. Run / import the test.py file:
    user@host:~$ python test.py
    Traceback (most recent call last):
      File "/home/user/pydantic-xml-bug/test.py", line 5, in <module>
        class SomeModel(BaseXmlModel):
      File "/home/user/pydantic-xml-bug/venv/lib/python3.10/site-packages/pydantic_xml/model.py", line 219, in __new__
        cls.__build_serializer__()
      File "/home/user/pydantic-xml-bug/venv/lib/python3.10/site-packages/pydantic_xml/model.py", line 279, in __build_serializer__
        serializer = Serializer.parse_core_schema(
      File "/home/user/pydantic-xml-bug/venv/lib/python3.10/site-packages/pydantic_xml/serializers/serializer.py", line 163, in parse_core_schema
        return cls.select_serializer(schema, ctx)
      File "/home/user/pydantic-xml-bug/venv/lib/python3.10/site-packages/pydantic_xml/serializers/serializer.py", line 220, in select_serializer
        return factories.model.from_core_schema(schema, ctx)
      File "/home/user/pydantic-xml-bug/venv/lib/python3.10/site-packages/pydantic_xml/serializers/factories/model.py", line 325, in from_core_schema
        return ModelSerializer.from_core_schema(schema, ctx)
      File "/home/user/pydantic-xml-bug/venv/lib/python3.10/site-packages/pydantic_xml/serializers/factories/model.py", line 60, in from_core_schema
        fields_serializers[field_name] = Serializer.parse_core_schema(model_field['schema'], field_ctx)
      File "/home/user/pydantic-xml-bug/venv/lib/python3.10/site-packages/pydantic_xml/serializers/serializer.py", line 162, in parse_core_schema
        schema, ctx = cls.preprocess_schema(schema, ctx)
      File "/home/user/pydantic-xml-bug/venv/lib/python3.10/site-packages/pydantic_xml/serializers/serializer.py", line 169, in preprocess_schema
        raise ModelError(f"type {schema_type} is not supported")
    pydantic_xml.errors.ModelError: type decimal is not supported

This works fine when using pydantic version 2.1.1.

dapper91 commented 1 year ago

@PoByBolek Thanks for the report.

dapper91 commented 1 year ago

fixed in version 2.0.0b3

PoByBolek commented 1 year ago

@dapper91 Awesome, thank you!