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
Feature Request Type
[ ] Core functionality
[x] Alteration (enhancement/optimization) of existing feature(s)
[ ] New behavior
Description
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.
Upvote & Fund
We're using Polar.sh so you can upvote and help fund this issue.
We receive the funding once the issue is completed & confirmed by you.
Thank you in advance for helping prioritize & fund our backlog.
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
Feature Request Type
Description
The following code will raise the error when it's converted to a GraphQL model:
TypeError: fields cannot be resolved. Unexpected type 'typing.Literal"
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.
Upvote & Fund