Chaho12 opened a new issue #20797:
URL: https://github.com/apache/airflow/issues/20797


   ### Description
   
   [Python Operator 
Execute](https://github.com/apache/airflow/blob/2.2.3/airflow/operators/python.py#L168-L176)
 method updates context like code below, which works as description mentions : 
`:param op_kwargs: a dictionary of keyword arguments that will get unpacked in 
your function`.
   ```python
       def execute(self, context: Dict):
           context.update(self.op_kwargs)
           context['templates_dict'] = self.templates_dict
   
           self.op_kwargs = determine_kwargs(self.python_callable, 
self.op_args, context)
   
           return_value = self.execute_callable()
           self.log.info("Done. Returned value was: %s", return_value)
           return return_value
    ```
   
   However, I think that this description is ironic to current situation where 
`_run_finished_callback` uses context and does not contain such additional 
kwargs set via `op_kwargs`.
   
https://github.com/apache/airflow/blob/2.2.3/airflow/models/taskinstance.py#L1562-L1594
   
   ### Use case/motivation
   
   Because python operator is based on base operator, user can and frequently 
add callbacks such as `on_retry_callback or  on_success_callback` for user's 
sake. However, callback calls [template 
context](https://github.com/apache/airflow/blob/2.2.3/airflow/models/taskinstance.py#L1572)
 which does not contain kwargs set when using python operator, but only 
[context](https://github.com/apache/airflow/blob/2.2.3/airflow/models/taskinstance.py#L1777).
 
   
   I find this not logical because description implies that options set via 
op_kwargs is applied to both python_callable and callbacks. At airflow 1, there 
was at least the deprecated `provide_context` parameter option where user can 
pass in an additional set of keyword arguments: one for each of the Jinja 
template variables and a templates_dict argument. Also, there was description 
that explicitly say that op_args and op_kwargs is meant only for python 
callable. `Use the op_args and op_kwargs arguments to pass additional arguments 
to the Python callable.`
   
   Therefore, I suggest 1 of 2 suggestions.
   1. Clarify description so that op_kwargs is only applied to 
`python_callable` parameter
        - user has to make custom functions to add custom args to context and 
send to callbacks.
   2. callbacks consider op_kwargs options such as 
[determine_kwargs](https://github.com/apache/airflow/blob/2.2.3/airflow/utils/operator_helpers.py#L91-L118)
 function.
   
   ### Related issues
   
   _No response_
   
   ### Are you willing to submit a PR?
   
   - [X] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [X] I agree to follow this project's [Code of 
Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
   


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to