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

KeyError: '__main__.Model1:95063711754448' #9319

Open
1 task done
jherbel opened this issue Apr 25, 2024 · 1 comment
Open
1 task done

KeyError: '__main__.Model1:95063711754448' #9319

jherbel opened this issue Apr 25, 2024 · 1 comment
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Milestone

Comments

@jherbel
Copy link

jherbel commented Apr 25, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

The code below crashes with the following traceback. See also #8897, which was closed.

Traceback (most recent call last):
  File "/home/joerg/tmp/pydantic_key_error.py", line 22, in <module>
    class Model4(BaseModel, frozen=True):
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 202, in __new__
    complete_model_class(
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 549, in complete_model_class
    schema = gen_schema.clean_schema(schema)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 435, in clean_schema
    schema = simplify_schema_references(schema)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 463, in simplify_schema_references
    schema = walk_core_schema(schema, count_refs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 414, in walk_core_schema
    return f(schema.copy(), _dispatch)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 459, in count_refs
    recurse(definitions[ref], count_refs)
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 197, in walk
    return f(schema, self._walk)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 447, in count_refs
    return recurse(s, count_refs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 200, in _walk
    schema = self._schema_type_to_method[schema['type']](schema.copy(), f)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 209, in _handle_other_schemas
    schema['schema'] = self.walk(sub_schema, f)  # type: ignore
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 197, in walk
    return f(schema, self._walk)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 447, in count_refs
    return recurse(s, count_refs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 200, in _walk
    schema = self._schema_type_to_method[schema['type']](schema.copy(), f)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 337, in handle_model_fields_schema
    replaced_field['schema'] = self.walk(v['schema'], f)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 197, in walk
    return f(schema, self._walk)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 459, in count_refs
    recurse(definitions[ref], count_refs)
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 200, in _walk
    schema = self._schema_type_to_method[schema['type']](schema.copy(), f)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 209, in _handle_other_schemas
    schema['schema'] = self.walk(sub_schema, f)  # type: ignore
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 197, in walk
    return f(schema, self._walk)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 447, in count_refs
    return recurse(s, count_refs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 200, in _walk
    schema = self._schema_type_to_method[schema['type']](schema.copy(), f)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 337, in handle_model_fields_schema
    replaced_field['schema'] = self.walk(v['schema'], f)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 197, in walk
    return f(schema, self._walk)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 447, in count_refs
    return recurse(s, count_refs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 203, in _walk
    schema['serialization'] = self._handle_ser_schemas(ser_schema, f)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 215, in _handle_ser_schemas
    ser_schema['schema'] = self.walk(schema, f)  # type: ignore
                           ^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 197, in walk
    return f(schema, self._walk)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 459, in count_refs
    recurse(definitions[ref], count_refs)
            ~~~~~~~~~~~^^^^^
KeyError: '__main__.Model1:110296424761552'

Example Code

from collections.abc import Sequence
from pydantic import PlainValidator, BaseModel
from typing import Annotated


class Model1(BaseModel, frozen=True):
    ...


def parse_model1(x: object) -> Model1:
    return Model1()


class Model2(BaseModel, frozen=True):
    model1:  Annotated[Model1, PlainValidator(parse_model1)]


class Model3(BaseModel, frozen=True):
    model2: Sequence[Model2]


class Model4(BaseModel, frozen=True):
    model2: Model2

Python, Pydantic & OS Version

pydantic version: 2.7.0
pydantic-core version: 2.18.1
pydantic-core build: profile=release pgo=true
install path: /home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic
python version: 3.12.1 (main, Jan  9 2024, 15:28:21) [GCC 11.4.0]
platform: Linux-6.5.0-28-generic-x86_64-with-glibc2.35
related packages: typing_extensions-4.9.0
commit: unknown
@jherbel jherbel added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 25, 2024
@sydney-runkle
Copy link
Member

Hmph, definitely a bug. Thanks for the report. Will attempt to fix for 2.7.2

@sydney-runkle sydney-runkle added this to the 2.7 fixes milestone Apr 25, 2024
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 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

2 participants