@@ -10,7 +10,7 @@ use crate::input::BorrowInput;
1010use crate :: input:: ConsumeIterator ;
1111use crate :: input:: ValidationMatch ;
1212use crate :: input:: { Input , ValidatedDict } ;
13- use crate :: lookup_key:: get_lookup_key ;
13+ use crate :: lookup_key:: LookupKeyCollection ;
1414use crate :: tools:: SchemaDict ;
1515use ahash:: AHashSet ;
1616use jiter:: PartialMode ;
@@ -20,7 +20,7 @@ use super::{build_validator, BuildValidator, CombinedValidator, DefinitionsBuild
2020#[ derive( Debug ) ]
2121struct TypedDictField {
2222 name : String ,
23- alias : Option < Py < PyAny > > ,
23+ lookup_key_collection : LookupKeyCollection ,
2424 name_py : Py < PyString > ,
2525 required : bool ,
2626 validator : CombinedValidator ,
@@ -109,11 +109,12 @@ impl BuildValidator for TypedDictValidator {
109109 }
110110 }
111111
112+ let validation_alias = field_info. get_item ( intern ! ( py, "validation_alias" ) ) ?;
113+ let lookup_key_collection = LookupKeyCollection :: new ( py, validation_alias, field_name) ?;
114+
112115 fields. push ( TypedDictField {
113116 name : field_name. to_string ( ) ,
114- alias : field_info
115- . get_item ( intern ! ( py, "validation_alias" ) ) ?
116- . map ( std:: convert:: Into :: into) ,
117+ lookup_key_collection,
117118 name_py : field_name_py. into ( ) ,
118119 validator,
119120 required,
@@ -162,7 +163,7 @@ impl Validator for TypedDictValidator {
162163
163164 // we only care about which keys have been used if we're iterating over the object for extra after
164165 // the first pass
165- let mut used_keys: Option < AHashSet < String > > =
166+ let mut used_keys: Option < AHashSet < & str > > =
166167 if self . extra_behavior == ExtraBehavior :: Ignore || dict. is_py_get_attr ( ) {
167168 None
168169 } else {
@@ -175,14 +176,10 @@ impl Validator for TypedDictValidator {
175176 let mut fields_set_count: usize = 0 ;
176177
177178 for field in & self . fields {
178- let lookup_key = get_lookup_key (
179- py,
180- field. alias . as_ref ( ) ,
181- validate_by_name,
182- validate_by_alias,
183- & field. name ,
184- ) ?;
185- let op_key_value = match dict. get_item ( & lookup_key) {
179+ let lookup_key = field
180+ . lookup_key_collection
181+ . select ( validate_by_alias, validate_by_name) ?;
182+ let op_key_value = match dict. get_item ( lookup_key) {
186183 Ok ( v) => v,
187184 Err ( ValError :: LineErrors ( line_errors) ) => {
188185 let field_loc: LocItem = field. name . clone ( ) . into ( ) ;
@@ -199,7 +196,7 @@ impl Validator for TypedDictValidator {
199196 if let Some ( ref mut used_keys) = used_keys {
200197 // key is "used" whether or not validation passes, since we want to skip this key in
201198 // extra logic either way
202- used_keys. insert ( lookup_path. first_key ( ) . to_string ( ) ) ;
199+ used_keys. insert ( lookup_path. first_key ( ) ) ;
203200 }
204201 let is_last_partial = if let Some ( ref last_key) = partial_last_key {
205202 let first_key_loc: LocItem = lookup_path. first_key ( ) . into ( ) ;
@@ -265,7 +262,7 @@ impl Validator for TypedDictValidator {
265262 if let Some ( used_keys) = used_keys {
266263 struct ValidateExtras < ' a , ' s , ' py > {
267264 py : Python < ' py > ,
268- used_keys : AHashSet < String > ,
265+ used_keys : AHashSet < & ' a str > ,
269266 errors : & ' a mut Vec < ValLineError > ,
270267 extras_validator : Option < & ' a CombinedValidator > ,
271268 output_dict : & ' a Bound < ' py , PyDict > ,
0 commit comments