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

Exclude BaseModel docstring from JSON schema description #8352

Merged
merged 5 commits into from Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion pydantic/_internal/_generate_schema.py
Expand Up @@ -212,6 +212,8 @@ def modify_model_json_schema(
Returns:
JsonSchemaValue: The updated JSON schema.
"""
from ..main import BaseModel

json_schema = handler(schema_or_field)
original_schema = handler.resolve_ref_schema(json_schema)
# Preserve the fact that definitions schemas should never have sibling keys:
Expand All @@ -221,7 +223,8 @@ def modify_model_json_schema(
original_schema['allOf'] = [{'$ref': ref}]
if 'title' not in original_schema:
original_schema['title'] = cls.__name__
docstring = cls.__doc__
# BaseModel; don't use cls.__doc__ as it will contain the verbose class signature by default
docstring = None if cls is BaseModel else cls.__doc__
if docstring and 'description' not in original_schema:
original_schema['description'] = inspect.cleandoc(docstring)
return json_schema
Expand Down
7 changes: 7 additions & 0 deletions tests/test_json_schema.py
Expand Up @@ -5812,3 +5812,10 @@ class OuterModel(pydantic.BaseModel):

with pytest.raises(ValidationError):
OuterModel(x=2, y=-1, z=-1)


def test_description_not_included_for_basemodel() -> None:
class Model(BaseModel):
x: BaseModel

assert 'description' not in Model.model_json_schema()['$defs']['BaseModel']