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

LookupError on JSON schema generation for annotated self-referencing TypedDict #8704

Open
1 task done
cliff-lau-cloverhealth opened this issue Feb 2, 2024 · 1 comment
Labels
bug V2 Bug related to Pydantic V2

Comments

@cliff-lau-cloverhealth
Copy link

cliff-lau-cloverhealth commented Feb 2, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Hey there!

I have a self-referencing TypedDict that I would like to annotate with Field, so that only one of the keys are required.

The model works well, but it throws an exception while generating a JSON schema for the PetMapping.pets property:

File "<hidden>/site-packages/pydantic/_internal/_schema_generation_shared.py", line 57, in resolve_ref_schema
    raise LookupError(
LookupError: Could not find a ref for #/$defs/__main____PetMapping-Input__1. Maybe you tried to call resolve_ref_schema from within a recursive model?

This model looks weird, but unfortunately I have to keep this structure for backward compatibility.

This problem does not exist on or before 2.3.0.

Example Code

from pydantic import BaseModel
from pydantic import Field
from typing_extensions import Annotated
from typing_extensions import TypedDict


class PetMapping(TypedDict, total=False):
    cat: str

    dog: str

    pets: list[Annotated["PetMapping", Field(min_length=1, max_length=1)]]  # this went wrong


class Foo(BaseModel):
    pet: PetMapping


_ = Foo.model_json_schema()

Python, Pydantic & OS Version

❯ python -c "import pydantic.version; print(pydantic.version.version_info())"
             pydantic version: 2.6.0
        pydantic-core version: 2.16.1
          pydantic-core build: profile=release pgo=false
                 install path: <hidden>
               python version: 3.10.13 (main, Sep 29 2023, 12:40:37) [Clang 14.0.0 (clang-1400.0.29.202)]
                     platform: macOS-14.3-x86_64-i386-64bit
             related packages: typing_extensions-4.6.3 email-validator-2.1.0.post1
                       commit: unknown
@cliff-lau-cloverhealth cliff-lau-cloverhealth added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 2, 2024
@cliff-lau-cloverhealth
Copy link
Author

#7645 #8689 might also be related to this, I guess.

@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label Feb 13, 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
Projects
None yet
Development

No branches or pull requests

2 participants