Since Pydantic 1.9, pydantic has supported the use of discriminated unions as a way to force what union type Pydantic evaluates an object by. Pydantic models with discriminated unions raise an error, at least because the typing.Literal is not supported
The following code will raise the error when it's converted to a GraphQL model: TypeError: fields cannot be resolved. Unexpected type 'typing.Literal"
class Foo(BaseModel):
class_type: Literal["foo"] = "foo"
foo: str
class Bar(BaseModel):
class_type: Literal["bar"] = "bar"
bar: str
class Model(BaseModel):
foobar: Union[Foo, Bar] = Field(discriminator="class_type")
The point of this, is to tell pydantic explicitly if the foobar variable should be parsed as a Bar object or a Foo object. The default behavior without discriminated unions is to try Foo first, which will always succeed in this case.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too