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

BaseModel with defer_build breaks with TypeAdapter. #7727

Closed
1 task done
zakstucke opened this issue Oct 3, 2023 · 2 comments · Fixed by #7736
Closed
1 task done

BaseModel with defer_build breaks with TypeAdapter. #7727

zakstucke opened this issue Oct 3, 2023 · 2 comments · Fixed by #7736
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@zakstucke
Copy link
Contributor

zakstucke commented Oct 3, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Repeated uses of a TypeAdapter with a model breaks when using defer_build.

Traceback (most recent call last):
  File "/ptest.py", line 40, in <module>
    ta.validate_python({"foo": "str"})
  File "/venv/lib/python3.11/site-packages/pydantic/type_adapter.py", line 283, in validate_python
    return self.validator.validate_python(__object, strict=strict, from_attributes=from_attributes, context=context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.11/site-packages/pydantic/_internal/_mock_val_ser.py", line 47, in __getattr__
    raise PydanticUserError(self._error_message, code=self._code)
pydantic.errors.PydanticUserError: `Foo` is not fully defined; you should define all referenced types, then call `Foo.model_rebuild()`.

For further information visit **https://errors.pydantic.dev/2.4/u/class-not-fully-defined**

Example Code

from pydantic import BaseModel, TypeAdapter

class Foo(BaseModel):
    foo: str
    model_config = {
        "defer_build": True,
    }


ta = TypeAdapter(Foo)
ta.validate_python({"foo": "str"})
ta.validate_python({"foo": "str"}) # <--- Second call breaks

Python, Pydantic & OS Version

pydantic version: 2.4.2
        pydantic-core version: 2.10.1
          pydantic-core build: profile=release pgo=true
                 install path: /venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.5 (main, Aug 25 2023, 13:19:50) [GCC 11.4.0]
                     platform: Linux-6.2.0-33-generic-x86_64-with-glibc2.35
             related packages: email-validator-2.0.0 mypy-1.5.1 fastapi-0.103.2 typing_extensions-4.8.0
@zakstucke zakstucke added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Oct 3, 2023
@zakstucke zakstucke changed the title BaseModel with "defer_build": True breaks with TypeAdapter. BaseModel with defer_build breaks with TypeAdapter. Oct 3, 2023
@Kludex Kludex removed the pending Awaiting a response / confirmation label Oct 3, 2023
@Kludex
Copy link
Member

Kludex commented Oct 3, 2023

Thanks for the report. Yeah, it does look like a bug.

@sydney-runkle
Copy link
Member

@zakstucke,

Thanks for the report! We've pushed a fix that'll become active with the next release 😄.

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

Successfully merging a pull request may close this issue.

3 participants