|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from typing import Callable |
| 22 | + |
| 23 | +from mypy.plugin import AttributeContext, MethodContext, Plugin |
| 24 | +from mypy.types import AnyType, Type, TypeOfAny |
| 25 | + |
| 26 | +OUTPUT_PROPERTIES = { |
| 27 | + "airflow.models.baseoperator.BaseOperator.output", |
| 28 | + "airflow.models.mappedoperator.MappedOperator.output", |
| 29 | +} |
| 30 | + |
| 31 | +TASK_CALL_FUNCTIONS = { |
| 32 | + "airflow.decorators.base.Task.__call__", |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +class OperatorOutputPlugin(Plugin): |
| 37 | + """Plugin to convert XComArg to the runtime type. |
| 38 | +
|
| 39 | + This allows us to pass an *XComArg* to a downstream task, such as:: |
| 40 | +
|
| 41 | + @task |
| 42 | + def f(a: str) -> int: |
| 43 | + return len(a) |
| 44 | +
|
| 45 | + f(op.output) # "op" is an operator instance. |
| 46 | + f(g()) # "g" is a taskflow task. |
| 47 | +
|
| 48 | + where the *a* argument of ``f`` should accept a *str* at runtime, but can be |
| 49 | + provided with an *XComArg* in the DAG. |
| 50 | +
|
| 51 | + In the long run, it is probably a good idea to make *XComArg* a generic that |
| 52 | + carries information about the task's return type, and build the entire XCom |
| 53 | + mechanism into the type checker. But Python's type system is still limiting |
| 54 | + in this regard now, and (using the above example) we yet to have a good way |
| 55 | + to convert ``f``'s argument list from ``[str]`` to ``[XComArg[str] | str]``. |
| 56 | + Perhaps *ParamSpec* will be extended enough one day to accommodate this. |
| 57 | + """ |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def _treat_as_any(context: AttributeContext | MethodContext) -> Type: |
| 61 | + """Pretend *XComArg* is actually *typing.Any*.""" |
| 62 | + return AnyType(TypeOfAny.special_form, line=context.context.line, column=context.context.column) |
| 63 | + |
| 64 | + def get_attribute_hook(self, fullname: str) -> Callable[[AttributeContext], Type] | None: |
| 65 | + if fullname not in OUTPUT_PROPERTIES: |
| 66 | + return None |
| 67 | + return self._treat_as_any |
| 68 | + |
| 69 | + def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | None: |
| 70 | + if fullname not in TASK_CALL_FUNCTIONS: |
| 71 | + return None |
| 72 | + return self._treat_as_any |
| 73 | + |
| 74 | + |
| 75 | +def plugin(version: str): |
| 76 | + return OperatorOutputPlugin |
0 commit comments