ashb commented on a change in pull request #15034: URL: https://github.com/apache/airflow/pull/15034#discussion_r602597009
########## File path: airflow/decorators/task_group.py ########## @@ -0,0 +1,78 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +A TaskGroup is a collection of closely related tasks on the same DAG that should be grouped +together when the DAG is displayed graphically. +""" +import functools +from inspect import signature +from typing import TYPE_CHECKING, Callable, Optional, TypeVar, cast + +from airflow.utils.task_group import TaskGroup + +if TYPE_CHECKING: + pass + +T = TypeVar("T", bound=Callable) # pylint: disable=invalid-name + + +def task_group(python_callable: Optional[Callable] = None, *tg_args, **tg_kwargs) -> Callable[[T], T]: + """ + Python TaskGroup decorator. Wraps a function into an Airflow TaskGroup. + Accepts kwargs for operator TaskGroup. Can be used to parametrize TaskGroup. + + :param python_callable: Function to decorate + :param tg_args: Arguments for TaskGroup object + :type tg_args: list + :param tg_kwargs: Kwargs for TaskGroup object. + :type tg_kwargs: dict + """ + + def wrapper(f: T): + + # Setting group_id as function name if not given in kwarg group_id + if len(tg_args) == 0 and 'group_id' not in tg_kwargs.keys(): + tg_kwargs['group_id'] = f.__name__ + + # Get dag initializer signature and bind it to validate that task_group_args, + # and task_group_kwargs are correct + task_group_sig = signature(TaskGroup.__init__) + task_group_bound_args = task_group_sig.bind_partial(*tg_args, **tg_kwargs) + + @functools.wraps(f) + def factory(*args, **kwargs): + # Generate signature for decorated function and bind the arguments when called + # we do this to extract parameters so we can annotate them on the DAG object. + # In addition, this fails if we are missing any args/kwargs with TypeError as expected. + f_sig = signature(f).bind(*args, **kwargs) + # Apply defaults to capture default values if set. + f_sig.apply_defaults() Review comment: Perhaps just move the `f_sig = signature(f)` to outside of the factory - when it's called once it will make no difference, but if the TG is used more than once getting the signatures are relatively costly. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
