pydantic / bump-pydantic

Convert Pydantic from V1 to V2 ♻
MIT License
317 stars 24 forks source link

PydanticSchemaGenerationError using constr #105

Closed Alexndrrr closed 1 year ago

Alexndrrr commented 1 year ago

I'm working to update my code base to work on v2, and I'm having trouble because constr doesn't seem to be working properly.

Example:

from pydantic.v1 import constr
from pydantic import TypeAdapter

TypeAdapter(constr(min_length=1)).validate_python("x")

That last line is throwing an exception:

Traceback (most recent call last):
  File "[...]_venv/lib/python3.10/site-packages/pydantic/type_adapter.py", line 163, in __init__
    core_schema = _getattr_no_parents(type, '__pydantic_core_schema__')
  File "[...]_venv/lib/python3.10/site-packages/pydantic/type_adapter.py", line 97, in _getattr_no_parents
    raise AttributeError(attribute)
AttributeError: __pydantic_core_schema__

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[...]_venv/lib/python3.10/site-packages/pydantic/type_adapter.py", line 165, in __init__
    core_schema = _get_schema(type, config_wrapper, parent_depth=_parent_depth + 1)
  File "[...]_venv/lib/python3.10/site-packages/pydantic/type_adapter.py", line 80, in _get_schema
    schema = gen.generate_schema(type_)
  File "[...]_venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 280, in generate_schema
    return self._generate_schema_for_type(
  File "[...]_venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 301, in _generate_schema_for_type
    schema = self._generate_schema(obj)
  File "[...]_venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 586, in _generate_schema
    return self._arbitrary_type_schema(obj, obj)
  File "[...]_venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 638, in _arbitrary_type_schema
    raise PydanticSchemaGenerationError(
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'pydantic.v1.types.ConstrainedStrValue'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.

If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.

For further information visit https://errors.pydantic.dev/2.0.3/u/schema-for-unknown-type

Using cpython 3.10.11, pydantic 2.0.3, and pydantic_core 2.3.0

Alexndrrr commented 1 year ago

Sorry, I meant to create this under pydantic, not bump-pydantic