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/feature request related to only and exclude

See original GitHub issue

Hi, first off this project it has helped us a lot, so a huge thanks!

I have a question / request related to the only and exclude parameters to schemas (and nested fields).

Provided a schema looks like this:

class TestSchema(Schema):
    foo = Int()
    bar = Str()
    baz = Dict()

The following happens: schema = TestSchema(only=('foo', 'bar', )) // works as expected schema = TestSchema(only=('foo', 'not_part_of_schema', )) // adds not_part_of_schema schema = TestSchema(exclude=('bar', 'baz', )) // works as expected schema = TestSchema(exclude=('bar', 'not_part_of_schema', )) // not_part_of_schema does nothing

The following should happen in my opinion (to avoid leaking input data when schema forbids it): schema = TestSchema(only=('foo', 'bar', )) // works schema = TestSchema(only=('foo', 'not_part_of_schema', )) // raises exception schema = TestSchema(exclude=('bar', 'baz', )) // works schema = TestSchema(exclude=('bar', 'not_part_of_schema', )) // raises exception

To get the behaviour above we’ve implemented the following override to a base schema:

class BaseSchema(Schema):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs) // sorry py3 only

        valid_fields = set(self.declared_fields.keys()) | set(self.opts.additional)
        excluded_fields = set(self.exclude) | set(self.opts.exclude)
        only_fields = set(self.only) if self.only else set()

        if not excluded_fields.issubset(valid_fields):
            raise LookupError('Excluded field(s) not part of valid fields')

        valid_fields -= excluded_fields
        if not only_fields.issubset(valid_fields):
            raise LookupError('Only field(s) not part of valid fields')

Is this behaviour possible in the current Marshmallow code? If not, is our solution even recommended (am I missing some really obvious use case for the injection)? If yes, is this something that could be useful in Marshmallow?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
lafrechcommented, Sep 2, 2018

Hey !

Good news everyone

This was done in https://github.com/marshmallow-code/marshmallow/pull/826, available since 3.0.0b12.

0reactions
lafrechcommented, Feb 9, 2018

Should we also check only/exclude conflicts (proposed in https://github.com/marshmallow-code/marshmallow/pull/731)?

Read more comments on GitHub >

github_iconTop Results From Across the Web

feature request: inverse targeting / exclude #2253 - GitHub
Is there anything that can be done such that db_instance - RDS formed by the terraform files can be saved if we destroy...
Read more >
Formatting Answer Choices - Qualtrics
Exclude From Analysis: Excludes the data for this answer choice from data analytics and reporting. The data for answers marked as Exclude From...
Read more >
What do you do when there is a last minute request to exclude ...
1. I would ask why it was excluded last minute. · 2. It's often down to weak management. · 11. It's better than...
Read more >
Ability to exclude Feature flags for specific users - GitLab
Relates to. Ability to add and remove users to a Feature Flag to expose a feature to only those specified users.
Read more >
Karate Gatling - exclude specific request or feature from report
In karate-config.js I am initially taking the authentication token only once by using callSingle(auth.feature) and this authentication token is ...
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