|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2020 The Matrix.org Foundation C.I.C. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""This is a mypy plugin for Synpase to deal with some of the funky typing that |
| 17 | +can crop up, e.g the cache descriptors. |
| 18 | +""" |
| 19 | + |
| 20 | +from typing import Callable, Optional |
| 21 | + |
| 22 | +from mypy.plugin import MethodSigContext, Plugin |
| 23 | +from mypy.typeops import bind_self |
| 24 | +from mypy.types import CallableType |
| 25 | + |
| 26 | + |
| 27 | +class SynapsePlugin(Plugin): |
| 28 | + def get_method_signature_hook( |
| 29 | + self, fullname: str |
| 30 | + ) -> Optional[Callable[[MethodSigContext], CallableType]]: |
| 31 | + if fullname.startswith( |
| 32 | + "synapse.util.caches.descriptors._CachedFunction.__call__" |
| 33 | + ): |
| 34 | + return cached_function_method_signature |
| 35 | + return None |
| 36 | + |
| 37 | + |
| 38 | +def cached_function_method_signature(ctx: MethodSigContext) -> CallableType: |
| 39 | + """Fixes the `_CachedFunction.__call__` signature to be correct. |
| 40 | +
|
| 41 | + It already has *almost* the correct signature, except: |
| 42 | +
|
| 43 | + 1. the `self` argument needs to be marked as "bound"; and |
| 44 | + 2. any `cache_context` argument should be removed. |
| 45 | + """ |
| 46 | + |
| 47 | + # First we mark this as a bound function signature. |
| 48 | + signature = bind_self(ctx.default_signature) |
| 49 | + |
| 50 | + # Secondly, we remove any "cache_context" args. |
| 51 | + # |
| 52 | + # Note: We should be only doing this if `cache_context=True` is set, but if |
| 53 | + # it isn't then the code will raise an exception when its called anyway, so |
| 54 | + # its not the end of the world. |
| 55 | + context_arg_index = None |
| 56 | + for idx, name in enumerate(signature.arg_names): |
| 57 | + if name == "cache_context": |
| 58 | + context_arg_index = idx |
| 59 | + break |
| 60 | + |
| 61 | + if context_arg_index: |
| 62 | + arg_types = list(signature.arg_types) |
| 63 | + arg_types.pop(context_arg_index) |
| 64 | + |
| 65 | + arg_names = list(signature.arg_names) |
| 66 | + arg_names.pop(context_arg_index) |
| 67 | + |
| 68 | + arg_kinds = list(signature.arg_kinds) |
| 69 | + arg_kinds.pop(context_arg_index) |
| 70 | + |
| 71 | + signature = signature.copy_modified( |
| 72 | + arg_types=arg_types, arg_names=arg_names, arg_kinds=arg_kinds, |
| 73 | + ) |
| 74 | + |
| 75 | + return signature |
| 76 | + |
| 77 | + |
| 78 | +def plugin(version: str): |
| 79 | + # This is the entry point of the plugin, and let's us deal with the fact |
| 80 | + # that the mypy plugin interface is *not* stable by looking at the version |
| 81 | + # string. |
| 82 | + # |
| 83 | + # However, since we pin the version of mypy Synapse uses in CI, we don't |
| 84 | + # really care. |
| 85 | + return SynapsePlugin |
0 commit comments