Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit188dc9a

Browse files
ygsh0816Viicos
andauthored
Suppress core schema generation warning when usingSkipValidation (#12002)
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
1 parent0c4a22b commit188dc9a

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

‎pydantic/_internal/_generate_schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
from ..functional_validatorsimportAfterValidator,BeforeValidator,FieldValidatorModes,PlainValidator,WrapValidator
6363
from ..json_schemaimportJsonSchemaValue
6464
from ..versionimportversion_short
65-
from ..warningsimportPydanticDeprecatedSince20
65+
from ..warningsimportPydanticArbitraryTypeWarning,PydanticDeprecatedSince20
6666
from .import_decorators,_discriminated_union,_known_annotated_metadata,_repr,_typing_extra
6767
from ._configimportConfigWrapper,ConfigWrapperStack
6868
from ._core_metadataimportCoreMetadata,update_core_metadata
@@ -619,7 +619,7 @@ def _arbitrary_type_schema(self, tp: Any) -> CoreSchema:
619619
' Pydantic will allow any object with no validation since we cannot even'
620620
' enforce that the input is an instance of the given type.'
621621
' To get rid of this error wrap the type with `pydantic.SkipValidation`.',
622-
UserWarning,
622+
PydanticArbitraryTypeWarning,
623623
)
624624
returncore_schema.any_schema()
625625
returncore_schema.is_instance_schema(tp)

‎pydantic/functional_validators.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
importdataclasses
66
importsys
7+
importwarnings
78
fromfunctoolsimportpartialmethod
89
fromtypesimportFunctionType
910
fromtypingimportTYPE_CHECKING,Annotated,Any,Callable,Literal,TypeVar,Union,cast,overload
@@ -14,6 +15,7 @@
1415
from ._internalimport_decorators,_generics,_internal_dataclass
1516
from .annotated_handlersimportGetCoreSchemaHandler
1617
from .errorsimportPydanticUserError
18+
from .warningsimportPydanticArbitraryTypeWarning
1719

1820
ifsys.version_info< (3,11):
1921
fromtyping_extensionsimportProtocol
@@ -817,7 +819,9 @@ def __class_getitem__(cls, item: Any) -> Any:
817819

818820
@classmethod
819821
def__get_pydantic_core_schema__(cls,source:Any,handler:GetCoreSchemaHandler)->core_schema.CoreSchema:
820-
original_schema=handler(source)
822+
withwarnings.catch_warnings():
823+
warnings.simplefilter('ignore',PydanticArbitraryTypeWarning)
824+
original_schema=handler(source)
821825
metadata= {'pydantic_js_annotation_functions': [lambda_c,h:h(original_schema)]}
822826
returncore_schema.any_schema(
823827
metadata=metadata,

‎pydantic/warnings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,7 @@ class PydanticExperimentalWarning(Warning):
9494
This warning is raised when using experimental functionality in Pydantic.
9595
It is raised to warn users that the functionality may change or be removed in future versions of Pydantic.
9696
"""
97+
98+
99+
classPydanticArbitraryTypeWarning(UserWarning):
100+
"""Warning raised when Pydantic fails to generate a core schema for an arbitrary type."""

‎tests/test_types.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
importsys
1010
importtyping
1111
importuuid
12+
importwarnings
1213
fromcollectionsimportCounter,OrderedDict,UserDict,defaultdict,deque
1314
fromcollections.abcimportIterable,Mapping,MutableMapping,Sequence
1415
fromdataclassesimportdataclass
@@ -6016,6 +6017,21 @@ class A(BaseModel):
60166017
}
60176018

60186019

6020+
@pytest.mark.skipif(sys.version_info< (3,12),reason="`Annotated` doesn't allow instances in <3.12")
6021+
deftest_skip_validation_arbitrary_type_object()->None:
6022+
"""https://github.com/pydantic/pydantic/issues/11997.
6023+
6024+
Using an arbitrary object (and not a type) normally raises a warning,
6025+
which should be suppressed when using `SkipValidation`.
6026+
"""
6027+
6028+
withwarnings.catch_warnings():
6029+
warnings.simplefilter('error')
6030+
6031+
classModel(BaseModel,arbitrary_types_allowed=True):
6032+
field:Annotated[object(),SkipValidation]
6033+
6034+
60196035
deftest_transform_schema():
60206036
ValidateStrAsInt=Annotated[str,GetPydanticSchema(lambda_s,h:core_schema.int_schema())]
60216037

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp