File tree Expand file tree Collapse file tree 4 files changed +34
-7
lines changed Expand file tree Collapse file tree 4 files changed +34
-7
lines changed Original file line number Diff line number Diff line change @@ -549,6 +549,7 @@ impl<'a> EitherInt<'a> {
549
549
Ok ( Self :: BigInt ( big_int) )
550
550
}
551
551
}
552
+
552
553
pub fn into_i64 ( self , py : Python < ' a > ) -> ValResult < i64 > {
553
554
match self {
554
555
EitherInt :: I64 ( i) => Ok ( i) ,
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ use core::fmt::Debug;
4
4
use std:: cmp:: Ordering ;
5
5
6
6
use pyo3:: prelude:: * ;
7
- use pyo3:: types:: { PyDict , PyList } ;
7
+ use pyo3:: types:: { PyDict , PyInt , PyList } ;
8
8
use pyo3:: { intern, PyTraverseError , PyVisit } ;
9
9
10
10
use ahash:: AHashMap ;
@@ -58,11 +58,13 @@ impl<T: Debug> LiteralLookup<T> {
58
58
expected_bool. false_id = Some ( id) ;
59
59
}
60
60
}
61
- if let Ok ( either_int) = k. exact_int ( ) {
62
- let int = either_int
63
- . into_i64 ( py)
64
- . map_err ( |_| py_schema_error_type ! ( "error extracting int {:?}" , k) ) ?;
65
- expected_int. insert ( int, id) ;
61
+ if k. is_exact_instance_of :: < PyInt > ( ) {
62
+ if let Ok ( int_64) = k. extract :: < i64 > ( ) {
63
+ expected_int. insert ( int_64, id) ;
64
+ } else {
65
+ // cover the case of an int that's > i64::MAX etc.
66
+ expected_py_dict. set_item ( k, id) ?;
67
+ }
66
68
} else if let Ok ( either_str) = k. exact_str ( ) {
67
69
let str = either_str
68
70
. as_cow ( )
Original file line number Diff line number Diff line change 1
1
import re
2
2
import sys
3
- from enum import Enum , IntFlag
3
+ from enum import Enum , IntEnum , IntFlag
4
4
5
5
import pytest
6
6
@@ -331,3 +331,16 @@ class MyFlags(IntFlag):
331
331
332
332
with pytest .raises (ValidationError ):
333
333
v .validate_python (None )
334
+
335
+
336
+ def test_big_int ():
337
+ class ColorEnum (IntEnum ):
338
+ GREEN = 1 << 63
339
+ BLUE = 1 << 64
340
+
341
+ v = SchemaValidator (
342
+ core_schema .with_default_schema (schema = core_schema .enum_schema (ColorEnum , list (ColorEnum .__members__ .values ())))
343
+ )
344
+
345
+ assert v .validate_python (ColorEnum .GREEN ) is ColorEnum .GREEN
346
+ assert v .validate_python (1 << 63 ) is ColorEnum .GREEN
Original file line number Diff line number Diff line change @@ -378,3 +378,14 @@ class Foo(str, Enum):
378
378
with pytest .raises (ValidationError ) as exc_info :
379
379
v .validate_python ('bar_val' )
380
380
assert exc_info .value .errors (include_url = False ) == err
381
+
382
+
383
+ def test_big_int ():
384
+ big_int = 2 ** 64 + 1
385
+ massive_int = 2 ** 128 + 1
386
+ v = SchemaValidator (core_schema .literal_schema ([big_int , massive_int ]))
387
+ assert v .validate_python (big_int ) == big_int
388
+ assert v .validate_python (massive_int ) == massive_int
389
+ m = r'Input should be 18446744073709551617 or 340282366920938463463374607431768211457 \[type=literal_error'
390
+ with pytest .raises (ValidationError , match = m ):
391
+ v .validate_python (37 )
You can’t perform that action at this time.
0 commit comments