marshmallow-code / marshmallow-code/marshmallow

Generate a TypedDict or dataclass from Schema

Open
#1,411 8 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feedback welcome
Dominant language
Python
Stars
7.2k
Forks
738
Avg merge
1d 23h
Merged PRs (30d)
7

Description

It would be nice to be able to use `Schema`ta with mypy or similar, e.g.
```python
class User(Schema):
name = fields.String()
age = fields.Integer()

def have_a_birthday(user: User.type) -> User.type:
user['age'] += '1' # will fail type check
return user # typechecks OK
```

I started an inline implementation with `Schema._declared_fields`, but since there's nothing like a `Field.deser_type` it was going to be quite a hacky map by cases - `isinstance(cls, fields.String)` etc. - and then I realised I had nested fields to contend with and jumped out of the rabbit hole before I got too deep.

I do think this would be very nice to have though, or perhaps even go further and have a `@deser_to_dataclass` decorator (that's the route I was going) that re-writes the `Schema` so that the type is `User` (or whatever) itself, and `User.name` is annotated automatically as `: str`.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading Schema._declared_fields and the Field types mentioned, including fields.String and fields.Integer, then consider how nested fields and a possible deser_to_dataclass decorator would fit. Done means agreeing on and implementing a defined TypedDict or dataclass generation design with mypy-compatible types; the issue names no files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design, developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.