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

Incompatible with pydantic v2 #2

Closed
Niivii opened this issue Jul 10, 2023 · 5 comments
Closed

Incompatible with pydantic v2 #2

Niivii opened this issue Jul 10, 2023 · 5 comments

Comments

@Niivii
Copy link

Niivii commented Jul 10, 2023

pydantic.error_wrappers:ErrorWrapper has been removed in V2

@romis2012
Copy link
Owner

Yes, pydantic-collections is compatible with pydantic>=1.8.2,<2.0. We just forgot to pin the pydantic version (

@romis2012
Copy link
Owner

Pydantic v2 support added in version 0.5.0

@matthewelwell
Copy link

Hi @romis2012, it seems like there are compatibility issues with pydantic==2.2.1? Perhaps this is a pydantic issue rather than a pydantic-collection issue so please forgive me if so but, given the relevance of this issue, I figured I would raise it here first.

The following code works with pydantic==2.1.1 but not 2.2.1.

from pydantic import BaseModel
from pydantic_collections import BaseCollectionModel


class MyModel(BaseModel):
    foo: str


class MyModelList(BaseCollectionModel[MyModel]):
    pass


if __name__ == "__main__":
    assert MyModelList([{"foo": "bar"}, {"foo": "baz"}])

When running this code in an environment using pydantic==2.2.1 I get the following exception:

Traceback (most recent call last):
  File "/Users/matthewelwell/projects/sandbox/pydantic2collections/models.py", line 2, in <module>
    from pydantic_collections import BaseCollectionModel
  File "/Users/matthewelwell/projects/sandbox/pydantic2collections/.venv/lib/python3.11/site-packages/pydantic_collections/__init__.py", line 10, in <module>
    from ._v2 import BaseCollectionModel, CollectionModelConfig
  File "/Users/matthewelwell/projects/sandbox/pydantic2collections/.venv/lib/python3.11/site-packages/pydantic_collections/_v2.py", line 86, in <module>
    class BaseCollectionModel(
  File "/Users/matthewelwell/projects/sandbox/pydantic2collections/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 123, in __new__
    cls: type[BaseModel] = super().__new__(mcs, cls_name, bases, namespace, **kwargs)  # type: ignore
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen abc>", line 106, in __new__
  File "/Users/matthewelwell/projects/sandbox/pydantic2collections/.venv/lib/python3.11/site-packages/pydantic/root_model.py", line 50, in __init_subclass__
    raise PydanticUserError(
pydantic.errors.PydanticUserError: `RootModel` does not support setting `model_config['extra']`

Note that I found this error while trying to reproduce another error I have found when using pydantic collections with v2. I was trying to create a minimal environment to reproduce it and instead found this. I will let you know if/when I can reproduce the other error I am having.

@matthewelwell
Copy link

It seems like it was broken here but from the comment in that PR, I have a feeling that the setting of extra='forbid' in the BaseCollectionModel.model_config attribute never worked anyway?

Because `RootModel` is not capable of storing or even accepting extra fields during initialization, we raise an error
if you try to specify a value for the config setting `'extra'` when creating a subclass of `RootModel`

@romis2012
Copy link
Owner

romis2012 commented Aug 22, 2023 via email

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

3 participants