如何获取可调用返回值的注释文本?

时间:2019-05-05 11:15:07

标签: python-3.x

import inspect
from typing import Union

class ApiResponse: pass

class ApiWrapper():
    def send_message(self) -> Union[ApiResponse, str]:
        print(ApiWrapper._get_caller_method_return_type())

    @classmethod
    def _get_caller_method_return_type(cls):
        caller_method_name = inspect.stack()[1][3]
        return inspect.signature(getattr(cls, caller_method_name)).return_annotation

api = ApiWrapper()
api.send_message()

打印:

typing.Union[__main__.ApiResponse, str]

但是我想看到以下结果:

Union[ApiResponse, str]

如何获取可调用对象的返回值的注释文本(即字符串Union[ApiResponse, str])?

0 个答案:

没有答案