Generate a TypedDict or dataclass from Schema
Author: OJFordCreated Sep 17, 2019Updated Feb 12, 2026
Labelsfeedback welcome
It would be nice to be able to use Schemata with mypy or similar, e.g.
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 OKI 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.
Source: marshmallow-code/marshmallow