techhub.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
A hub primarily for passionate technologists, but everyone is welcome

Administered by:

Server stats:

5.3K
active users

#pydantic

0 posts0 participants0 posts today

Pydantic for data validation and hydration

blog.narf.ssji.net/2025/03/30/

When using Python in any serious way, one quickly encounters Pydantic. While I’ve run into it in many frameworks, up to now, I never took the time to sit down and work out what it was doing, beyond something like “data models and validation based on type annotations”.

I have so far used dataclasses for my data models and data transfer objects (DTO). But it finally happened, and I needed finer validation on the data within my model. So I decided to take Pydantic for a spin myself, and see what it was doing for real.

tl;dr:

Pydantic _is_ data models and validation based on type annotations (amongst other things)

It’s a (almost) drop-in replacement for dataclasses

Type resolution and validation does add one order of magnitude over dataclasses, but it’s quite acceptable as a tradeoff for not having to write any bespoke validation code

Not only can it validate data, but it can also construct nested Pydantic objects, with support for polymorphism.

The pydantic.TypeAdapter exposes some lower-level mechanisms allowing to compose Pydantic behaviours as needed

[…]

Ara millor, la línia de temps "home" necessita un token vàlid però la línia de temps "public" (federada) no.

codeberg.org/spla/appy/commit/

He tingut que crear un nou model #Pydantic per els tuts de la línia de temps federada perquè no han de tenir "favourited", "reblogged", "muted", "bookmarked", "pinned" ni "filtered"

Summary card of repository spla/appy
Codeberg.orghome timeline needs a valid token but public timeline don't need it · 081cf661ccappy - a headless ActivityPub server written with Python and fastAPI.

So... I've got this "issue". Am I doing it right, or is there a better way? I'm parsing a JSON-response, converting to a dict, and then finally running them through #Pydantic model_validate. I'm finding it somewhat ... verbose to for each "level" in the dict having to define a new Pydantic BaseModel class leaving me with sometimes VERY small classes, just because I need to define a new class. Is there an "easier" way to define a pydantic class which maps to a dict on multiple levels?

#Python developerzy: mam model Pydanticowy, który jest tworzony na podstawie danych z pliku projektowego, tworzonego przez użytkownika.
Mam pole, którego użytkownik nie musi podawać, jego default value to "foo". Jaki typ pola podać?
foo_field: str - bo zawsze jest w nim string
foo_field: Optional[str] - co prawda nigdy nie będzie None, ale dzięki temu użytkownik wie, że nie musi podawać wartości w pliku
#tech #it #pydantic #rozbijanieGownaNaAtomy

For many years I have found Flask to be too basic a tool for modern development. But since I create APIs using Flask with Pydantic to validate the input data, some middlewares for parsing and Blueprint to separate the code into modules... I must admit that I am super comfortable, fast and easy to test.
#flask #python #pydantic