Skip to content

Commit

Permalink
Added handling of cases with double *args and **kwargs addition
Browse files Browse the repository at this point in the history
  • Loading branch information
ApostolFet committed Dec 4, 2024
1 parent 3ff963d commit e4e0c15
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/dishka/integrations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def auto_injected_func(*args: P.args, **kwargs: P.kwargs) -> T:


def _add_params(
params: list[Parameter],
params: Sequence[Parameter],
additional_params: Sequence[Parameter],
):
params_kind_dict: dict[_ParameterKind, list[Parameter]] = {}
Expand All @@ -237,13 +237,33 @@ def _add_params(
for param in additional_params:
params_kind_dict.setdefault(param.kind, []).append(param)

result_params = []
result_params.extend(params_kind_dict.get(Parameter.POSITIONAL_ONLY, []))
result_params.extend(
params_kind_dict.get(Parameter.POSITIONAL_OR_KEYWORD, []),

var_positional = params_kind_dict.get(Parameter.VAR_POSITIONAL, [])
if len(var_positional) > 1:
param_names = (param.name for param in var_positional)
var_positional_names = ", *".join(param_names)
base_msg = "more than one var positional parameter: *"
msg = base_msg + var_positional_names
raise ValueError(msg)

var_keyword = params_kind_dict.get(Parameter.VAR_KEYWORD, [])
if len(var_keyword) > 1:
var_keyword_names = ", **".join(param.name for param in var_keyword)
msg = "more than one var keyword parameter: " + var_keyword_names
raise ValueError(msg)

positional_only = params_kind_dict.get(Parameter.POSITIONAL_ONLY, [])
positional_or_keyword = params_kind_dict.get(
Parameter.POSITIONAL_OR_KEYWORD,
[],
)
result_params.extend(params_kind_dict.get(Parameter.VAR_POSITIONAL, []))
result_params.extend(params_kind_dict.get(Parameter.KEYWORD_ONLY, []))
result_params.extend(params_kind_dict.get(Parameter.VAR_KEYWORD, []))
keyword_only = params_kind_dict.get(Parameter.KEYWORD_ONLY, [])

result_params = []
result_params.extend(positional_only)
result_params.extend(positional_or_keyword)
result_params.extend(var_positional)
result_params.extend(keyword_only)
result_params.extend(var_keyword)

return result_params

0 comments on commit e4e0c15

Please sign in to comment.