File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -48,6 +48,20 @@ use super::{
48
48
Input ,
49
49
} ;
50
50
51
+ static ORDERED_DICT_TYPE : PyOnceLock < Py < PyType > > = PyOnceLock :: new ( ) ;
52
+
53
+ fn get_ordered_dict_type ( py : Python < ' _ > ) -> & Bound < ' _ , PyType > {
54
+ ORDERED_DICT_TYPE
55
+ . get_or_init ( py, || {
56
+ py. import ( "collections" )
57
+ . and_then ( |collections_module| collections_module. getattr ( "OrderedDict" ) )
58
+ . unwrap ( )
59
+ . extract ( )
60
+ . unwrap ( )
61
+ } )
62
+ . bind ( py)
63
+ }
64
+
51
65
static FRACTION_TYPE : PyOnceLock < Py < PyType > > = PyOnceLock :: new ( ) ;
52
66
53
67
pub fn get_fraction_type ( py : Python < ' _ > ) -> & Bound < ' _ , PyType > {
@@ -399,6 +413,13 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
399
413
}
400
414
401
415
fn lax_dict < ' a > ( & ' a self ) -> ValResult < GenericPyMapping < ' a , ' py > > {
416
+ let ordered_dict_type = get_ordered_dict_type ( self . py ( ) ) ;
417
+ if self . is_instance ( ordered_dict_type) . unwrap_or ( false ) {
418
+ // OrderedDict is a subclass of dict, but we want to treat it as a mapping to preserve order
419
+ if let Ok ( mapping) = self . downcast :: < PyMapping > ( ) {
420
+ return Ok ( GenericPyMapping :: Mapping ( mapping) ) ;
421
+ }
422
+ }
402
423
if let Ok ( dict) = self . downcast :: < PyDict > ( ) {
403
424
Ok ( GenericPyMapping :: Dict ( dict) )
404
425
} else if let Ok ( mapping) = self . downcast :: < PyMapping > ( ) {
You can’t perform that action at this time.
0 commit comments