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

Add information about class in error message of schema generation #8917

Merged
merged 5 commits into from Feb 29, 2024
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
2 changes: 1 addition & 1 deletion pydantic/_internal/_generate_schema.py
Expand Up @@ -2160,7 +2160,7 @@ def _extract_get_pydantic_json_schema(tp: Any, schema: CoreSchema) -> GetJsonSch
if not has_custom_v2_modify_js_func:
raise PydanticUserError(
'The `__modify_schema__` method is not supported in Pydantic v2. '
'Use `__get_pydantic_json_schema__` instead.',
'Use `__get_pydantic_json_schema__` instead for class' + str(tp),
code='custom-json-schema',
)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_internal.py
Expand Up @@ -206,3 +206,19 @@ def test_schema_is_valid():
collect_invalid_schemas(cs.nullable_schema(cs.int_schema(metadata={HAS_INVALID_SCHEMAS_METADATA_KEY: True})))
is True
)


def test_model_name_in_schema_error_message():
from pydantic import BaseModel, ConfigDict, PydanticUserError

class SomeLongName:
@classmethod
def __modify_schema__(cls, field_schema):
field_schema['title'] = 'SomeLongName'

with pytest.raises(PydanticUserError, match='The `__modify_schema__`.*SomeLongName.*'):

class B(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)

a: SomeLongName