Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You may be interested in the type matching provided by Pydantic. It is a handy way to avoid writing `match-case` expressions.

``` >>> from pydantic import BaseModel >>> from typing import Literal, List

>>> class Chicken(BaseModel): ... n_legs: Literal[2]

>>> class Cat(BaseModel): ... n_legs: Literal[4]

>>> class Farm(BaseModel): ... animals: List[Chicken | Cat]

>>> Farm.model_validate(dict(animals=[{'n_legs': 2}, {'n_legs': 4}])) Farm(animals=[Chicken(n_legs=2), Cat(n_legs=4)]) ```



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: