python – 如何键入提示函数,其中返回类型取决于参数的输入类型?

假设我有一个将Python数据类型转换为Postgres数据类型的函数,如下所示:

def map_type(input):
    if isinstance(input,int):
        return MyEnum(input)
    elif isinstance(input,str):
        return MyCustomClass(str)

我可以输入提示:

def map_type(input: Union[int,str]) -> Union[MyEnum,MyCustomClass]: ...

但是,即使它是正确的,下面的代码也无法进行类型检查:

myvar = map_type('foobar')
print(myvar.property_of_my_custom_class)

完整示例(工作代码,但类型提示中的错误):

from typing import Union
from enum import Enum


class MyEnum(Enum):
    VALUE_1 = 1
    VALUE_2 = 2


class MyCustomClass:

    def __init__(self,value: str) -> None:
        self.value = value

    @property
    def myproperty(self) -> str:
        return 2 * self.value


def map_type(value: Union[int,MyCustomClass]:

    if isinstance(value,int):
        return MyEnum(value)
    elif isinstance(value,str):
        return MyCustomClass(value)
    raise TypeError('Invalid input type')


myvar1 = map_type(1)
print(myvar1.value,myvar1.name)

myvar2 = map_type('foobar')
print(myvar2.myproperty)

我知道我可以将映射拆分为两个函数,但目标是具有泛型类型映射函数.

我也在考虑使用类和多态,但是我如何键入提示最顶级的类方法呢?因为它们的输出类型取决于具体的实例类型.

最佳答案
这正是070​​00的用途.

简而言之,您执行以下操作:

from typing import overload

# ...snip...

@overload
def map_type(value: int) -> MyEnum: ...

@overload
def map_type(value: str) -> MyCustomClass: ...

def map_type(value: Union[int,MyCustomClass]:
    if isinstance(value,str):
        return MyCustomClass(value)
    raise TypeError('Invalid input type')

现在,当你执行map_type(3)时,mypy会理解返回类型是MyEnum.

在运行时,实际运行的唯一功能是最后一个 – 前两个被完全覆盖并被忽略.

相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...