TGViewer
ITGram ITGram @itgram_channel · 390 subscribers
Post #597 761
msgspec is a serialization and validation library for Python with schemas defined declaratively with type annotations (like dataclasses). Think of it as a pydantic alternative. And it has quite a few advantages:

1. Performance. In their benchmarks they claim to tear apart everything else, especially pydantic. And while Pydantic 2.0 with the core implemented on Rust is on horizon, it's not clear when it will be finished, and I still expect msgspec to stay ahead.

2. No implicit type conversion. If the filed type is specified as int and you pass in a string, pydantic (by default) will try its best to convert the passed value to the int. While it is useful for some cases, like parsing URL or GET query parameters, most often you'll use it for interacting with other APIs and providing your own API, and there types are better to be strict.

3. Clean and consistent API. Pydantic has a relatively long history, and it has accumulated a fair share of bad decisions. Again, it's something that the author wants to fix in pydantic 2.0, which isn't clear when it will come out. Msgspec has a clean and functional API that doesn't mess with your classes.

4. Out-of-the-box support for many formats: JSON, YAML, TOML, and MessagePack. And if you need more, there is a function that converts your data into JSON-compatible primitive types that you can then serialize as you want.

A nice bonus is the support for dataclass and TypedDict, so you can give it a try without rewriting the models you have.

#python
More from @itgram_channel
  1. Jul 26, 2023taplo is a CLI tool (and a Rust library) for working with TOML. It can check TOML files fo…
  2. Jul 21, 2023Mutation testing is technique for evaluating how good your tests are. The idea is that a s…
  3. Jul 19, 2023refurb is yet another linter for Python. What makes it special is that it internally runs…
  4. Jul 18, 2023To make all my content accessible, I've provided source, website, and RSS for every projec…
  5. Jul 17, 2023A long time ago, I used to use Twitter to find out about new projects and blog posts. Late…
  6. Jul 17, 2023Channel photo updated
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →