@@ -11,7 +11,7 @@ use crate::build_tools::py_schema_err;
1111use crate :: build_tools:: { schema_or_config_same, ExtraBehavior } ;
1212use crate :: errors:: { ErrorTypeDefaults , ValError , ValLineError , ValResult } ;
1313use crate :: input:: { Arguments , BorrowInput , Input , KeywordArgs , PositionalArgs , ValidationMatch } ;
14- use crate :: lookup_key:: { get_lookup_key, LookupKey } ;
14+ use crate :: lookup_key:: get_lookup_key;
1515use crate :: tools:: SchemaDict ;
1616
1717use super :: validation_state:: ValidationState ;
@@ -42,9 +42,10 @@ impl FromStr for VarKwargsMode {
4242struct Parameter {
4343 positional : bool ,
4444 name : String ,
45- kw_lookup_key : Option < LookupKey > ,
4645 kwarg_key : Option < Py < PyString > > ,
4746 validator : CombinedValidator ,
47+ alias : Option < Py < PyAny > > ,
48+ mode : String ,
4849}
4950
5051#[ derive( Debug ) ]
@@ -56,6 +57,8 @@ pub struct ArgumentsValidator {
5657 var_kwargs_validator : Option < Box < CombinedValidator > > ,
5758 loc_by_alias : bool ,
5859 extra : ExtraBehavior ,
60+ validate_by_alias : bool ,
61+ validate_by_name : bool ,
5962}
6063
6164impl BuildValidator for ArgumentsValidator {
@@ -75,10 +78,6 @@ impl BuildValidator for ArgumentsValidator {
7578 let mut had_default_arg = false ;
7679 let mut had_keyword_only = false ;
7780
78- let validate_by_name = schema_or_config_same ( schema, config, intern ! ( py, "validate_by_name" ) ) ?. unwrap_or ( false ) ;
79- let validate_by_alias =
80- schema_or_config_same ( schema, config, intern ! ( py, "validate_by_alias" ) ) ?. unwrap_or ( true ) ;
81-
8281 for ( arg_index, arg) in arguments_schema. iter ( ) . enumerate ( ) {
8382 let arg = arg. downcast :: < PyDict > ( ) ?;
8483
@@ -99,19 +98,11 @@ impl BuildValidator for ArgumentsValidator {
9998 had_keyword_only = true ;
10099 }
101100
102- let mut kw_lookup_key = None ;
103- let mut kwarg_key = None ;
104- if mode == "keyword_only" || mode == "positional_or_keyword" {
105- let validation_alias = arg. get_item ( intern ! ( py, "alias" ) ) ?;
106- kw_lookup_key = Some ( get_lookup_key (
107- py,
108- validation_alias,
109- validate_by_name,
110- validate_by_alias,
111- name. as_str ( ) ,
112- ) ?) ;
113- kwarg_key = Some ( py_name. unbind ( ) ) ;
114- }
101+ let kwarg_key = if mode == "keyword_only" || mode == "positional_or_keyword" {
102+ Some ( py_name. unbind ( ) )
103+ } else {
104+ None
105+ } ;
115106
116107 let schema = arg. get_as_req ( intern ! ( py, "schema" ) ) ?;
117108
@@ -138,9 +129,10 @@ impl BuildValidator for ArgumentsValidator {
138129 parameters. push ( Parameter {
139130 positional,
140131 name,
141- kw_lookup_key,
142132 kwarg_key,
143133 validator,
134+ alias : arg. get_item ( intern ! ( py, "alias" ) ) ?. map ( std:: convert:: Into :: into) ,
135+ mode : mode. to_string ( ) ,
144136 } ) ;
145137 }
146138
@@ -171,6 +163,8 @@ impl BuildValidator for ArgumentsValidator {
171163 var_kwargs_validator,
172164 loc_by_alias : config. get_as ( intern ! ( py, "loc_by_alias" ) ) ?. unwrap_or ( true ) ,
173165 extra : ExtraBehavior :: from_schema_or_config ( py, schema, config, ExtraBehavior :: Forbid ) ?,
166+ validate_by_alias : schema_or_config_same ( schema, config, intern ! ( py, "validate_by_alias" ) ) ?. unwrap_or ( true ) ,
167+ validate_by_name : schema_or_config_same ( schema, config, intern ! ( py, "validate_by_name" ) ) ?. unwrap_or ( false ) ,
174168 }
175169 . into ( ) )
176170 }
@@ -199,7 +193,10 @@ impl Validator for ArgumentsValidator {
199193 let mut output_args: Vec < PyObject > = Vec :: with_capacity ( self . positional_params_count ) ;
200194 let output_kwargs = PyDict :: new ( py) ;
201195 let mut errors: Vec < ValLineError > = Vec :: new ( ) ;
202- let mut used_kwargs: AHashSet < & str > = AHashSet :: with_capacity ( self . parameters . len ( ) ) ;
196+ let mut used_kwargs: AHashSet < String > = AHashSet :: with_capacity ( self . parameters . len ( ) ) ;
197+
198+ let validate_by_alias = state. validate_by_alias_or ( self . validate_by_alias ) ;
199+ let validate_by_name = state. validate_by_name_or ( self . validate_by_name ) ;
203200
204201 // go through arguments getting the value from args or kwargs and validating it
205202 for ( index, parameter) in self . parameters . iter ( ) . enumerate ( ) {
@@ -210,10 +207,21 @@ impl Validator for ArgumentsValidator {
210207 }
211208 }
212209 let mut kw_value = None ;
210+ let mut kw_lookup_key = None ;
211+ if parameter. mode == "keyword_only" || parameter. mode == "positional_or_keyword" {
212+ kw_lookup_key = Some ( get_lookup_key (
213+ py,
214+ parameter. alias . as_ref ( ) ,
215+ validate_by_name,
216+ validate_by_alias,
217+ & parameter. name ,
218+ ) ?) ;
219+ }
220+
213221 if let Some ( kwargs) = args. kwargs ( ) {
214- if let Some ( ref lookup_key) = parameter . kw_lookup_key {
222+ if let Some ( ref lookup_key) = kw_lookup_key {
215223 if let Some ( ( lookup_path, value) ) = kwargs. get_item ( lookup_key) ? {
216- used_kwargs. insert ( lookup_path. first_key ( ) ) ;
224+ used_kwargs. insert ( lookup_path. first_key ( ) . to_string ( ) ) ;
217225 kw_value = Some ( ( lookup_path, value) ) ;
218226 }
219227 }
@@ -257,7 +265,7 @@ impl Validator for ArgumentsValidator {
257265 } else {
258266 output_args. push ( value) ;
259267 }
260- } else if let Some ( ref lookup_key) = parameter . kw_lookup_key {
268+ } else if let Some ( lookup_key) = kw_lookup_key {
261269 let error_type = if parameter. positional {
262270 ErrorTypeDefaults :: MissingArgument
263271 } else {
0 commit comments