Marshmallow: json-to-schema helper?

Albert-Jan Roskam sjeik_appie at hotmail.com
Mon Apr 4 13:25:02 EDT 2022


   Hi,
   I'm looking for a convenience function to convert a Marshmallow schema
   into a valid Python class definition. That is, I want to generate python
   code (class MySchema.. etc) that I could write to a .py file. Does this
   exist? I tried the code below, but that is not the intended use of
   marshmallow.Schema.from_dict or inspect.getsource.
   from inspect import getsource
   from marshmallow import Schema
   d = dict(something='potentially', very='complicated')
   schema = Schema.from_dict(d)
   python_class_def_as_str = getsource(schema())  # OSError
   https://marshmallow.readthedocs.io/en/stable/api_reference.html#marshmallow.Schema.from_dict
   https://docs.python.org/3/library/inspect.html#inspect.getsource
   Thanks!
   Albert-Jan


More information about the Python-list mailing list