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.

Allow callables for fields.Nested

See original GitHub issue

Suggested earlier here: https://github.com/marshmallow-code/marshmallow/issues/19#issuecomment-43685498.

The suggestion would be to allow:

foo = fields.Nested(lambda: Foo)

in addition to

foo = fields.Nested('Foo')

Seems a bit cleaner in general.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:36 (32 by maintainers)

github_iconTop GitHub Comments

3reactions
sloriacommented, Oct 31, 2015

Reopening this because I think this would enable valid use cases that are currently unmet.

(1) Passing arbitrary arguments to the nested Schema (only many, exclude, and only are currently supported. This could solve https://github.com/marshmallow-code/marshmallow/issues/285#issuecomment-152577723.

# Pass arbitrary arguments
foo = fields.Nested(lambda: FooSchema(partial=True))

# https://github.com/marshmallow-code/marshmallow/issues/285#issuecomment-152577723
class TreeSchema(Schema):
    tree = fields.Nested(lambda: TreeSchema(many=True, strict=True))

(2) With a slight modification to the OP’s proposed API, users could easily implement a polymorphic nested field.

def make_schema(obj):
    if isinstance(obj, Foo):
        return FooSchema()
    elif isinstance(obj, Bar):
        return BarSchema()

class MySchema(Schema):
    poly = fields.Nested(make_schema)

Feedback welcome.

1reaction
deckar01commented, Jul 31, 2018

Not in marshmallow currently, but it is a schema syntax I have been investigating. Also, https://github.com/justanr/marshmallow-annotations/issues/5.

Back on track: I don’t think Method and Function are viable substitutes for a complete Nested field. The suggested workaround is not something I would expect developers to discover on their own or something I would want to document. Strings work well and are easy to document/use for recursively schema references. If a callable is needed for more complex behavior, a custom field is probably the best tool for the job.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nesting Schemas — marshmallow 3.19.0 documentation
If you have two objects that nest each other, you can pass a callable to Nested . This allows you to resolve order-of-declaration...
Read more >
How to use the marshmallow.fields.Nested function in ... - Snyk
To help you get started, we've selected a few marshmallow.fields.Nested examples, based on popular ways it is used in public projects.
Read more >
2.3. Defining documents — MongoEngine 0.24.2 documentation
MongoEngine allows you to define schemata for documents as this helps to reduce coding errors, and allows for utility methods to be defined...
Read more >
Python Inner Functions: What Are They Good For?
Inner functions, also known as nested functions, are functions that you define inside other functions. In Python, this kind of function has direct...
Read more >
Call functions from your app | Cloud Functions for Firebase
The Cloud Functions for Firebase client SDKs let you call functions directly ... With callables, Firebase Authentication tokens, FCM tokens, and App Check ......
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