|
| 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 | +import copy |
| 20 | +import functools |
| 21 | +from typing import List |
| 22 | + |
| 23 | +from mypy.nodes import ARG_NAMED_OPT # pylint: disable=no-name-in-module |
| 24 | +from mypy.plugin import FunctionContext, Plugin # pylint: disable=no-name-in-module |
| 25 | +from mypy.types import CallableType, NoneType, UnionType # pylint: disable=no-name-in-module |
| 26 | + |
| 27 | +TYPED_DECORATORS = { |
| 28 | + "fallback_to_default_project_id of GoogleBaseHook": ["project_id"], |
| 29 | + "airflow.providers.google.cloud.hooks.dataflow._fallback_to_project_id_from_variables": ["project_id"], |
| 30 | + "provide_gcp_credential_file of GoogleBaseHook": [], |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +class TypedDecoratorPlugin(Plugin): |
| 35 | + """Mypy plugin for typed decorators.""" |
| 36 | + def get_function_hook(self, fullname: str): |
| 37 | + """Check for known typed decorators by name.""" |
| 38 | + if fullname in TYPED_DECORATORS: |
| 39 | + return functools.partial( |
| 40 | + _analyze_decorator, |
| 41 | + provided_arguments=TYPED_DECORATORS[fullname], |
| 42 | + ) |
| 43 | + return None |
| 44 | + |
| 45 | + |
| 46 | +def _analyze_decorator(function_ctx: FunctionContext, provided_arguments: List[str]): |
| 47 | + if not isinstance(function_ctx.arg_types[0][0], CallableType): |
| 48 | + return function_ctx.default_return_type |
| 49 | + if not isinstance(function_ctx.default_return_type, CallableType): |
| 50 | + return function_ctx.default_return_type |
| 51 | + return _change_decorator_function_type( |
| 52 | + function_ctx.arg_types[0][0], |
| 53 | + function_ctx.default_return_type, |
| 54 | + provided_arguments, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def _change_decorator_function_type( |
| 59 | + decorated: CallableType, |
| 60 | + decorator: CallableType, |
| 61 | + provided_arguments: List[str], |
| 62 | +) -> CallableType: |
| 63 | + decorator.arg_kinds = decorated.arg_kinds |
| 64 | + decorator.arg_names = decorated.arg_names |
| 65 | + |
| 66 | + # Mark provided arguments as optional |
| 67 | + decorator.arg_types = copy.copy(decorated.arg_types) |
| 68 | + for argument in provided_arguments: |
| 69 | + index = decorated.arg_names.index(argument) |
| 70 | + decorated_type = decorated.arg_types[index] |
| 71 | + decorator.arg_types[index] = UnionType.make_union([decorated_type, NoneType()]) |
| 72 | + decorated.arg_kinds[index] = ARG_NAMED_OPT |
| 73 | + |
| 74 | + return decorator |
| 75 | + |
| 76 | + |
| 77 | +def plugin(version: str): # pylint: disable=unused-argument |
| 78 | + """Mypy plugin entrypoint.""" |
| 79 | + return TypedDecoratorPlugin |
0 commit comments