Settings

Theme

Show HN: Pydantic++ – Utilities to Improve Pydantic

github.com

2 points by iamandoni 2 days ago · 0 comments · 2 min read

Reader

I am extremely grateful to the builders and maintainers of Pydantic. It is a really well designed library that has raised the bar of the Python ecosystem. However, I've always found two pieces of the library frustrating to work with:

1. There is no way to atomically update model fields in a type safe manner. `.model_copy(update={...})` consumes a raw dict that only gets validated at runtime. LSP / type-checking offers no help here and refactor tools never catch `.update` calls.

2. While Pydantic works extremely well for full data classes, it falls short in real world RESTful workflows. Specifically in update and upsert (PATCH / PUT) workflows, there is no way to construct a partial object. Users cannot set a subset of the fields in a type-safe manner. While there are stand alone partial pydantic solutions, they all break SOLID design principles and don't have type checking support.

As such, I created Pydantic++ to encapsulate a handful of nice utilities that build upon the core Pydantic library with full mypy type checking support. At it's v1.0.0 it contains support for:

  * `ModelUpdater` - A fluent builder pattern for updating a model with type safety.
  * `PartialBaseModel` - Type safe partial objects that respect Liskov's Substitution Principle.
  * `ModelRegistry` - Automatic model registration via module crawling.
  * `Dummy Models` - Random field instantiation for unit testing.
I built this to solve a couple of my own pain points and currently use this in 2 production FastAPI-based projects. As I release and announce `v1.0.0`, I want to open this up for others to use, contribute to, and built upon as well.

I am looking forward to hearing your use cases and other ideas for utilities to add to Pydantic++!

No comments yet.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection