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.

Question: How can I pass the context in a nested field of a structured dict?

See original GitHub issue

I noticed that if you use a nested field for values in a structured Dict, the context is not automatically given to the nested schema. Is there a way to pass it the context?

Example:

class Inner(Schema):
    foo = fields.String()

    @validates('foo')
    def validate_foo(self, value):
        if 'foo_context' not in self.context:
            raise ValidationError('no context!')


class Outer(Schema):
    bar = fields.Dict(values=fields.Nested(Inner))

# gives no error:
Inner(context={'foo_context': 'foo'}).load({'foo': 'some foo'})
# gives 'no context!' error:
Outer(context={'foo_context': 'foo'}).load({'bar': { 'key': {'foo': 'some foo'}}})

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, May 20, 2018

Dict needs to copy the _add_to_schema logic from List to link the “container” to the parent.

1reaction
sloriacommented, May 19, 2018

It looks like List works as expected. The following test passes on 2.x-line:

    def test_nested_list_fields_inherit_context(self):
        class InnerSchema(Schema):
            foo = fields.Field()

            @validates('foo')
            def validate_foo(self, value):
                if 'foo_context' not in self.context:
                    raise ValidationError('Missing context')

        class OuterSchema(Schema):
            bars = fields.List(fields.Nested(InnerSchema()))

        inner = InnerSchema(strict=True)
        inner.context['foo_context'] = 'foo'
        assert inner.load({'foo': 42})

        outer = OuterSchema(strict=True)
        outer.context['foo_context'] = 'foo'
        assert outer.load({'bars': [{'foo': 42}]})

And I verified that the test fails if outer.context['foo_context'] = 'foo' is removed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How do I use nested lists properly in that context
I need to create a memory game in Python. I need to print a 5x4 (4 rows, 5 elements in a line) field...
Read more >
collections — Container datatypes — Python 3.11.1 ...
This module implements specialized container datatypes providing alternatives to Python's general purpose built-in containers, dict , list , set , and tuple ....
Read more >
Sorting a Python Dictionary: Values, Keys, and More
In this tutorial, you'll get the lowdown on sorting Python dictionaries. By the end, you'll be able to sort by key, value, or...
Read more >
Translating dictionary keys in complex nested Python structures
Put simply: given a structure (e.g. nested dictionaries) and a mapping of old dictionary keys to new ones, produce a new structure that...
Read more >
Specify nested and repeated columns in table schemas
To create a column with repeated data, set the mode of the column to REPEATED in the schema. A repeated field can be...
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