是否可以使用Python的受约束类型Var使子类不兼容?

发布于 2025-01-25 15:25:04 字数 1253 浏览 5 评论 0 原文

根据 docs> docs ,受约束的 > typevar s应该完全匹配

但是,当使用自定义类时,该行为似乎是违反直觉的:

from dataclasses import dataclass
from typing import Generic, TypeVar

@dataclass
class PrinterConnection:
    name: str = ""


@dataclass
class WiFiPrinterConnection(PrinterConnection):
    name = "WiFi"


@dataclass
class USBPrinterConnection(PrinterConnection):
    name = "USB"


@dataclass
class USBTypeCPrinterConnection(USBPrinterConnection):
    name = "USB-C"


Connection = TypeVar("Connection", USBPrinterConnection, WiFiPrinterConnection)

@dataclass
class Printer(Generic[Connection]):
    connection: Connection

Printer(WiFiPrinterConnection())      # No Warning - As Expected
Printer(USBPrinterConnection())       # No Warning - As Expected
Printer(USBTypeCPrinterConnection())  # No Warning - NOT Expected

在此示例中, usbtypecprinternection 不是 typevar >中定义的类型之一,但没有发出警告。

我的问题是:

  1. 为什么在这里没有警告?
  2. 有没有办法允许特定的类而不是其子类?

这些示例是用Python 3.10完成的,用于检查类型。

According to the docs, constrained TypeVars should match exactly.

However, when using custom classes, the behaviour seems counterintuitive:

from dataclasses import dataclass
from typing import Generic, TypeVar

@dataclass
class PrinterConnection:
    name: str = ""


@dataclass
class WiFiPrinterConnection(PrinterConnection):
    name = "WiFi"


@dataclass
class USBPrinterConnection(PrinterConnection):
    name = "USB"


@dataclass
class USBTypeCPrinterConnection(USBPrinterConnection):
    name = "USB-C"


Connection = TypeVar("Connection", USBPrinterConnection, WiFiPrinterConnection)

@dataclass
class Printer(Generic[Connection]):
    connection: Connection

Printer(WiFiPrinterConnection())      # No Warning - As Expected
Printer(USBPrinterConnection())       # No Warning - As Expected
Printer(USBTypeCPrinterConnection())  # No Warning - NOT Expected

In this example, USBTypeCPrinterConnection is not one of the types defined in the the TypeVar and yet no warning is thrown.

My questions are:

  1. Why isn't a Warning being thrown here?
  2. Is there a way to allow for particular classes and not its subclasses?

These examples were done with Python 3.10 and Pylance for the type checking.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2025-02-01 15:25:04

受约束的 typeVar 必须解决指定类型之一,并且子类可以解散给其父母。

考虑

from typing import TypeVar

class Parent:
    pass

class Child(Parent):
    pass

T = TypeVar("T", Parent, int)  # We need at least two arguments
def foo(x: T) -> T: return x

reveal_type(foo(Child()))  # Parent

child 类型var 已解决 parent> parent

A constrained TypeVar must resolve to one of the specified types, and a subclass can resolve to its parent.

consider

from typing import TypeVar

class Parent:
    pass

class Child(Parent):
    pass

T = TypeVar("T", Parent, int)  # We need at least two arguments
def foo(x: T) -> T: return x

reveal_type(foo(Child()))  # Parent

Playground

as you can see even tho we passed a Child the type var was resolved to a Parent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文