繁体   English   中英

Python 子 class 中方法返回的覆盖类型提示,没有重新定义方法签名

[英]Python overriding type hint on a method's return in child class, without redefining method signature

我有一个基础 class 在方法返回时带有float类型提示。

在子 class 中,在不重新定义签名的情况下,我可以以某种方式将方法返回的类型提示更新为int吗?


示例代码

#!/usr/bin/env python3.6


class SomeClass:
    """This class's some_method will return float."""

    RET_TYPE = float

    def some_method(self, some_input: str) -> float:
        return self.RET_TYPE(some_input)


class SomeChildClass(SomeClass):
    """This class's some_method will return int."""

    RET_TYPE = int


if __name__ == "__main__":
    ret: int = SomeChildClass().some_method("42"). # 
    ret2: float = SomeChildClass().some_method("42")

我的 IDE 抱怨类型不匹配:

pycharm 预期类型浮点数

发生这种情况是因为我的 IDE 仍在使用SomeClass.some_method的类型提示。


研究

我认为解决方案可能是使用 generics,但我不确定是否有更简单的方法。

Python:如何覆盖子类中实例属性的类型提示?

建议可能使用实例变量注释,但我不确定如何为返回类型执行此操作。

以下代码在 PyCharm 上运行良好。 我添加了complex的案例以使其更清晰。

我基本上将该方法提取到通用 class 中,然后将其用作每个子类的 mixin。 请格外小心使用,因为它似乎相当不标准。

from typing import ClassVar, Generic, TypeVar, Callable


S = TypeVar('S', bound=complex)


class SomeMethodImplementor(Generic[S]):
    RET_TYPE: ClassVar[Callable]

    def some_method(self, some_input: str) -> S:
        return self.__class__.RET_TYPE(some_input)


class SomeClass(SomeMethodImplementor[complex]):
    RET_TYPE = complex


class SomeChildClass(SomeClass, SomeMethodImplementor[float]):
    RET_TYPE = float


class OtherChildClass(SomeChildClass, SomeMethodImplementor[int]):
    RET_TYPE = int


if __name__ == "__main__":
    ret: complex = SomeClass().some_method("42")
    ret2: float = SomeChildClass().some_method("42")
    ret3: int = OtherChildClass().some_method("42")
    print(ret, type(ret), ret2, type(ret2), ret3, type(ret3))

例如,如果您将ret2: float更改为ret2: int ,它将正确显示类型错误。

可悲的是, mypy在这种情况下确实显示错误(版本 0.770),

otherhint.py:20: error: Incompatible types in assignment (expression has type "Type[float]", base class "SomeClass" defined the type as "Type[complex]")
otherhint.py:24: error: Incompatible types in assignment (expression has type "Type[int]", base class "SomeClass" defined the type as "Type[complex]")
otherhint.py:29: error: Incompatible types in assignment (expression has type "complex", variable has type "float")
otherhint.py:30: error: Incompatible types in assignment (expression has type "complex", variable has type "int")

第一个错误可以通过编写来“修复”

    RET_TYPE: ClassVar[Callable] = int

对于每个子类。 现在,错误减少到

otherhint.py:29: error: Incompatible types in assignment (expression has type "complex", variable has type "float")
otherhint.py:30: error: Incompatible types in assignment (expression has type "complex", variable has type "int")

这与我们想要的恰恰相反,但如果你只关心 PyCharm,那也没关系。

你可以使用类似的东西:

from typing import TypeVar, Generic


T = TypeVar('T', float, int) # types you support


class SomeClass(Generic[T]):
    """This class's some_method will return float."""

    RET_TYPE = float

    def some_method(self, some_input: str) -> T:
        return self.RET_TYPE(some_input)


class SomeChildClass(SomeClass[int]):
    """This class's some_method will return int."""

    RET_TYPE = int


if __name__ == "__main__":
    ret: int = SomeChildClass().some_method("42")
    ret2: float = SomeChildClass().some_method("42")

但是有一个问题。 那我不知道怎么解决。 对于 SomeChildClass 方法 some_method IDE 将显示通用提示。 至少 pycharm(我想你是这个)不会将其显示为错误。

好的,所以我可以尝试并结合@AntonPomieshcheko 和@KevinLanguasco 的答案来提出一个解决方案,其中:

  • 我的 IDE (PyCharm) 可以正确推断返回类型
  • 如果类型不匹配, mypy报告
  • 运行时不会出错,即使类型提示指示不匹配

这正是我想要的行为。 非常感谢大家:)

#!/usr/bin/env python3

from typing import TypeVar, Generic, ClassVar, Callable


T = TypeVar("T", float, int)  # types supported


class SomeBaseClass(Generic[T]):
    """This base class's some_method will return a supported type."""

    RET_TYPE: ClassVar[Callable]

    def some_method(self, some_input: str) -> T:
        return self.RET_TYPE(some_input)


class SomeChildClass1(SomeBaseClass[float]):
    """This child class's some_method will return a float."""

    RET_TYPE = float


class SomeChildClass2(SomeBaseClass[int]):
    """This child class's some_method will return an int."""

    RET_TYPE = int


class SomeChildClass3(SomeBaseClass[complex]):
    """This child class's some_method will return a complex."""

    RET_TYPE = complex


if __name__ == "__main__":
    some_class_1_ret: float = SomeChildClass1().some_method("42")
    some_class_2_ret: int = SomeChildClass2().some_method("42")

    # PyCharm can infer this return is a complex.  However, running mypy on
    # this will report (this is desirable to me):
    # error: Value of type variable "T" of "SomeBaseClass" cannot be "complex"
    some_class_3_ret = SomeChildClass3().some_method("42")

    print(
        f"some_class_1_ret = {some_class_1_ret} of type {type(some_class_1_ret)}\n"
        f"some_class_2_ret = {some_class_2_ret} of type {type(some_class_2_ret)}\n"
        f"some_class_3_ret = {some_class_3_ret} of type {type(some_class_3_ret)}\n"
    )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM