@@ -145,10 +145,22 @@ def f(input_value, info):
145145@pytest .mark .parametrize (
146146 'config,kwargs,expected_repr' ,
147147 [
148- (None , {}, 'ValidationInfo(config=None, context=None, data=None, field_name=None)' ),
149- (None , {'context' : {1 : 2 }}, 'ValidationInfo(config=None, context={1: 2}, data=None, field_name=None)' ),
150- (None , {'context' : None }, 'ValidationInfo(config=None, context=None, data=None, field_name=None)' ),
151- ({'title' : 'hello' }, {}, "ValidationInfo(config={'title': 'hello'}, context=None, data=None, field_name=None)" ),
148+ (None , {}, 'ValidationInfo(config=None, context=None, data=None, field_name=None, model_type=None)' ),
149+ (
150+ None ,
151+ {'context' : {1 : 2 }},
152+ 'ValidationInfo(config=None, context={1: 2}, data=None, field_name=None, model_type=None)' ,
153+ ),
154+ (
155+ None ,
156+ {'context' : None },
157+ 'ValidationInfo(config=None, context=None, data=None, field_name=None, model_type=None)' ,
158+ ),
159+ (
160+ {'title' : 'hello' },
161+ {},
162+ "ValidationInfo(config={'title': 'hello'}, context=None, data=None, field_name=None, model_type=None)" ,
163+ ),
152164 ],
153165)
154166def test_val_info_repr (config , kwargs , expected_repr ):
@@ -591,8 +603,14 @@ class Model:
591603 def f (input_value : Any , info : core_schema .ValidationInfo ) -> Any :
592604 assert info .field_name == 'x'
593605 assert info .data == {}
594- assert repr (info ) == "ValidationInfo(config=None, context=None, data={}, field_name='x')"
595- assert str (info ) == "ValidationInfo(config=None, context=None, data={}, field_name='x')"
606+ assert (
607+ repr (info )
608+ == "ValidationInfo(config=None, context=None, data={}, field_name='x', model_type=<class 'tests.validators.test_function.test_model_field_before_validator.<locals>.Model'>)"
609+ )
610+ assert (
611+ str (info )
612+ == "ValidationInfo(config=None, context=None, data={}, field_name='x', model_type=<class 'tests.validators.test_function.test_model_field_before_validator.<locals>.Model'>)"
613+ )
596614 assert isinstance (input_value , bytes )
597615 return f'input: { input_value .decode ()} '
598616
@@ -602,7 +620,9 @@ def f(input_value: Any, info: core_schema.ValidationInfo) -> Any:
602620 core_schema .model_fields_schema (
603621 {
604622 'x' : core_schema .model_field (
605- core_schema .with_info_before_validator_function (f , core_schema .str_schema (), field_name = 'x' )
623+ core_schema .with_info_before_validator_function (
624+ f , core_schema .str_schema (), field_name = 'x' , model_type = Model
625+ )
606626 )
607627 }
608628 ),
@@ -628,7 +648,9 @@ def f(input_value: str, info: core_schema.ValidationInfo) -> Any:
628648 core_schema .model_fields_schema (
629649 {
630650 'x' : core_schema .model_field (
631- core_schema .with_info_after_validator_function (f , core_schema .str_schema (), field_name = 'x' )
651+ core_schema .with_info_after_validator_function (
652+ f , core_schema .str_schema (), field_name = 'x' , model_type = Model
653+ )
632654 )
633655 }
634656 ),
@@ -652,7 +674,11 @@ def f(input_value: Any, info: core_schema.ValidationInfo) -> Any:
652674 core_schema .model_schema (
653675 Model ,
654676 core_schema .model_fields_schema (
655- {'x' : core_schema .model_field (core_schema .with_info_plain_validator_function (f , field_name = 'x' ))}
677+ {
678+ 'x' : core_schema .model_field (
679+ core_schema .with_info_plain_validator_function (f , field_name = 'x' , model_type = Model )
680+ )
681+ }
656682 ),
657683 )
658684 )
@@ -676,7 +702,9 @@ def f(input_value: Any, val: core_schema.ValidatorFunctionWrapHandler, info: cor
676702 core_schema .model_fields_schema (
677703 {
678704 'x' : core_schema .model_field (
679- core_schema .with_info_wrap_validator_function (f , core_schema .str_schema (), field_name = 'x' )
705+ core_schema .with_info_wrap_validator_function (
706+ f , core_schema .str_schema (), field_name = 'x' , model_type = Model
707+ )
680708 )
681709 }
682710 ),
@@ -921,25 +949,27 @@ def sample_repr(v: Any, info: core_schema.ValidationInfo) -> Any:
921949 reprs .append (repr (info ))
922950 return v
923951
952+ class Foo :
953+ def __repr__ (self ) -> str :
954+ return 'This is Foo!'
955+
924956 v = SchemaValidator (
925957 core_schema .chain_schema (
926958 [
927959 core_schema .with_info_plain_validator_function (sample_repr ),
928960 core_schema .with_info_plain_validator_function (sample_repr , field_name = 'x' ),
961+ core_schema .with_info_plain_validator_function (sample_repr , field_name = 'x' , model_type = Foo ),
929962 ]
930963 )
931964 )
932965
933- class Foo :
934- def __repr__ (self ) -> str :
935- return 'This is Foo!'
936-
937966 v .validate_python (Foo ())
938967
939968 # insert_assert(reprs)
940969 assert reprs == [
941- 'ValidationInfo(config=None, context=None, data=None, field_name=None)' ,
942- "ValidationInfo(config=None, context=None, data=None, field_name='x')" ,
970+ 'ValidationInfo(config=None, context=None, data=None, field_name=None, model_type=None)' ,
971+ "ValidationInfo(config=None, context=None, data=None, field_name='x', model_type=None)" ,
972+ "ValidationInfo(config=None, context=None, data=None, field_name='x', model_type=<class 'tests.validators.test_function.test_reprs.<locals>.Foo'>)" ,
943973 ]
944974
945975
0 commit comments