question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mashumaro instead of Pydantic

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from dataclasses import dataclass
from typing import Optional

from fastapi import FastAPI
from mashumaro import DataClassJSONMixin

app = FastAPI()


@dataclass
class Item(DataClassJSONMixin):
    name: str
    price: float
    is_offer: Optional[bool] = None


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}


@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    return {"item_name": item.name, "item_id": item_id}

Description

pydantic can be a bit on the slower side and the default functionality is not geared towards strict typing. "1" can become 1 in an int() field. I don’t think this is good behavior for APIs. Also, mypy only catches pydantic issues with a plugin. Which does work well, but it is a bit unusual in my mind.

https://github.com/Fatal1ty/mashumaro looks like it provides the needed functionality, but with speed benefits and strictness.

I know it’d be a huge change, or maybe mashumaro could be supported in addition. In the past I’ve liked dacite, but it looks like mashumaro might be even better.

Just to note that as it is, dataclasses in FastAPI are very limited and buggy.

Wanted Solution

Being able to use mashumaro classes in FastAPI.

Wanted Code

from dataclasses import dataclass
from typing import Optional

from fastapi import FastAPI
from mashumaro import DataClassJSONMixin

app = FastAPI()


@dataclass
class Item(DataClassJSONMixin):
    name: str
    price: float
    is_offer: Optional[bool] = None


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}


@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    return {"item_name": item.name, "item_id": item_id}

Alternatives

dacite

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.68.1

Python Version

3.8.10

Additional Context

Thank you!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
c00kiemon5tercommented, Jul 18, 2022

apischema actually looks very good!

1reaction
kevinheaveycommented, Dec 6, 2021

One key feature that Pydantic has, that most of the other libraries are missing as far as I can tell, is JSON schema support and related types (e.g. constr(regex=...) correctly translates to the pattern JSON schema field), which is required in order for FastAPI to generate OpenAPI schemas.

Just chiming in to add that apischema supports this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

marshmallow vs. pydantic - Python's 2 best libraries for data ...
An introduction and comparison of the Python libraries marshmallow vs. pydantic, which (de-) serialize data from and to Python objects and ...
Read more >
I use Attrs instead of Pydantic - Hacker News
Wait so there is a Marshmallow and a Mashumaro which is the romanisation of the Japanese translation of marshmallow!? Talking about giving ...
Read more >
mashumaro · PyPI
mashumaro is a fast and well tested serialization framework on top of ... Instead of creating two similar dataclasses we can have one...
Read more >
apischema v0.17 - I've developed the fastest typed JSON (de ...
You've surely heard about Pydantic , which has contributed a lot to… ... It's also faster than alternatives like mashumaro or cattrs.
Read more >
orjson vs mashumaro - compare differences and reviews?
Compare orjson vs mashumaro and see what are their differences. ... more comparison with Pydantic, which more than 5x slower (up to 30x...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found