mypy_synapse_plugin.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. """This is a mypy plugin for Synpase to deal with some of the funky typing that
  16. can crop up, e.g the cache descriptors.
  17. """
  18. from typing import Callable, Optional
  19. from mypy.plugin import MethodSigContext, Plugin
  20. from mypy.typeops import bind_self
  21. from mypy.types import CallableType
  22. class SynapsePlugin(Plugin):
  23. def get_method_signature_hook(
  24. self, fullname: str
  25. ) -> Optional[Callable[[MethodSigContext], CallableType]]:
  26. if fullname.startswith(
  27. "synapse.util.caches.descriptors._CachedFunction.__call__"
  28. ):
  29. return cached_function_method_signature
  30. return None
  31. def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
  32. """Fixes the `_CachedFunction.__call__` signature to be correct.
  33. It already has *almost* the correct signature, except:
  34. 1. the `self` argument needs to be marked as "bound"; and
  35. 2. any `cache_context` argument should be removed.
  36. """
  37. # First we mark this as a bound function signature.
  38. signature = bind_self(ctx.default_signature)
  39. # Secondly, we remove any "cache_context" args.
  40. #
  41. # Note: We should be only doing this if `cache_context=True` is set, but if
  42. # it isn't then the code will raise an exception when its called anyway, so
  43. # its not the end of the world.
  44. context_arg_index = None
  45. for idx, name in enumerate(signature.arg_names):
  46. if name == "cache_context":
  47. context_arg_index = idx
  48. break
  49. if context_arg_index:
  50. arg_types = list(signature.arg_types)
  51. arg_types.pop(context_arg_index)
  52. arg_names = list(signature.arg_names)
  53. arg_names.pop(context_arg_index)
  54. arg_kinds = list(signature.arg_kinds)
  55. arg_kinds.pop(context_arg_index)
  56. signature = signature.copy_modified(
  57. arg_types=arg_types, arg_names=arg_names, arg_kinds=arg_kinds,
  58. )
  59. return signature
  60. def plugin(version: str):
  61. # This is the entry point of the plugin, and let's us deal with the fact
  62. # that the mypy plugin interface is *not* stable by looking at the version
  63. # string.
  64. #
  65. # However, since we pin the version of mypy Synapse uses in CI, we don't
  66. # really care.
  67. return SynapsePlugin