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 · 7 comments
Open
1 task done

KeyError: '__main__.Model1:95063711754448' #9319

jherbel opened this issue Apr 25, 2024 · 7 comments
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
CheckmkCI pushed a commit to Checkmk/checkmk that referenced this issue May 13, 2024
The Pydantic version we used before has an issue in the mypy plugin
which can lead to actual typing errors slipping through, see
pydantic/pydantic#9008.

Note that the version we udpate to has this issue:
pydantic/pydantic#9319
However, since a workaround is available, we still udpate to avoid
introducing new typing errors.

Also fix newly found issues. Apparently, polyfactory now has issues with
handling field aliases, so we switch to validation aliases, which are
sufficient in this case.

CMK-17006

Change-Id: I8b8ebb072f0c68b799d28c132c5f5605232db9c7
@cielecki
Copy link

@jherbel We have a similar problem but we are not using Sequence, I'm wondering if you have any hints on what is the issue here and how to develop a workaround for specific code base?

@jherbel
Copy link
Author

jherbel commented May 14, 2024

Hi @cielecki, by chance, I discovered yesterday that list instead of Sequence works in the scenario above. Maybe this helps, though you wrote that you are not using Sequence. Unfortunately, I cannot give any tips regarding a general workaround.

@cielecki
Copy link

@jherbel thanks

I decided to go with just going with "Any" in few places in order to make it work in the short term ...

@sydney-runkle
Copy link
Member

Hi folks, when did this issue crop up for you?

@sydney-runkle
Copy link
Member

Looks like this was introduced in the 2.6.0 release - moving to the 2.8 milestone - still a priority, but going to focus on the 2.7 patch this week. Will send updates soon!

@sydney-runkle sydney-runkle modified the milestones: 2.7 fixes, v2.8.0 May 20, 2024
@jherbel
Copy link
Author

jherbel commented May 21, 2024

I cannot really say, sry. We are currently 2.5.2, which is not affected by this.

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

3 participants