From 6e011185bb4bb7611c53a919e58f1a95624b4b1a Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 30 Aug 2024 02:23:59 -0400 Subject: [PATCH] Support the unpack operator in signature (#41316) Co-authored-by: Tzu-ping Chung Co-authored-by: jabbera Co-authored-by: jabbera --- airflow/utils/operator_helpers.py | 8 +++++++- tests/utils/test_operator_helpers.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/airflow/utils/operator_helpers.py b/airflow/utils/operator_helpers.py index dbbe562120f00..108a84a9eabb9 100644 --- a/airflow/utils/operator_helpers.py +++ b/airflow/utils/operator_helpers.py @@ -162,7 +162,13 @@ def determine( signature = inspect.signature(func) has_wildcard_kwargs = any(p.kind == p.VAR_KEYWORD for p in signature.parameters.values()) - for name in itertools.islice(signature.parameters.keys(), len(args)): + for name, param in itertools.islice(signature.parameters.items(), len(args)): + # Keyword-only arguments can't be passed positionally and are not checked. + if param.kind == inspect.Parameter.KEYWORD_ONLY: + continue + if param.kind == inspect.Parameter.VAR_KEYWORD: + continue + # Check if args conflict with names in kwargs. if name in kwargs: raise ValueError(f"The key {name!r} in args is a part of kwargs and therefore reserved.") diff --git a/tests/utils/test_operator_helpers.py b/tests/utils/test_operator_helpers.py index f46ad2e98a3fc..9ebd9d08ae00c 100644 --- a/tests/utils/test_operator_helpers.py +++ b/tests/utils/test_operator_helpers.py @@ -224,3 +224,15 @@ def func(ds_nodash): kwargs_callable(*args, **kwargs) assert "ds_nodash" in str(exc_info) + + +@pytest.mark.parametrize( + "func,args,kwargs,expected", + [ + (callable10, (1, 2), {"ds_nodash": 1}, {"ds_nodash": 1}), + (callable11, (1, 2), {"ds_nodash": 1}, {"ds_nodash": 1}), + ], +) +def test_args_and_kwargs_conflicts(func, args, kwargs, expected): + kwargs_result = operator_helpers.determine_kwargs(func, args=args, kwargs=kwargs) + assert expected == kwargs_result