File tree Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -121,7 +121,15 @@ impl<T: EnumValidateValue> Validator for EnumValidator<T> {
121121 return Ok ( v) ;
122122 } else if let Some ( ref missing) = self . missing {
123123 state. floor_exactness ( Exactness :: Lax ) ;
124- let enum_value = missing. bind ( py) . call1 ( ( input. to_object ( py) , ) ) ?;
124+ let enum_value = missing. bind ( py) . call1 ( ( input. to_object ( py) , ) ) . map_err ( |_| {
125+ ValError :: new (
126+ ErrorType :: Enum {
127+ expected : self . expected_repr . clone ( ) ,
128+ context : None ,
129+ } ,
130+ input,
131+ )
132+ } ) ?;
125133 // check enum_value is an instance of the class like
126134 // https://github.com/python/cpython/blob/v3.12.2/Lib/enum.py#L1148
127135 if enum_value. is_instance ( class) ? {
Original file line number Diff line number Diff line change 11import re
22import sys
3- from enum import Enum
3+ from enum import Enum , IntFlag
44
55import pytest
66
@@ -267,3 +267,21 @@ class MyEnum(Enum):
267267
268268 with pytest .raises (SchemaError , match = '`members` should have length > 0' ):
269269 SchemaValidator (core_schema .enum_schema (MyEnum , []))
270+
271+
272+ def test_missing_error_converted_to_val_error () -> None :
273+ class MyFlags (IntFlag ):
274+ OFF = 0
275+ ON = 1
276+
277+ v = SchemaValidator (
278+ core_schema .with_default_schema (
279+ schema = core_schema .enum_schema (MyFlags , list (MyFlags .__members__ .values ())), default = MyFlags .OFF
280+ )
281+ )
282+
283+ assert v .validate_python (MyFlags .OFF ) is MyFlags .OFF
284+ assert v .validate_python (0 ) is MyFlags .OFF
285+
286+ with pytest .raises (ValidationError ):
287+ v .validate_python (None )
You can’t perform that action at this time.
0 commit comments