如何为继承的重载方法添加别名?

How can I alias an inherited overloaded method?

提问人:spacether 提问时间:4/27/2023 最后编辑:spacether 更新时间:4/28/2023 访问量:31

问:

如何为继承的重载方法添加别名并保留方法输入 + 输出签名?

该方法必须在继承自基类的类中具有新名称。

例如:

class BaseApi(api_client.Api):
    @typing.overload
    def _redirection(
        self,
        server_index: typing.Optional[int] = None,
        stream: bool = False,
        timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
        skip_deserialization: typing_extensions.Literal[False] = ...,
    ) -> typing.Union[
        response_3xx.ResponseFor3XX.response_cls,
        response_303.ResponseFor303.response_cls,
    ]: ...

    @typing.overload
    def _redirection(
        self,
        skip_deserialization: typing_extensions.Literal[True],
        server_index: typing.Optional[int] = None,
        stream: bool = False,
        timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
    ) -> api_client.ApiResponseWithoutDeserialization: ...

    @typing.overload
    def _redirection(
        self,
        server_index: typing.Optional[int] = None,
        stream: bool = False,
        timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
        skip_deserialization: bool = ...,
    ) -> typing.Union[
        response_3xx.ResponseFor3XX.response_cls,
        response_303.ResponseFor303.response_cls,
        api_client.ApiResponseWithoutDeserialization,
    ]: ...

    def _redirection(
        self,
        server_index: typing.Optional[int] = None,
        stream: bool = False,
        timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
        skip_deserialization: bool = False,
    ):
        # implementation

class ApiForGet(BaseApi):
    # this class is used by api classes that refer to endpoints by path and http method names

    def get(
        self,
        server_index: typing.Optional[int] = None,
        stream: bool = False,
        timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
        skip_deserialization: bool = False,
    ):
        return self._redirection(
            server_index=server_index,
            stream=stream,
            timeout=timeout,
            skip_deserialization=skip_deserialization
        )

class Redirection(BaseApi):
    # this class is used by api classes that refer to endpoints with operationId.snakeCase fn names

    def redirection(
        self,
        server_index: typing.Optional[int] = None,
        stream: bool = False,
        timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
        skip_deserialization: bool = False,
    ):
        return self._redirection(
            server_index=server_index,
            stream=stream,
            timeout=timeout,
            skip_deserialization=skip_deserialization
        )

当我打电话时,我得到但是当我使用时:我得到res = BaseApi._redirection(skip_deserialization=True)ApiResponseWithoutDeserialiationres = ApiForGet._redirection(skip_deserialization=True)ApiResponseFor3XX | ApiResponseFor3XX | ApiResponseWithoutDeserialiation

注意:我正在使用 vscode

我能想到的选项是:

  • 在 ApiForGet + Redirection 中写入重载,但这会将它们复制 2 倍
  • 使用某种装饰器魔术来重命名方法和重载方法

我该怎么做? 为什么我键入的输入没有传递到更深的呼叫站点?

这些工作,有更好的方法吗?

class ApiForGet(BaseApi):
    # this class is used by api classes that refer to endpoints by path and http method names

    get = BaseApi._redirection


class ApiForGet(BaseApi):
    pass

ApiForGet.get = ApiForGet._redirection
python-3.x 方法 运算符重载 别名 类型

评论


答:

0赞 spacether 4/28/2023 #1

这些工作:

class ApiForGet(BaseApi):
    # this class is used by api classes that refer to endpoints by path and http method names

    get = BaseApi._redirection


class ApiForGet(BaseApi):
    pass

ApiForGet.get = ApiForGet._redirection