From 14e0b7d62da05557eb8a01184f6977ecfa9c6fdc Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Thu, 29 Feb 2024 16:09:44 +0100 Subject: [PATCH] Add information about class in error message of schema generation (#8917) Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com> --- pydantic/_internal/_generate_schema.py | 2 +- tests/test_internal.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pydantic/_internal/_generate_schema.py b/pydantic/_internal/_generate_schema.py index e9fd34abcb..c07e532cd7 100644 --- a/pydantic/_internal/_generate_schema.py +++ b/pydantic/_internal/_generate_schema.py @@ -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', ) diff --git a/tests/test_internal.py b/tests/test_internal.py index 5b2705dbf8..ad4bf03102 100644 --- a/tests/test_internal.py +++ b/tests/test_internal.py @@ -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