如何让 python 类型在需要父类时将子类识别为有效类型?

Pet*_*nev 5 python class typing callable subtype

这是我需要做的一个最小的例子:

from typing import Callable, Any


class Data:
    pass


class SpecificData(Data):
    pass


class Event:
    pass


class SpecificEvent(Event):
    pass


def detect_specific_event(data: SpecificData, other_info: str) -> SpecificEvent:
    return SpecificEvent()


def run_detection(callback: Callable[[Data, Any], Event]) -> None:
    return


run_detection(detect_specific_event)
Run Code Online (Sandbox Code Playgroud)

现在我收到警告:

Expected type '(Data, Any) -> Event', got '(data: SpecificData, other_info: str) -> SpecificEvent' instead 
Run Code Online (Sandbox Code Playgroud)

对我来说,这个警告似乎没有意义,因为 SpecificData 和 SpecificEvent 分别是 Data 和 Event 的子类型,所以一切都应该没问题。有没有办法让这个工作如我所期望的那样?我的想法是能够拥有类似的东西:

class OtherSpecificData(Data):
    pass


class OtherSpecificEvent(Event):
    pass


def detect_other_event(data: OtherSpecificData, other_info: str) -> OtherSpecificEvent:
    return OtherSpecificEvent()

run_detection(detect_other_event)
Run Code Online (Sandbox Code Playgroud)

所以run_detection功能尽可能通用。现在这给出了与上面相同的警告。

Bos*_*hoi 2

参数子类型与返回子类型相反。

  • 返回值从被调用者分配给调用者。
  • 参数值从调用者分配给被调用者。

并且赋值应该比变量的预期类型更具体。例如:

data: Data = SpecificData()  # okay
data: SpecificData = Data()  # not okay
Run Code Online (Sandbox Code Playgroud)

所以你应该这样做:

from typing import Callable, Any


class Data:
    pass


class SpecificData(Data):
    pass


class Event:
    pass


class SpecificEvent(Event):
    pass


def detect_specific_event(data: Data, other_info: str) -> SpecificEvent:
    return SpecificEvent()


def run_detection(callback: Callable[[SpecificData, Any], Event]) -> None:
    return


run_detection(detect_specific_event)
Run Code Online (Sandbox Code Playgroud)