Doesn't support algebraic data types, though.
FWIW I think this article mentioning them at all is sort of weird because the article doesn't propose them either, lmao
FWIW I think this article mentioning them at all is sort of weird because the article doesn't propose them either, lmao
[1]: https://stackoverflow.com/questions/16258553/how-can-i-defin...
Then again, you can just create newtype wrappers, and that's essentially the same thing as different constructors for a sum type.
class A(BaseModel):
status: Literal["success"]
class B(BaseModel):
status: Literal["error"]
class C(BaseModel, ABC):
__root__: Union[A, B] = Field(discriminator="status")
aorb = C.parse_obj(blah…).__root__
if aorb.status == "error":
# aorb will type check as B.I was looking for a solution in pydantic that is wildly relevant. Thanks, I didn’t realize that was implemented/existed.
My only objection is using ABC in your MRO. Haha, just a little bit overloaded with abstract base classes and using A, B, and C.
I got it eventually! Cool.
I don't know if type checkers can figure out if a pattern match statement is exhaustive yet though, that would be interesting.