turbaszek commented on a change in pull request #8805:
URL: https://github.com/apache/airflow/pull/8805#discussion_r422630352
##########
File path: airflow/models/baseoperator.py
##########
@@ -60,9 +60,25 @@
ScheduleInterval = Union[str, timedelta, relativedelta]
+class BaseOperatorMeta(type):
+ """
+ Base metaclass of BaseOperator.
+ """
+ def __call__(cls, *args, **kwargs):
+ """
+ Called when you call BaseOperator(). In this way we are able to
perform an action
+ after initializing an operator no matter where the
``super().__init__`` is called
+ (before or after assign of new attributes in a custom operator).
+ """
+ obj = type.__call__(cls, *args, **kwargs)
+ # Set upstream task defined by XComArgs passed to template fields of
an operator
+ obj._set_xcomargs_dependencies() # pylint: disable=protected-access
+ return obj
+
+
# pylint: disable=too-many-instance-attributes,too-many-public-methods
@functools.total_ordering
-class BaseOperator(Operator, LoggingMixin):
+class BaseOperator(Operator, LoggingMixin, metaclass=BaseOperatorMeta):
Review comment:
> Why metaclass instead of __init__?
The `_set_xcomargs_dependencies` method has to work for any operator, no
matter where someone calls `super().__init__(*args, **kwargs)`. In other words,
`_set_xcomargs_dependencies ` has to be called after initialization of an
operator.
Calling resolve in `__init__` works only when custom operator is doing:
```python
class CustomOp(BaseOperator):
template_fields = ("custom_field",)
def __init__(self, custom_field, *args, **kwargs):
self.custom_field = custom_field
super().__init__(*args, **kwargs) # resvole happens here
```
but not in case of (this is much more popular)
```python
class CustomOp(BaseOperator):
template_fields = ("custom_field",)
def __init__(self, custom_field, *args, **kwargs):
super().__init__(*args, **kwargs) # resvole happens here
self.custom_field = custom_field # this is not resolved
```
This is also checked in tests.
----------------------------------------------------------------
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]