Skip to content

docs: arbitrary_types_allowed needed in Pydantic example #1624

@dimaqq

Description

@dimaqq

Please confirm the following

  • I understand this is open source software provided for free and that I might not receive a timely response.
  • I am positive I am NOT reporting a (potential) security
    vulnerability, to the best of my knowledge. (These must be shared by
    submitting this report form instead, if
    any hesitation exists.)
  • I am willing to submit a pull request with reporoducers as xfailing test cases or even entire fix. (Assign this issue to me.)

Describe the bug

In https://yarl.aio-libs.org/en/latest/#why-isn-t-boolean-supported-by-the-url-query-api it is stated that:

The URL could be used as a field type in pydantic models seamlessly:

from pydantic import BaseModel
from yarl import URL

class Model(BaseModel):
    url: URL

However, the example does not work out of the box:

PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'yarl.URL'>. 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.12/u/schema-for-unknown-type

The working example would be:

In [20]: class C(pydantic.BaseModel):
    ...:     model_config = pydantic.ConfigDict(arbitrary_types_allowed=True)
    ...:     endpoint: yarl.URL

Versions tested:

In [23]: pydantic.__version__
Out[23]: '2.12.5'

In [24]: yarl.__version__
Out[24]: '1.22.0'

In [26]: sys.version
Out[26]: '3.10.11 (v3.10.11:7d4cc5aa85, Apr  4 2023, 19:05:19) [Clang 13.0.0 (clang-1300.0.29.30)]'

To Reproduce

...

Expected behavior

...

Logs/tracebacks

...

Python Version

In [26]: sys.version
Out[26]: '3.10.11 (v3.10.11:7d4cc5aa85, Apr  4 2023, 19:05:19) [Clang 13.0.0 (clang-1300.0.29.30)]'

multidict Version

In [28]: multidict.__version__
Out[28]: '6.7.1'

propcache Version

In [31]: propcache.__version__
Out[31]: '0.4.1'

yarl Version

In [24]: yarl.__version__
Out[24]: '1.22.0'

OS

macOS

Additional context

Pls consider simplifying the bug report form 🙏🏻

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions