8
8
import pytest
9
9
from dirty_equals import IsFloat , IsInt
10
10
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
12
12
13
13
from ..conftest import plain_repr
14
14
@@ -262,16 +262,47 @@ def test_one_choice():
262
262
assert v .validate_python ('hello' ) == 'hello'
263
263
264
264
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 :
266
280
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 ),
268
283
)
284
+
269
285
assert v .validate_python (1 ) == 1
270
286
assert v .validate_python (123 ) == 123
271
287
272
288
with pytest .raises (ValidationError ) as exc_info :
273
289
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
+ ]
274
294
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' )
275
306
assert exc_info .value .errors (include_url = False ) == [
276
307
{'type' : 'bool_type' , 'loc' : ('bool' ,), 'msg' : 'Input should be a valid boolean' , 'input' : '123' },
277
308
{'type' : 'int_type' , 'loc' : ('int' ,), 'msg' : 'Input should be a valid integer' , 'input' : '123' },
@@ -469,10 +500,10 @@ def test_left_to_right_union():
469
500
470
501
471
502
def 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 )]
473
504
474
505
# 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' ))
476
507
out = v .validate_python (1 )
477
508
assert out == 1
478
509
assert isinstance (out , int )
@@ -482,7 +513,12 @@ def test_left_to_right_union_strict():
482
513
assert isinstance (out , float )
483
514
484
515
# 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
+ )
486
522
out = v .validate_python (1.0 )
487
523
assert out == 1.0
488
524
assert isinstance (out , float )
0 commit comments