Pydantic继承通用类

发布于 2025-01-17 20:12:23 字数 1132 浏览 2 评论 0原文

我是 python 和 pydantic 的新手,有打字稿背景。我想知道你是否可以继承泛型类?

在打字稿中,代码如下所示

interface GenericInterface<T> {
  value: T
}

interface ExtendsGeneric<T> extends GenericInterface<T> {
  // inherit value from GenericInterface
  otherValue: string
}

const thing: ExtendsGeneric<Number> = {
  value: 1,
  otherValue: 'string'
}

我一直在尝试的是类似的东西

#python3.9
from pydantic.generics import GenericModel
from typing import TypeVar
from typing import Generic

T = TypeVar("T", int, str)

class GenericField(GenericModel, Generic[T]):
    value: T

class ExtendsGenericField(GenericField[T]):
    otherValue: str

ExtendsGenericField[int](value=1, otherValue="other value")

我得到了 TypeError: Too muchparameters for ExtendsGenericField; 的错误实际为 1,预期为 0。 这种检查是因为在 Pydantic 文档 中明确指出“为了声明通用模型...使用 TypeVar 实例作为注释,您将在其中替换它们...”简单的解决方法是使 ExtendsGeneric 继承于 GenericModel > 并且有value 在它自己的类定义中,但我试图重用类。

是否可以从泛型类继承值?

New to python and pydantic, I come from a typescript background. I was wondering if you can inherit a generic class?

In typescript the code would be as follows

interface GenericInterface<T> {
  value: T
}

interface ExtendsGeneric<T> extends GenericInterface<T> {
  // inherit value from GenericInterface
  otherValue: string
}

const thing: ExtendsGeneric<Number> = {
  value: 1,
  otherValue: 'string'
}

What I have been trying is something along the lines of

#python3.9
from pydantic.generics import GenericModel
from typing import TypeVar
from typing import Generic

T = TypeVar("T", int, str)

class GenericField(GenericModel, Generic[T]):
    value: T

class ExtendsGenericField(GenericField[T]):
    otherValue: str

ExtendsGenericField[int](value=1, otherValue="other value")

And I get the error of TypeError: Too many parameters for ExtendsGenericField; actual 1, expected 0.
This sort of checks out because in the Pydantic docs it explicitly states "In order to declare a generic model...Use the TypeVar instances as annotations where you will want to replace them..." The easy workaround is to make ExtendsGeneric inherit from GenericModel and have value in its own class definition, but I was trying to reuse classes.

Is inheriting a value from a generic class possible?

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

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

发布评论

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

评论(3

南街女流氓 2025-01-24 20:12:23

python中的仿制药有点奇怪,问题在于扩展了Sentenericfield本身并未将其声明为通用。要解决,只需添加generic [t]作为的超级类ExtendSgenericField

from pydantic.generics import GenericModel
from typing import TypeVar
from typing import Generic

T = TypeVar("T", int, str)

class GenericField(GenericModel, Generic[T]):
    value: T

class ExtendsGenericField(GenericField[T], Generic[T]):
    otherValue: str

ExtendsGenericField[int](value=1, otherValue="other value")

Generics are a little weird in Python, and the problem is that ExtendsGenericField itself isn't declared as generic. To solve, just add Generic[T] as a super class of ExtendsGenericField:

from pydantic.generics import GenericModel
from typing import TypeVar
from typing import Generic

T = TypeVar("T", int, str)

class GenericField(GenericModel, Generic[T]):
    value: T

class ExtendsGenericField(GenericField[T], Generic[T]):
    otherValue: str

ExtendsGenericField[int](value=1, otherValue="other value")
吹泡泡o 2025-01-24 20:12:23

不确定,但是转介您的我的代码工作:)

from datetime import datetime
from pydantic import BaseModel, parse_obj_as
from enum import Enum
import json
from typing import TypeVar, Generic
from pydantic.generics import GenericModel

class TradeOverlapping(BaseModel):
    COBDate: str
    DBServer: str

class EventType(Enum):
    STH_READY= 'STH_READY'

T = TypeVar("T")
class KafkaEvent(GenericModel,Generic[T]):
    Source: str
    Version: str
    TimeStamp: datetime
    Event: EventType
    Data: T

p = KafkaEvent[TradeOverlapping].parse_obj({"Source":"TOPIC","Version":"1.0","TimeStamp":"2022-08-03 06:49:17","Event":EventType('STH_READY'),"Data":TradeOverlapping(COBDate="2023-04-05", DBServer="DB_SERVER")})
print("Producer:")
print(p.json())

print("----------------------------")

value = '{"Source":"TOPIC","Version":"1.0","TimeStamp":"2022-08-03 06:49:17","Event":"STH_READY","Data":{"COBDate":"2023-04-05","DBServer":"DB_SERVER"}}'
obj = parse_obj_as(KafkaEvent[TradeOverlapping], json.loads(value))
print("Consumer:")
print(obj)

Not sure but referring yours my code worked :)

from datetime import datetime
from pydantic import BaseModel, parse_obj_as
from enum import Enum
import json
from typing import TypeVar, Generic
from pydantic.generics import GenericModel

class TradeOverlapping(BaseModel):
    COBDate: str
    DBServer: str

class EventType(Enum):
    STH_READY= 'STH_READY'

T = TypeVar("T")
class KafkaEvent(GenericModel,Generic[T]):
    Source: str
    Version: str
    TimeStamp: datetime
    Event: EventType
    Data: T

p = KafkaEvent[TradeOverlapping].parse_obj({"Source":"TOPIC","Version":"1.0","TimeStamp":"2022-08-03 06:49:17","Event":EventType('STH_READY'),"Data":TradeOverlapping(COBDate="2023-04-05", DBServer="DB_SERVER")})
print("Producer:")
print(p.json())

print("----------------------------")

value = '{"Source":"TOPIC","Version":"1.0","TimeStamp":"2022-08-03 06:49:17","Event":"STH_READY","Data":{"COBDate":"2023-04-05","DBServer":"DB_SERVER"}}'
obj = parse_obj_as(KafkaEvent[TradeOverlapping], json.loads(value))
print("Consumer:")
print(obj)
幸福丶如此 2025-01-24 20:12:23

由于Pydantic V2(至少使用v2.6.x),因此以下作品(参考):

# at least with Python 3.9
from typing import Generic, TypeVar
from pydantic import BaseModel

T = TypeVar("T")

class BaseClass(BaseModel, Generic[T]):
    value: T

class ChildClass(BaseClass[T], Generic[T]):
    other_value: str


x = ChildClass[int](value=1, other_value="other value")

type(x)
# __main__.ChildClass[int]

Since Pydantic v2 (at least with v2.6.x), the following works (reference):

# at least with Python 3.9
from typing import Generic, TypeVar
from pydantic import BaseModel

T = TypeVar("T")

class BaseClass(BaseModel, Generic[T]):
    value: T

class ChildClass(BaseClass[T], Generic[T]):
    other_value: str


x = ChildClass[int](value=1, other_value="other value")

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