小编Pet*_*nev的帖子

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

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

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功能尽可能通用。现在这给出了与上面相同的警告。

python class typing callable subtype

5
推荐指数
1
解决办法
2991
查看次数

标签 统计

callable ×1

class ×1

python ×1

subtype ×1

typing ×1