cattrs VS pydantic

Compare cattrs vs pydantic and see what are their differences.

cattrs

Complex custom class converters for attrs. (by python-attrs)
Our great sponsors
  • SonarQube - Static code analysis for 29 languages.
  • Scout APM - Less time debugging, more time building
  • SaaSHub - Software Alternatives and Reviews
cattrs pydantic
4 102
469 9,926
6.4% -
9.2 8.9
14 days ago 2 days ago
Python Python
MIT License MIT License
The number of mentions indicates the total number of mentions that we've tracked plus the number of user suggested alternatives.
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.

cattrs

Posts with mentions or reviews of cattrs. We have used some of these posts to build our list of alternatives and similar projects. The last one was on 2022-01-18.

pydantic

Posts with mentions or reviews of pydantic. We have used some of these posts to build our list of alternatives and similar projects. The last one was on 2022-05-06.
  • Flask vs FastAPI?
    11 projects | reddit.com/r/Python | 6 May 2022
    Yea everything is built from inspect up.Pydantic takes that and builds a class out of it. There are some strange side effects from that process that are non obvious especially when paired with how fastapi does their dependency injection creates some non obvious items. These items are very hard to modify.
  • Build a JSON file following a defined structure
    1 project | reddit.com/r/Python | 2 May 2022
    I'd say you're on the right track with dataclasses, but it might be easier to use Pydantic.
  • Which projects do you think have fantastic documentation?
    6 projects | reddit.com/r/Python | 29 Apr 2022
  • Configurable mapping file python
    1 project | reddit.com/r/learnpython | 29 Apr 2022
    It's not exactly the same thing but maybe Pydantic could be useful?
  • Python’s “Type Hints” are a bit of a disappointment to me
    15 projects | news.ycombinator.com | 21 Apr 2022
    Every point in this blog post strikes me as either (1) unaware of the tooling around python typing other than mypy, or (2) a criticism of static-typing-bolted-on-to-a-dynamically-typed-language, rather than Python's hints. Regarding (1), my advise to OP is to try out Pyright, Pydantic, and Typeguard. Pyright, especailly, is amazing and makes the process of working with type hints 2 or 3 times smoother IMO. And, I don't think points that fall under (2) are fair criticisms of type *hints*. They are called hints for a reason.

    Otherwise, here's a point-by-point response, either recommending OP checks out tooling, or showing that the point being made is not specific to Python.

    > type hints are not binding.

    There are projects [0][1] that allow you to enforce type hints at runtime if you so choose.

    It's worth mentioning that this is very analogous to how Typescript does it, in that type info is erased completely at runtime.

    > Type checking is your job after all, ...[and that] requires maintenance.

    There are LSPs like Pyright[2] (pyright specifically is the absolute best, IMO) that report type errors as you code. Again, this is very very similar to typescript.

    > There is an Any type and it renders everything useless

    I have never seen a static-typing tool that was bolted on to a dynamically typed language, without an `Any` type, including typescript.

    > Duck type compatibility of int and float

    The author admits that they cannot state why this behavior is problematic, except for saying that it's "ambiguous".

    > Most projects need third-party type hints

    Again, this is a criticism of all cases where static types are bolted on dynamically typed languages, not Python's implementation specifically.

    > Sadly, dataclasses ignore type hints as well

    Pydantic[3] is an amazing data parsing library that takes advantage of type hints, and it's interface is a superset of that of dataclasses. What's more, it underpins FastAPI[4], an amazing API-backend framework (with 44K Github stars).

    > Type inference and lazy programmers

    The argument of this section boils down to using `Any` as a generic argument not being an error by default. This is configurable to be an error both in Pyright[5], and mypy[6].

    > Exceptions are not covered [like Java]

    I can't find the interview/presentation, but Guido Van Rossum specifically calls out Java's implementation of "exception annotations" as a demonstration of why that is a bad idea, and that it would never happen in Python. I'm not saying Guido's opinion is the absolute truth, but just letting you know that this is an explicit decision, not an unwanted shortcoming.

    [0] https://github.com/RussBaz/enforce

    [1] https://github.com/agronholm/typeguard

    [2] https://github.com/microsoft/pyright

    [3] https://pydantic-docs.helpmanual.io

    [4] https://github.com/tiangolo/fastapi

    [5] https://github.com/microsoft/pyright/blob/main/docs/configur...

    [6] https://mypy.readthedocs.io/en/stable/config_file.html#confv...

  • How do you define beginner, intermediate, and advanced?
    1 project | reddit.com/r/Python | 30 Mar 2022
    I’d say take a look through Pydantic, lots of metaclassing in there. In particular with constrained numbers
  • How to build your own chatbot NLP engine
    7 projects | dev.to | 28 Mar 2022
    The main.py module is in charge of exposing our FastAPI methods. As an example, this is the method for training a bot. It relies on Pydantic to facilitate the processing of the JSON input and output parameters. Parameter types are the dto version of the dsl classes.
  • Do type hints make your code slower?
    1 project | reddit.com/r/Python | 27 Mar 2022
    Sorry, I was very unclear. They were going to postpone evaluation of annotations, breaking Pydantic notwithstanding some big efforts. See https://github.com/samuelcolvin/pydantic/issues/2678
  • FastAPI vs. Flask: Comparing the Pros and Cons of Top Microframeworks for Building a REST API in Python
    5 projects | dev.to | 26 Mar 2022
    FastAPI, on the other hand, gives us the Pydantic library to use, which makes data validation much simpler and faster than typing it by hand. It’s closely related to FastAPI itself, so we can be sure that Pydantic will be compatible with our framework at all times.
  • Azure Functions and FastAPI
    3 projects | dev.to | 24 Mar 2022
    Model binding for requests and response with additional model validation features provided by Pydantic.

What are some alternatives?

When comparing cattrs and pydantic you can also consider the following projects:

sqlmodel - SQL databases in Python, designed for simplicity, compatibility, and robustness.

Cerberus - Lightweight, extensible data validation library for Python

SQLAlchemy - The Database Toolkit for Python

marshmallow - A lightweight library for converting complex objects to and from simple Python datatypes.

pyparsing - Python library for creating PEG parsers [Moved to: https://github.com/pyparsing/pyparsing]

nexe - 🎉 create a single executable out of your node.js apps

Lark - Lark is a parsing toolkit for Python, built with a focus on ergonomics, performance and modularity.

phonenumbers - Python port of Google's libphonenumber

Flask - The Python micro framework for building web applications.

Construct - Construct: Declarative data structures for python that allow symmetric parsing and building

Fast JSON schema for Python - Fast JSON schema validator for Python.

TextDistance - Compute distance between sequences. 30+ algorithms, pure python implementation, common interface, optional external libs usage.