|
15 | 15 | # specific language governing permissions and limitations
|
16 | 16 | # under the License.
|
17 | 17 |
|
18 |
| -from typing import Callable, Dict, Iterable, List, Optional, Union |
| 18 | +from typing import TYPE_CHECKING |
19 | 19 |
|
20 |
| -from airflow.decorators.python import python_task |
21 |
| -from airflow.decorators.python_virtualenv import _virtualenv_task |
| 20 | +from airflow.decorators.python import PythonDecoratorMixin, python_task # noqa |
| 21 | +from airflow.decorators.python_virtualenv import PythonVirtualenvDecoratorMixin |
22 | 22 | from airflow.decorators.task_group import task_group # noqa
|
23 | 23 | from airflow.models.dag import dag # noqa
|
| 24 | +from airflow.providers_manager import ProvidersManager |
24 | 25 |
|
25 | 26 |
|
26 |
| -class _TaskDecorator: |
27 |
| - def __call__( |
28 |
| - self, python_callable: Optional[Callable] = None, multiple_outputs: Optional[bool] = None, **kwargs |
29 |
| - ): |
30 |
| - """ |
31 |
| - Python operator decorator. Wraps a function into an Airflow operator. |
32 |
| - Accepts kwargs for operator kwarg. This decorator can be reused in a single DAG. |
| 27 | +class _TaskDecorator(PythonDecoratorMixin, PythonVirtualenvDecoratorMixin): |
| 28 | + def __getattr__(self, name): |
| 29 | + if name.startswith("__"): |
| 30 | + raise AttributeError(f'{type(self).__name__} has no attribute {name!r}') |
| 31 | + decorators = ProvidersManager().taskflow_decorators |
| 32 | + if name not in decorators: |
| 33 | + raise AttributeError(f"task decorator {name!r} not found") |
| 34 | + return decorators[name] |
33 | 35 |
|
34 |
| - :param python_callable: Function to decorate |
35 |
| - :type python_callable: Optional[Callable] |
36 |
| - :param multiple_outputs: if set, function return value will be |
37 |
| - unrolled to multiple XCom values. List/Tuples will unroll to xcom values |
38 |
| - with index as key. Dict will unroll to xcom values with keys as XCom keys. |
39 |
| - Defaults to False. |
40 |
| - :type multiple_outputs: bool |
41 |
| - """ |
42 |
| - return self.python(python_callable=python_callable, multiple_outputs=multiple_outputs, **kwargs) |
43 | 36 |
|
44 |
| - @staticmethod |
45 |
| - def python(python_callable: Optional[Callable] = None, multiple_outputs: Optional[bool] = None, **kwargs): |
46 |
| - """ |
47 |
| - Python operator decorator. Wraps a function into an Airflow operator. |
48 |
| - Accepts kwargs for operator kwarg. This decorator can be reused in a single DAG. |
| 37 | +# [START mixin_for_autocomplete] |
| 38 | +if TYPE_CHECKING: |
| 39 | + try: |
| 40 | + from airflow.providers.docker.decorators.docker import DockerDecoratorMixin |
49 | 41 |
|
50 |
| - :param python_callable: Function to decorate |
51 |
| - :type python_callable: Optional[Callable] |
52 |
| - :param multiple_outputs: if set, function return value will be |
53 |
| - unrolled to multiple XCom values. List/Tuples will unroll to xcom values |
54 |
| - with index as key. Dict will unroll to xcom values with keys as XCom keys. |
55 |
| - Defaults to False. |
56 |
| - :type multiple_outputs: bool |
57 |
| - """ |
58 |
| - return python_task(python_callable=python_callable, multiple_outputs=multiple_outputs, **kwargs) |
59 |
| - |
60 |
| - @staticmethod |
61 |
| - def virtualenv( |
62 |
| - python_callable: Optional[Callable] = None, |
63 |
| - multiple_outputs: Optional[bool] = None, |
64 |
| - requirements: Optional[Iterable[str]] = None, |
65 |
| - python_version: Optional[Union[str, int, float]] = None, |
66 |
| - use_dill: bool = False, |
67 |
| - system_site_packages: bool = True, |
68 |
| - string_args: Optional[Iterable[str]] = None, |
69 |
| - templates_dict: Optional[Dict] = None, |
70 |
| - templates_exts: Optional[List[str]] = None, |
71 |
| - **kwargs, |
72 |
| - ): |
73 |
| - """ |
74 |
| - Allows one to run a function in a virtualenv that is |
75 |
| - created and destroyed automatically (with certain caveats). |
76 |
| -
|
77 |
| - The function must be defined using def, and not be |
78 |
| - part of a class. All imports must happen inside the function |
79 |
| - and no variables outside of the scope may be referenced. A global scope |
80 |
| - variable named virtualenv_string_args will be available (populated by |
81 |
| - string_args). In addition, one can pass stuff through op_args and op_kwargs, and one |
82 |
| - can use a return value. |
83 |
| - Note that if your virtualenv runs in a different Python major version than Airflow, |
84 |
| - you cannot use return values, op_args, op_kwargs, or use any macros that are being provided to |
85 |
| - Airflow through plugins. You can use string_args though. |
86 |
| -
|
87 |
| - .. seealso:: |
88 |
| - For more information on how to use this operator, take a look at the guide: |
89 |
| - :ref:`howto/operator:PythonVirtualenvOperator` |
90 |
| -
|
91 |
| - :param python_callable: A python function with no references to outside variables, |
92 |
| - defined with def, which will be run in a virtualenv |
93 |
| - :type python_callable: function |
94 |
| - :param multiple_outputs: if set, function return value will be |
95 |
| - unrolled to multiple XCom values. List/Tuples will unroll to xcom values |
96 |
| - with index as key. Dict will unroll to xcom values with keys as XCom keys. |
97 |
| - Defaults to False. |
98 |
| - :type multiple_outputs: bool |
99 |
| - :param requirements: A list of requirements as specified in a pip install command |
100 |
| - :type requirements: list[str] |
101 |
| - :param python_version: The Python version to run the virtualenv with. Note that |
102 |
| - both 2 and 2.7 are acceptable forms. |
103 |
| - :type python_version: Optional[Union[str, int, float]] |
104 |
| - :param use_dill: Whether to use dill to serialize |
105 |
| - the args and result (pickle is default). This allow more complex types |
106 |
| - but requires you to include dill in your requirements. |
107 |
| - :type use_dill: bool |
108 |
| - :param system_site_packages: Whether to include |
109 |
| - system_site_packages in your virtualenv. |
110 |
| - See virtualenv documentation for more information. |
111 |
| - :type system_site_packages: bool |
112 |
| - :param op_args: A list of positional arguments to pass to python_callable. |
113 |
| - :type op_args: list |
114 |
| - :param op_kwargs: A dict of keyword arguments to pass to python_callable. |
115 |
| - :type op_kwargs: dict |
116 |
| - :param string_args: Strings that are present in the global var virtualenv_string_args, |
117 |
| - available to python_callable at runtime as a list[str]. Note that args are split |
118 |
| - by newline. |
119 |
| - :type string_args: list[str] |
120 |
| - :param templates_dict: a dictionary where the values are templates that |
121 |
| - will get templated by the Airflow engine sometime between |
122 |
| - ``__init__`` and ``execute`` takes place and are made available |
123 |
| - in your callable's context after the template has been applied |
124 |
| - :type templates_dict: dict of str |
125 |
| - :param templates_exts: a list of file extensions to resolve while |
126 |
| - processing templated fields, for examples ``['.sql', '.hql']`` |
127 |
| - :type templates_exts: list[str] |
128 |
| - """ |
129 |
| - return _virtualenv_task( |
130 |
| - python_callable=python_callable, |
131 |
| - multiple_outputs=multiple_outputs, |
132 |
| - requirements=requirements, |
133 |
| - python_version=python_version, |
134 |
| - use_dill=use_dill, |
135 |
| - system_site_packages=system_site_packages, |
136 |
| - string_args=string_args, |
137 |
| - templates_dict=templates_dict, |
138 |
| - templates_exts=templates_exts, |
139 |
| - **kwargs, |
140 |
| - ) |
| 42 | + class _DockerTask(_TaskDecorator, DockerDecoratorMixin): |
| 43 | + pass |
141 | 44 |
|
| 45 | + _TaskDecorator = _DockerTask |
| 46 | + except ImportError: |
| 47 | + pass |
| 48 | +# [END mixin_for_autocomplete] |
142 | 49 |
|
143 | 50 | task = _TaskDecorator()
|
0 commit comments