|
| 1 | +""" |
| 2 | +Custom mypy plugin to solve the temporary problem with untyped decorators. |
| 3 | +
|
| 4 | +This problem appears when we try to change the return type of the function. |
| 5 | +However, currently it is impossible due to this bug: |
| 6 | +https://github.com/python/mypy/issues/3157 |
| 7 | +
|
| 8 | +This plugin is a temporary solution to the problem. |
| 9 | +It should be later replaced with the official way of doing things. |
| 10 | +
|
| 11 | +``mypy`` API docs are here: |
| 12 | +https://mypy.readthedocs.io/en/latest/extending_mypy.html |
| 13 | +
|
| 14 | +Adapted from https://github.com/dry-python/returns: contrib/mypy/decorator_plugin.py |
| 15 | +
|
| 16 | +Copyright 2016-2019 dry-python organization |
| 17 | +
|
| 18 | +Redistribution and use in source and binary forms, with or without |
| 19 | +modification, are permitted provided that the following conditions are |
| 20 | +met: |
| 21 | +
|
| 22 | +1. Redistributions of source code must retain the above copyright |
| 23 | + notice, this list of conditions and the following disclaimer. |
| 24 | +
|
| 25 | +2. Redistributions in binary form must reproduce the above copyright |
| 26 | + notice, this list of conditions and the following disclaimer in the |
| 27 | + documentation and/or other materials provided with the |
| 28 | + distribution. |
| 29 | +
|
| 30 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 31 | +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 32 | +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 33 | +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 34 | +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 35 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 36 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 37 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 38 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 39 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 40 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 41 | +""" |
| 42 | + |
| 43 | +from typing import Callable, Optional, Type |
| 44 | + |
| 45 | +from mypy.plugin import FunctionContext, Plugin |
| 46 | +from mypy.types import CallableType |
| 47 | + |
| 48 | +#: Set of full names of our decorators. |
| 49 | +_TYPED_DECORATORS = { |
| 50 | + 'morphocut.core.ReturnOutputs', |
| 51 | + 'morphocut.core.Output', |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +def _change_decorator_function_type( |
| 56 | + decorated: CallableType, |
| 57 | + decorator: CallableType, |
| 58 | +) -> CallableType: |
| 59 | + """Replaces revealed argument types by mypy with types from decorated.""" |
| 60 | + decorator.arg_types = decorated.arg_types |
| 61 | + decorator.arg_kinds = decorated.arg_kinds |
| 62 | + decorator.arg_names = decorated.arg_names |
| 63 | + return decorator |
| 64 | + |
| 65 | + |
| 66 | +def _analyze_decorator(function_ctx: FunctionContext): |
| 67 | + """Tells us what to do when one of the typed decorators is called.""" |
| 68 | + if not isinstance(function_ctx.arg_types[0][0], CallableType): |
| 69 | + print(function_ctx, "default_return_type") |
| 70 | + return function_ctx.default_return_type |
| 71 | + if not isinstance(function_ctx.default_return_type, CallableType): |
| 72 | + print(function_ctx, "default_return_type") |
| 73 | + return function_ctx.default_return_type |
| 74 | + |
| 75 | + print(function_ctx, "_change_decorator_function_type") |
| 76 | + return _change_decorator_function_type( |
| 77 | + function_ctx.arg_types[0][0], |
| 78 | + function_ctx.default_return_type, |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +class _TypedDecoratorPlugin(Plugin): |
| 83 | + def get_function_hook( # type: ignore |
| 84 | + self, fullname: str, |
| 85 | + ) -> Optional[Callable[[FunctionContext], Type]]: |
| 86 | + """ |
| 87 | + One of the specified ``mypy`` callbacks. |
| 88 | +
|
| 89 | + Runs on each function call in the source code. |
| 90 | + We are only interested in a particular subset of all functions. |
| 91 | + So, we return a function handler for them. |
| 92 | +
|
| 93 | + Otherwise, we return ``None``. |
| 94 | + """ |
| 95 | + if fullname in _TYPED_DECORATORS: |
| 96 | + return _analyze_decorator |
| 97 | + return None |
| 98 | + |
| 99 | + |
| 100 | +def plugin(version: str) -> Type[Plugin]: |
| 101 | + """Plugin's public API and entrypoint.""" |
| 102 | + return _TypedDecoratorPlugin |
0 commit comments