Gui*_*not 2 python typing pydantic
我想实例化一个打字Union来源于两类pydantic.BaseModel直接。但是我得到了一个TypeError: Cannot instantiate typing.Union.
我见过的所有示例都声明Union为类的属性(例如此处)。
以下是我想使用的最小示例。
from pydantic import BaseModel
from typing import Union
class A(BaseModel):
a: int
class B(A):
b: int
class C(A):
c: str
MyUnion = Union[B, C, A]
mu = MyUnion(a=666, c='foo') # This command throws the TypeError
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这一目标?
这是我得到的错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-40-8163e3490185> in <module>
----> 1 MyUnion()
c:\program files\python37\lib\typing.py in __call__(self, *args, **kwargs)
668 raise TypeError(f"Type {self._name} cannot be instantiated; "
669 f"use {self._name.lower()}() instead")
--> 670 result = self.__origin__(*args, **kwargs)
671 try:
672 result.__orig_class__ = self
c:\program files\python37\lib\typing.py in __call__(self, *args, **kwds)
327
328 def __call__(self, *args, **kwds):
--> 329 raise TypeError(f"Cannot instantiate {self!r}")
330
331 def __instancecheck__(self, obj):
TypeError: Cannot instantiate typing.Union
Run Code Online (Sandbox Code Playgroud)
联盟不是这样运作的。
Union 与unionC 中的the 相同。
这意味着变量可以是类型 A 或类型 B。
例如
def f(a: Union[int, str]) -> None:
...
Run Code Online (Sandbox Code Playgroud)
这意味着a可以是 anint或 a str,它们的子类,没有别的。
小智 5
您正在寻找的是parse_obj_as:
https://pydantic-docs.helpmanual.io/usage/models/#parsing-data-into-a-specified-type
from pydantic import BaseModel, parse_obj_as
from typing import Union
class A(BaseModel):
a: int
class B(A):
b: int
class C(A):
c: str
MyUnion = Union[B, C, A]
mu = parse_obj_as(MyUnion, {"a":666, "c":'foo'})
mu
# >>> C(a=666, c='foo')
Run Code Online (Sandbox Code Playgroud)