From da26cd71f447922d697e5effc2c4df262459993d Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Thu, 29 Feb 2024 15:12:32 +0100 Subject: [PATCH 1/5] Add information about class in error message of schema --- pydantic/_internal/_generate_schema.py | 2 +- 1 file changed, 1 insertion(+), 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', ) From 38429ff2c0f693279ffbaf874b56587297914b6e Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Thu, 29 Feb 2024 15:23:19 +0100 Subject: [PATCH 2/5] add test --- tests/test_internal.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_internal.py b/tests/test_internal.py index 5b2705dbf8..7ebc338d49 100644 --- a/tests/test_internal.py +++ b/tests/test_internal.py @@ -206,3 +206,14 @@ 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_error_message_schema() + class 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) + a: SomeLongName \ No newline at end of file From 08bd2bbe1c3cc599cc250e9a27f5ccc732bc62e7 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Thu, 29 Feb 2024 15:31:33 +0100 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com> --- tests/test_internal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_internal.py b/tests/test_internal.py index 7ebc338d49..56efb73390 100644 --- a/tests/test_internal.py +++ b/tests/test_internal.py @@ -208,8 +208,8 @@ def test_schema_is_valid(): ) -def test_error_message_schema() - class Class SomeLongName: +def test_model_name_in_schema_error_message() + class SomeLongName: @classmethod def __modify_schema__(cls, field_schema): field_schema["title"] = "SomeLongName" From a8cff2114ecaca956c59c8ddb0780e2d2435535f Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Thu, 29 Feb 2024 14:51:41 +0000 Subject: [PATCH 4/5] fix lints --- tests/test_internal.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/test_internal.py b/tests/test_internal.py index 56efb73390..4dc68a6246 100644 --- a/tests/test_internal.py +++ b/tests/test_internal.py @@ -208,12 +208,15 @@ def test_schema_is_valid(): ) -def test_model_name_in_schema_error_message() +def test_model_name_in_schema_error_message(): + from pydantic import BaseModel, 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) - a: SomeLongName \ No newline at end of file + field_schema['title'] = 'SomeLongName' + + with pytest.raises(PydanticUserError, match='The `__modify_schema__`.*SomeLongName.*'): + + class B(BaseModel): + a: SomeLongName From c880b1fdc8b59443b33dc6b342846552c38dc46a Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Thu, 29 Feb 2024 15:01:50 +0000 Subject: [PATCH 5/5] fix test --- tests/test_internal.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_internal.py b/tests/test_internal.py index 4dc68a6246..ad4bf03102 100644 --- a/tests/test_internal.py +++ b/tests/test_internal.py @@ -209,7 +209,7 @@ def test_schema_is_valid(): def test_model_name_in_schema_error_message(): - from pydantic import BaseModel, PydanticUserError + from pydantic import BaseModel, ConfigDict, PydanticUserError class SomeLongName: @classmethod @@ -219,4 +219,6 @@ def __modify_schema__(cls, field_schema): with pytest.raises(PydanticUserError, match='The `__modify_schema__`.*SomeLongName.*'): class B(BaseModel): + model_config = ConfigDict(arbitrary_types_allowed=True) + a: SomeLongName