
本文探讨了在使用Mypy进行类型检查时,将多个`@runtime_checkable`协议的联合类型赋值给类型别名,并在`isinstance`检查中使用该别名时,Mypy会错误地报告“Parameterized generics cannot be used in instance checks”的问题。文章通过代码示例详细展示了该问题的表现、与正常情况的对比,并指出这实际上是Mypy的一个已知bug,而非协议本身参数化的问题,并提供了相关问题报告链接。
在Python的类型提示系统中,Protocol提供了一种结构化类型(Structural Typing)的方式,允许我们定义对象必须实现的方法或属性集合。当结合@runtime_checkable装饰器使用时,这些协议不仅能在静态类型检查时发挥作用,还可以在运行时通过isinstance()函数进行类型检查。然而,在使用Mypy进行类型检查时,将多个@runtime_checkable协议的联合类型(Union Type)赋值给类型别名,并在isinstance检查中使用该别名时,可能会遇到一个令人困惑的错误。
typing.Protocol允许定义一个接口,任何符合该接口的对象都可被视为实现了该协议,而无需显式继承。例如,SupportsInt协议要求对象实现__int__方法。@runtime_checkable装饰器则进一步增强了协议的能力,使得我们可以在运行时使用isinstance()或issubclass()来检查一个对象是否符合某个协议。
考虑以下协议定义:
from typing import Protocol, runtime_checkable, SupportsIndex, SupportsInt
@runtime_checkable
class SupportsTrunc(Protocol):
"""
Protocol for objects that can be truncated to an integer.
Corresponds to the __trunc__ method.
"""
def __trunc__(self) -> int:
...
# SupportsInt and SupportsIndex are built-in runtime_checkable Protocols
# defined in typing or _typeshed.这里定义了一个SupportsTrunc协议,并引入了SupportsInt和SupportsIndex,它们都是Python标准库中已定义的@runtime_checkable协议。
当尝试创建一个包含这些协议的联合类型别名,并在isinstance检查中使用它时,Mypy会抛出错误。
_ConvertibleToInt = SupportsInt | SupportsIndex | SupportsTrunc
def process_int_convertible(o: object) -> None:
if isinstance(o, _ConvertibleToInt):
# 错误: Parameterized generics cannot be used with class or instance checks
# 错误: Argument 2 to "isinstance" has incompatible type "<typing special form>"; expected "_ClassInfo"
print(f"Object {o} is convertible to an integer.")
else:
print(f"Object {o} is not directly convertible to an integer.")
# 示例调用
process_int_convertible(10)
process_int_convertible(3.14)
process_int_convertible("hello")Mypy报告的错误信息“Parameterized generics cannot be used with class or instance checks”似乎暗示这些协议是参数化的泛型,但实际上它们并非如此。SupportsInt、SupportsIndex和SupportsTrunc都是非泛型协议。
为了更清晰地理解问题的触发条件,我们可以通过几个最小示例进行对比:
from typing import SupportsIndex, SupportsInt
_ConvertibleToInt = SupportsInt | SupportsIndex
def check_conversion(o: object) -> None:
if isinstance(o, _ConvertibleToInt): # Mypy 错误
print("Object is convertible.")此示例与前一个类似,即使只包含两个协议,只要它们通过类型别名构成联合类型,Mypy就会报错。
如果直接在isinstance中使用联合类型,Mypy则不会报错。
from typing import SupportsIndex, SupportsInt
def check_conversion_direct(o: object) -> None:
if isinstance(o, SupportsInt | SupportsIndex): # Mypy 正常
print("Object is convertible.")这表明问题并非出在联合类型本身,而是与“联合类型作为类型别名”这一组合有关。
如果类型别名只指向单个协议,Mypy同样不会报错。
from typing import SupportsInt
_ConvertibleToInt = SupportsInt
def check_single_protocol(o: object) -> None:
if isinstance(o, _ConvertibleToInt): # Mypy 正常
print("Object supports int conversion.")这进一步确认了问题的根源在于“联合类型”和“类型别名”的结合使用。
即使联合类型别名中包含的是同一个协议的重复,Mypy仍然会报错。
from typing import SupportsInt
_ConvertibleToInt = SupportsInt | SupportsInt
def check_repeated_protocol(o: object) -> None:
if isinstance(o, _ConvertibleToInt): # Mypy 错误
print("Object supports int conversion.")这排除了协议类型多样性导致问题的可能性,进一步指向了Mypy在处理Union类型别名时的特定逻辑缺陷。
根据上述分析,Mypy在处理isinstance检查时,当第二个参数是一个由多个@runtime_checkable协议组成的联合类型别名时,会错误地报告“Parameterized generics cannot be used in instance checks”。这并不是因为协议本身是参数化的泛型,而是Mypy内部处理这种特定类型别名组合时的bug。
这个行为已被社区确认为Mypy的一个已知bug,并在Mypy的GitHub仓库中有所报告(例如:mypy/#16707)。
在Mypy修复此问题之前,您可以考虑以下临时解决方案:
if isinstance(o, SupportsInt | SupportsIndex | SupportsTrunc):
# ...if isinstance(o, SupportsInt) or isinstance(o, SupportsIndex) or isinstance(o, SupportsTrunc):
# ...然而,这种方法失去了使用联合类型别名在类型检查时的简洁性。
尽管Python的类型提示系统提供了强大的工具来增强代码的可读性和健壮性,但在其实现工具(如Mypy)中仍可能存在一些待解决的问题。遇到类似“Parameterized generics cannot be used with class or instance checks”的错误时,如果确认所使用的类型并非泛型,那么很可能遇到了Mypy的内部限制或bug。了解这些已知问题,有助于开发者在编写类型安全代码时做出更明智的选择,或在遇到错误时能够快速定位并寻找合适的解决方案或临时规避方法。
以上就是深入理解Mypy中isinstance与Protocol联合类型别名的陷阱的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号