turbaszek commented on a change in pull request #8962:
URL: https://github.com/apache/airflow/pull/8962#discussion_r438640458
##########
File path: airflow/operators/python.py
##########
@@ -145,6 +149,131 @@ def execute_callable(self):
return self.python_callable(*self.op_args, **self.op_kwargs)
+class _PythonFunctionalOperator(BaseOperator):
+ """
+ Wraps a Python callable and captures args/kwargs when called for execution.
+
+ :param python_callable: A reference to an object that is callable
+ :type python_callable: python callable
+ :param op_kwargs: a dictionary of keyword arguments that will get unpacked
+ in your function
+ :type op_kwargs: dict (templated)
+ :param op_args: a list of positional arguments that will get unpacked when
+ calling your callable
+ :type op_args: list (templated)
+ :param multiple_outputs: if set, function return value will be
+ unrolled to multiple XCom values. Dict will unroll to xcom values with
keys as keys.
+ Defaults to False.
+ :type multiple_outputs: bool
+ """
+
+ template_fields = ('op_args', 'op_kwargs')
+ ui_color = '#ffefeb'
+
+ # since we won't mutate the arguments, we should just do the shallow copy
+ # there are some cases we can't deepcopy the objects(e.g protobuf).
+ shallow_copy_attrs = ('python_callable',)
+
+ @apply_defaults
+ def __init__(
+ self,
+ python_callable: Callable,
+ op_args: Tuple[Any],
+ op_kwargs: Dict[str, Any],
+ multiple_outputs: bool = False,
+ **kwargs
+ ) -> None:
+ kwargs['task_id'] = self._get_unique_task_id(kwargs['task_id'],
kwargs.get('dag', None))
+ super().__init__(**kwargs)
Review comment:
I see another issue here. Currently, if `task_id` is not provided user
will get `KeyError: 'task_id'` instead of `TypeError: __init__() missing 1
required positional argument: 'task_id' `
Also, this seems to work as expected:
```python
In [8]: class CustomOp(BaseOperator):
...: def __init__(self, a, b, *args, **kwargs):
...: super().__init__(*args, **kwargs)
...: self.task_id = "other task id"
...:
In [9]: op = CustomOp(a=1, b=2, task_id="task_id")
In [10]: op.task_id
Out[10]: 'other task id'
```
----------------------------------------------------------------
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]