88import pytest
99from dirty_equals import IsFloat , IsInt
1010
11- from pydantic_core import SchemaError , SchemaValidator , ValidationError , core_schema , validate_core_schema
11+ from pydantic_core import CoreConfig , SchemaError , SchemaValidator , ValidationError , core_schema , validate_core_schema
1212
1313from ..conftest import plain_repr
1414
@@ -262,16 +262,47 @@ def test_one_choice():
262262 assert v .validate_python ('hello' ) == 'hello'
263263
264264
265- def test_strict_union ():
265+ def test_strict_union_flag () -> None :
266+ v = SchemaValidator (core_schema .union_schema (choices = [core_schema .bool_schema (), core_schema .int_schema ()]))
267+ assert v .validate_python (1 , strict = True ) == 1
268+ assert v .validate_python (123 , strict = True ) == 123
269+
270+ with pytest .raises (ValidationError ) as exc_info :
271+ v .validate_python ('123' , strict = True )
272+
273+ assert exc_info .value .errors (include_url = False ) == [
274+ {'type' : 'bool_type' , 'loc' : ('bool' ,), 'msg' : 'Input should be a valid boolean' , 'input' : '123' },
275+ {'type' : 'int_type' , 'loc' : ('int' ,), 'msg' : 'Input should be a valid integer' , 'input' : '123' },
276+ ]
277+
278+
279+ def test_strict_union_config_level () -> None :
266280 v = SchemaValidator (
267- core_schema .union_schema (strict = True , choices = [core_schema .bool_schema (), core_schema .int_schema ()])
281+ core_schema .union_schema (choices = [core_schema .bool_schema (), core_schema .int_schema ()]),
282+ config = CoreConfig (strict = True ),
268283 )
284+
269285 assert v .validate_python (1 ) == 1
270286 assert v .validate_python (123 ) == 123
271287
272288 with pytest .raises (ValidationError ) as exc_info :
273289 v .validate_python ('123' )
290+ assert exc_info .value .errors (include_url = False ) == [
291+ {'type' : 'bool_type' , 'loc' : ('bool' ,), 'msg' : 'Input should be a valid boolean' , 'input' : '123' },
292+ {'type' : 'int_type' , 'loc' : ('int' ,), 'msg' : 'Input should be a valid integer' , 'input' : '123' },
293+ ]
274294
295+
296+ def test_strict_union_member_level () -> None :
297+ v = SchemaValidator (
298+ core_schema .union_schema (choices = [core_schema .bool_schema (strict = True ), core_schema .int_schema (strict = True )])
299+ )
300+
301+ assert v .validate_python (1 ) == 1
302+ assert v .validate_python (123 ) == 123
303+
304+ with pytest .raises (ValidationError ) as exc_info :
305+ v .validate_python ('123' )
275306 assert exc_info .value .errors (include_url = False ) == [
276307 {'type' : 'bool_type' , 'loc' : ('bool' ,), 'msg' : 'Input should be a valid boolean' , 'input' : '123' },
277308 {'type' : 'int_type' , 'loc' : ('int' ,), 'msg' : 'Input should be a valid integer' , 'input' : '123' },
@@ -469,10 +500,10 @@ def test_left_to_right_union():
469500
470501
471502def test_left_to_right_union_strict ():
472- choices = [core_schema .int_schema (), core_schema .float_schema ()]
503+ choices = [core_schema .int_schema (strict = True ), core_schema .float_schema (strict = True )]
473504
474505 # left_to_right union will select not cast if int first (strict int will not accept float)
475- v = SchemaValidator (core_schema .union_schema (choices , mode = 'left_to_right' , strict = True ))
506+ v = SchemaValidator (core_schema .union_schema (choices , mode = 'left_to_right' ))
476507 out = v .validate_python (1 )
477508 assert out == 1
478509 assert isinstance (out , int )
@@ -482,7 +513,12 @@ def test_left_to_right_union_strict():
482513 assert isinstance (out , float )
483514
484515 # reversing union will select float always (as strict float will accept int)
485- v = SchemaValidator (core_schema .union_schema (list (reversed (choices )), mode = 'left_to_right' , strict = True ))
516+ v = SchemaValidator (
517+ core_schema .union_schema (
518+ list (reversed (choices )),
519+ mode = 'left_to_right' ,
520+ )
521+ )
486522 out = v .validate_python (1.0 )
487523 assert out == 1.0
488524 assert isinstance (out , float )
0 commit comments