@@ -352,14 +352,12 @@ class ColorEnum(IntEnum):
352
352
[- 1 , 0 , 1 ],
353
353
)
354
354
def test_enum_int_validation_should_succeed_for_decimal (value : int ):
355
- # GIVEN
356
355
class MyEnum (Enum ):
357
356
VALUE = value
358
357
359
358
class MyIntEnum (IntEnum ):
360
359
VALUE = value
361
360
362
- # WHEN
363
361
v = SchemaValidator (
364
362
core_schema .with_default_schema (
365
363
schema = core_schema .enum_schema (MyEnum , list (MyEnum .__members__ .values ())),
@@ -374,74 +372,65 @@ class MyIntEnum(IntEnum):
374
372
)
375
373
)
376
374
377
- # THEN
378
375
assert v .validate_python (Decimal (value )) is MyEnum .VALUE
379
376
assert v .validate_python (Decimal (float (value ))) is MyEnum .VALUE
380
-
381
377
assert v_int .validate_python (Decimal (value )) is MyIntEnum .VALUE
382
378
assert v_int .validate_python (Decimal (float (value ))) is MyIntEnum .VALUE
383
379
384
380
385
381
def test_enum_int_validation_should_succeed_for_custom_type ():
386
- # GIVEN
387
382
class AnyWrapper :
388
383
def __init__ (self , value ):
389
384
self .value = value
390
385
391
386
def __eq__ (self , other : object ) -> bool :
392
- return self .value == other
387
+ if sys .version_info < (3 , 13 ):
388
+ return self .value == other
389
+ return self .value == other .value
393
390
394
391
class MyEnum (Enum ):
395
392
VALUE = 999
396
393
SECOND_VALUE = 1000000
397
394
THIRD_VALUE = 'Py03'
398
395
399
- # WHEN
400
396
v = SchemaValidator (
401
397
core_schema .with_default_schema (
402
398
schema = core_schema .enum_schema (MyEnum , list (MyEnum .__members__ .values ())),
403
399
default = MyEnum .VALUE ,
404
400
)
405
401
)
406
402
407
- # THEN
408
403
assert v .validate_python (AnyWrapper (999 )) is MyEnum .VALUE
409
404
assert v .validate_python (AnyWrapper (1000000 )) is MyEnum .SECOND_VALUE
410
405
assert v .validate_python (AnyWrapper ('Py03' )) is MyEnum .THIRD_VALUE
411
406
412
407
413
408
def test_enum_str_validation_should_fail_for_decimal_when_expecting_str_value ():
414
- # GIVEN
415
409
class MyEnum (Enum ):
416
410
VALUE = '1'
417
411
418
- # WHEN
419
412
v = SchemaValidator (
420
413
core_schema .with_default_schema (
421
414
schema = core_schema .enum_schema (MyEnum , list (MyEnum .__members__ .values ())),
422
415
default = MyEnum .VALUE ,
423
416
)
424
417
)
425
418
426
- # THEN
427
419
with pytest .raises (ValidationError ):
428
420
v .validate_python (Decimal (1 ))
429
421
430
422
431
423
def test_enum_int_validation_should_fail_for_incorrect_decimal_value ():
432
- # GIVEN
433
424
class MyEnum (Enum ):
434
425
VALUE = 1
435
426
436
- # WHEN
437
427
v = SchemaValidator (
438
428
core_schema .with_default_schema (
439
429
schema = core_schema .enum_schema (MyEnum , list (MyEnum .__members__ .values ())),
440
430
default = MyEnum .VALUE ,
441
431
)
442
432
)
443
433
444
- # THEN
445
434
with pytest .raises (ValidationError ):
446
435
v .validate_python (Decimal (2 ))
447
436
@@ -453,22 +442,46 @@ class MyEnum(Enum):
453
442
454
443
455
444
def test_enum_int_validation_should_fail_for_plain_type_without_eq_checking ():
456
- # GIVEN
457
445
class MyEnum (Enum ):
458
446
VALUE = 1
459
447
460
448
class MyClass :
461
449
def __init__ (self , value ):
462
450
self .value = value
463
451
464
- # WHEN
465
452
v = SchemaValidator (
466
453
core_schema .with_default_schema (
467
454
schema = core_schema .enum_schema (MyEnum , list (MyEnum .__members__ .values ())),
468
455
default = MyEnum .VALUE ,
469
456
)
470
457
)
471
458
472
- # THEN
473
459
with pytest .raises (ValidationError ):
474
460
v .validate_python (MyClass (1 ))
461
+
462
+
463
+ def support_custom_new_method () -> None :
464
+ """Demonstrates support for custom new methods, as well as conceptually, multi-value enums without dependency on a 3rd party lib for testing."""
465
+
466
+ class Animal (Enum ):
467
+ CAT = 'cat' , 'meow'
468
+ DOG = 'dog' , 'woof'
469
+
470
+ def __new__ (cls , species : str , sound : str ):
471
+ obj = object .__new__ (cls )
472
+
473
+ obj ._value_ = species
474
+ obj ._all_values = (species , sound )
475
+
476
+ obj .species = species
477
+ obj .sound = sound
478
+
479
+ cls ._value2member_map_ [sound ] = obj
480
+
481
+ return obj
482
+
483
+ v = SchemaValidator (core_schema .enum_schema (Animal , list (Animal .__members__ .values ())))
484
+ assert v .validate_python ('cat' ) is Animal .CAT
485
+ assert v .validate_python ('meow' ) is Animal .CAT
486
+ assert v .validate_python ('dog' ) is Animal .DOG
487
+ assert v .validate_python ('woof' ) is Animal .DOG
0 commit comments