@@ -352,14 +352,12 @@ class ColorEnum(IntEnum):
352352 [- 1 , 0 , 1 ],
353353)
354354def test_enum_int_validation_should_succeed_for_decimal (value : int ):
355- # GIVEN
356355 class MyEnum (Enum ):
357356 VALUE = value
358357
359358 class MyIntEnum (IntEnum ):
360359 VALUE = value
361360
362- # WHEN
363361 v = SchemaValidator (
364362 core_schema .with_default_schema (
365363 schema = core_schema .enum_schema (MyEnum , list (MyEnum .__members__ .values ())),
@@ -374,74 +372,65 @@ class MyIntEnum(IntEnum):
374372 )
375373 )
376374
377- # THEN
378375 assert v .validate_python (Decimal (value )) is MyEnum .VALUE
379376 assert v .validate_python (Decimal (float (value ))) is MyEnum .VALUE
380-
381377 assert v_int .validate_python (Decimal (value )) is MyIntEnum .VALUE
382378 assert v_int .validate_python (Decimal (float (value ))) is MyIntEnum .VALUE
383379
384380
385381def test_enum_int_validation_should_succeed_for_custom_type ():
386- # GIVEN
387382 class AnyWrapper :
388383 def __init__ (self , value ):
389384 self .value = value
390385
391386 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
393390
394391 class MyEnum (Enum ):
395392 VALUE = 999
396393 SECOND_VALUE = 1000000
397394 THIRD_VALUE = 'Py03'
398395
399- # WHEN
400396 v = SchemaValidator (
401397 core_schema .with_default_schema (
402398 schema = core_schema .enum_schema (MyEnum , list (MyEnum .__members__ .values ())),
403399 default = MyEnum .VALUE ,
404400 )
405401 )
406402
407- # THEN
408403 assert v .validate_python (AnyWrapper (999 )) is MyEnum .VALUE
409404 assert v .validate_python (AnyWrapper (1000000 )) is MyEnum .SECOND_VALUE
410405 assert v .validate_python (AnyWrapper ('Py03' )) is MyEnum .THIRD_VALUE
411406
412407
413408def test_enum_str_validation_should_fail_for_decimal_when_expecting_str_value ():
414- # GIVEN
415409 class MyEnum (Enum ):
416410 VALUE = '1'
417411
418- # WHEN
419412 v = SchemaValidator (
420413 core_schema .with_default_schema (
421414 schema = core_schema .enum_schema (MyEnum , list (MyEnum .__members__ .values ())),
422415 default = MyEnum .VALUE ,
423416 )
424417 )
425418
426- # THEN
427419 with pytest .raises (ValidationError ):
428420 v .validate_python (Decimal (1 ))
429421
430422
431423def test_enum_int_validation_should_fail_for_incorrect_decimal_value ():
432- # GIVEN
433424 class MyEnum (Enum ):
434425 VALUE = 1
435426
436- # WHEN
437427 v = SchemaValidator (
438428 core_schema .with_default_schema (
439429 schema = core_schema .enum_schema (MyEnum , list (MyEnum .__members__ .values ())),
440430 default = MyEnum .VALUE ,
441431 )
442432 )
443433
444- # THEN
445434 with pytest .raises (ValidationError ):
446435 v .validate_python (Decimal (2 ))
447436
@@ -453,22 +442,46 @@ class MyEnum(Enum):
453442
454443
455444def test_enum_int_validation_should_fail_for_plain_type_without_eq_checking ():
456- # GIVEN
457445 class MyEnum (Enum ):
458446 VALUE = 1
459447
460448 class MyClass :
461449 def __init__ (self , value ):
462450 self .value = value
463451
464- # WHEN
465452 v = SchemaValidator (
466453 core_schema .with_default_schema (
467454 schema = core_schema .enum_schema (MyEnum , list (MyEnum .__members__ .values ())),
468455 default = MyEnum .VALUE ,
469456 )
470457 )
471458
472- # THEN
473459 with pytest .raises (ValidationError ):
474460 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