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

Surprising difference in validation between collections.abc.Sequence and typing.Sequence #8321

Open
1 task done
jenshnielsen opened this issue Dec 7, 2023 · 2 comments
Open
1 task done
Labels
bug V2 Bug related to Pydantic V2

Comments

@jenshnielsen
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I was upgrading some code to drop python 3.9 support and ran into a validation error when replacing typing.Sequence with collections.abc.Sequence in the example below the basemodel fail to validate without arbitrary_types_allowed if collections.abc.Sequence is used without subscript (which normally would be equivalent to collections.abc.Sequence[Any]

PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'collections.abc.Sequence'>. 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.5/u/schema-for-unknown-type

The same issue (with a slightly different error message) happens in v1

Example Code

from collections.abc import Sequence
from typing import Sequence as TSequence
from pydantic import BaseModel

class DoesNotWork(BaseModel):
    bar: Sequence = [1,2,3]

class Works1(BaseModel):
   bar: TSequence = [1,2,3]

class Works2(BaseModel):
   bar: Sequence[Any] = [1,2,3]

Python, Pydantic & OS Version

pydantic version: 2.5.2
        pydantic-core version: 2.14.5
          pydantic-core build: profile=release pgo=true
                 install path: C:\Users\jenielse\Miniconda3\envs\qcodespip311\Lib\site-packages\pydantic
               python version: 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)]
                     platform: Windows-10-10.0.22621-SP0
             related packages: mypy-1.7.1 typing_extensions-4.8.
@jenshnielsen jenshnielsen added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Dec 7, 2023
@Kludex Kludex removed the pending Awaiting a response / confirmation label Dec 28, 2023
@Kludex
Copy link
Member

Kludex commented Dec 28, 2023

Yes, at least the error message should be more precise. But I think collections.abc.Sequence should just work.

@sydney-runkle
Copy link
Member

Indeed, this is not the behavior that one would expect. I recently fixed a bug with respect to Sequence validation.

When this issue is addressed, I think we should revisit the refactor I've suggested in this PR: #8614

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

No branches or pull requests

3 participants