Skip to content

Commit

Permalink
Add information about class in error message of schema generation (#8917
Browse files Browse the repository at this point in the history
)

Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
  • Loading branch information
Czaki and sydney-runkle committed Feb 29, 2024
1 parent bbf0724 commit 14e0b7d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit 14e0b7d

Please sign in to comment.