8000 Give better error message attempting to compute the json schema of a model with undefined fields by dmontagu · Pull Request #6519 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@dmontagu
Copy link
Contributor
@dmontagu dmontagu commented Jul 7, 2023

Right now, if you run the following:

from pydantic import BaseModel

class Foo(BaseModel):
    x: 'Bar'

Foo.model_json_schema()

it produces the error:

Traceback (most recent call last):
  File "/Users/davidmontague/Library/Application Support/JetBrains/PyCharm2023.1/scratches/scratch_638.py", line 6, in <module>
    Foo.model_json_schema()
  File "/Users/davidmontague/Programming/pydantic/pydantic/pydantic/main.py", line 372, in model_json_schema
    return model_json_schema(
  File "/Users/davidmontague/Programming/pydantic/pydantic/pydantic/json_schema.py", line 2033, in model_json_schema
    return schema_generator_instance.generate(cls.__pydantic_core_schema__, mode=mode)
  File "/Users/davidmontague/Programming/pydantic/pydantic/pydantic/_internal/_model_construction.py", line 193, in __getattr__
    raise AttributeError(item)
AttributeError: __pydantic_core_schema__. Did you mean: '__get_pydantic_core_schema__'?

With this change, it produces:

pydantic.errors.PydanticUserError: `Foo` is not fully defined; you should define `Bar`, then call `Foo.model_rebuild()`.

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

skip change file check

@cloudflare-workers-and-pages
Copy link

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: cd76548
Status: ✅  Deploy successful!
Preview URL: https://06e91b26.pydantic-docs2.pages.dev
Branch Preview URL: https://better-json-schema-failure-e.pydantic-docs2.pages.dev

View logs

@dmontagu dmontagu enabled auto-merge (squash) July 7, 2023 19:30
@dmontagu dmontagu merged commit 12fde9c into main Jul 7, 2023
@dmontagu dmontagu deleted the better-json-schema-failure-error branch July 7, 2023 19:34
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

Successfully merging this pull request may close these issues.

3 participants

0