Python 类型提示中的类型提升

Python 类型提示中的类型提升

本文探讨了如何在 python 中使用类型提示精确地表示类型提升,例如,一个函数根据输入参数的类型返回 `int` 或 `Float`。虽然可以使用 `@overload` 实现,但这种方法比较繁琐。本文介绍了一种使用 `TypeVar` 和 `Generic` 的方法,以实现更简洁的类型提示,并提供示例代码和注意事项。 在 Python 中,类型提示是一个强大的工具,可以提高代码的可读性和可维护性。然而,在处理类型提升等复杂情况时,类型提示可能会变得比较棘手。本文将介绍一种使用 `TypeVar` 和 `Generic` 的方法,以解决这个问题。 ### 使用 `TypeVar` 和 `Generic` 实现类型提升 假设我们有一个函数 `mul`,它接受两个参数 `a` 和 `b`,这两个参数可以是 `int` 或 `float` 类型。如果 `a` 和 `b` 都是 `int` 类型,则函数返回 `int` 类型;否则,函数返回 `float` 类型。 使用 `@overload` 可以实现这个功能,如下所示: “`python from typing import overload @overload def mul(a: int, b: int) -> int: … @overload def mul(a: float, b: int | float) -> float: … @overload def mul(a: int | float, b: float) -> float: … def mul(a, b): return a * b

但是,这种方法比较繁琐,需要为每种可能的输入类型组合都定义一个重载。

我们可以使用 TypeVar 和 Generic 来实现更简洁的类型提示。首先,我们定义两个 TypeVar,分别表示 a 和 b 的类型:

from typing import TypeVar, Generic  T1 = TypeVar('T1', str, float) T2 = TypeVar('T2', str, float)

然后,我们定义一个 Generic 类 Promoted,它接受两个类型参数 T1 和 T2,并根据这两个类型参数返回相应的类型:

class Promoted(Generic[T1, T2]):     def __class_getitem__(cls, types):         t1, t2 = types         if t1 == str or t2 == str:             return str         return float

最后,我们可以使用 Promoted 类来注释 mul 函数的返回类型:

def method(a: T1, b: T2) -> Promoted[T1, T2]:     if isinstance(a, str) or isinstance(b, str):         return f"{a} {b}"     else:         return a*b

这样,mul 函数的返回类型将根据输入参数的类型自动推断。

示例代码

以下是一个完整的示例代码:

from typing import TypeVar, Generic  T1 = TypeVar('T1', str, float) T2 = TypeVar('T2', str, float)  class Promoted(Generic[T1, T2]):     def __class_getitem__(cls, types):         t1, t2 = types         if t1 == str or t2 == str:             return str         return float  def method(a: T1, b: T2) -> Promoted[T1, T2]:     if isinstance(a, str) or isinstance(b, str):         return f"{a} {b}"     else:         return a*b  result1 = method("Hello", "World")  # should be inferred as str result2 = method(3.0, 4.0)  # should be inferred as float result3 = method("Price", 5.0)  # should be inferred as str  print(f"{result1=}, {type(result1)=}") print(f"{result2=}, {type(result2)=}") print(f"{result3=}, {type(result3)=}")

注意: 目前,一些 ide 可能无法正确推断 Promoted 类的类型。在这种情况下,类型提示可能会显示为 “Promoted” 而不是 str 或 float。但是,这并不影响代码的实际行为。

总结

本文介绍了一种使用 TypeVar 和 Generic 的方法,以在 Python 中实现类型提升的类型提示。这种方法比使用 @overload 更加简洁,并且可以自动推断函数的返回类型。虽然目前一些 IDE 可能无法完全支持这种方法,但它仍然是一种非常有用的技术,可以提高代码的可读性和可维护性。


© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享