Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal error when using pydantic.PlainValidator #229

Open
eltoder opened this issue Nov 22, 2024 · 0 comments
Open

Internal error when using pydantic.PlainValidator #229

eltoder opened this issue Nov 22, 2024 · 0 comments

Comments

@eltoder
Copy link
Contributor

eltoder commented Nov 22, 2024

pydantic_xml seems to have an issue with PlainValidator:

import datetime as dt
import pydantic
import pydantic_xml as pxml
from typing import Annotated

Date = Annotated[
    dt.date,
    pydantic.PlainValidator(
        lambda v: dt.datetime.strptime(v, "%Y%m%d").date() if isinstance(v, str) else v
    ),
]

class Model(pxml.BaseXmlModel):
    date: Date = pxml.element()

This generates an error at import time when building the model

Traceback (most recent call last):
  File "/home/eltoder/dev/scratch/xml_plain_validator.py", line 13, in <module>
    class Model(pxml.BaseXmlModel):
  File "/home/eltoder/dev/scratch/.venv/lib/python3.10/site-packages/pydantic_xml/model.py", line 375, in __new__
    cls.__build_serializer__()
  File "/home/eltoder/dev/scratch/.venv/lib/python3.10/site-packages/pydantic_xml/model.py", line 461, in __build_serializer__
    serializer = Serializer.parse_core_schema(
  File "/home/eltoder/dev/scratch/.venv/lib/python3.10/site-packages/pydantic_xml/serializers/serializer.py", line 184, in parse_core_schema
    return cls.select_serializer(schema, ctx)
  File "/home/eltoder/dev/scratch/.venv/lib/python3.10/site-packages/pydantic_xml/serializers/serializer.py", line 241, in select_serializer
    return factories.model.from_core_schema(schema, ctx)
  File "/home/eltoder/dev/scratch/.venv/lib/python3.10/site-packages/pydantic_xml/serializers/factories/model.py", line 453, in from_core_schema
    return ModelSerializer.from_core_schema(schema, ctx)
  File "/home/eltoder/dev/scratch/.venv/lib/python3.10/site-packages/pydantic_xml/serializers/factories/model.py", line 91, in from_core_schema
    fields_serializers[field_name] = Serializer.parse_core_schema(model_field['schema'], field_ctx)
  File "/home/eltoder/dev/scratch/.venv/lib/python3.10/site-packages/pydantic_xml/serializers/serializer.py", line 183, in parse_core_schema
    schema, ctx = cls.preprocess_schema(schema, ctx)
  File "/home/eltoder/dev/scratch/.venv/lib/python3.10/site-packages/pydantic_xml/serializers/serializer.py", line 198, in preprocess_schema
    inner_schema = schema['schema']
KeyError: 'schema'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant