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> {
121
121
return Ok ( v) ;
122
122
} else if let Some ( ref missing) = self . missing {
123
123
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
+ } ) ?;
125
133
// check enum_value is an instance of the class like
126
134
// https://github.com/python/cpython/blob/v3.12.2/Lib/enum.py#L1148
127
135
if enum_value. is_instance ( class) ? {
Original file line number Diff line number Diff line change 1
1
import re
2
2
import sys
3
- from enum import Enum
3
+ from enum import Enum , IntFlag
4
4
5
5
import pytest
6
6
@@ -267,3 +267,21 @@ class MyEnum(Enum):
267
267
268
268
with pytest .raises (SchemaError , match = '`members` should have length > 0' ):
269
269
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