@@ -35,7 +35,7 @@ pub struct LiteralLookup<T: Debug> {
3535 expected_py_dict : Option < Py < PyDict > > ,
3636 // Catch all for unhashable types like list
3737 expected_py_values : Option < Vec < ( Py < PyAny > , usize ) > > ,
38- // Fallback for ints, bools, and strings to use Python equality checks
38+ // Fallback for ints, bools, and strings to use Python hash and equality checks
3939 expected_py_primitives : Option < Vec < ( Py < PyAny > , usize ) > > ,
4040
4141 pub values : Vec < T > ,
@@ -159,9 +159,17 @@ impl<T: Debug> LiteralLookup<T> {
159159
160160 if let Some ( expected_py_primitives) = & self . expected_py_primitives {
161161 let py_input = py_input. get_or_insert_with ( || input. to_object ( py) ) ;
162+ let py_input_bound = py_input. bind ( py) ;
163+
162164 for ( k, id) in expected_py_primitives {
163- if k. bind ( py) . eq ( & * py_input) . unwrap_or ( false ) {
164- return Ok ( Some ( ( input, & self . values [ * id] ) ) ) ;
165+ let bound_k = k. bind ( py) ;
166+ if bound_k. eq ( & * py_input) . unwrap_or ( false ) {
167+ match ( bound_k. hash ( ) , py_input_bound. hash ( ) ) {
168+ ( Ok ( k_hash) , Ok ( input_hash) ) if k_hash == input_hash => {
169+ return Ok ( Some ( ( input, & self . values [ * id] ) ) ) ;
170+ }
171+ _ => continue , // Skip to the next item on hash failure or mismatch
172+ }
165173 }
166174 }
167175 } ;
0 commit comments