Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inclusive should support default values #381

Open
nickovs opened this issue Feb 10, 2019 · 5 comments
Open

Inclusive should support default values #381

nickovs opened this issue Feb 10, 2019 · 5 comments

Comments

@nickovs
Copy link
Contributor

nickovs commented Feb 10, 2019

The Inclusive schema builder does not support default values even though it inherits from Optional, which does support them. It would be valuable if defaults were supported for situations such as this:

dimensions = Schema({
    Inclusive("width", "dims", default=1024): Coerce(int),
    Inclusive("height", "dims", default=576): Coerce(int),
}

In general, when using Inclusive to enforce "all or nothing", having default values for the "nothing" case would be very helpful.

@sfelf
Copy link

sfelf commented Feb 12, 2019

Sorry dumb question here... how is this different from:

dimensions = Schema({
    Optional("width", default=1024): Coerce(int),
    Optional("height", default=576): Coerce(int),
}

@nickovs
Copy link
Contributor Author

nickovs commented Feb 12, 2019

If you put a set of values in an Inclusive group then either all of the items in the group need to be supplied or none of them can be. It is reasonable to want to set a default value for the set of variables while preserving that behaviour. With your schema dimensions({'width':17}) returns {'width':17, 'height':576} whereas with my schema it would be an error to leave it half-specified.

@sfelf
Copy link

sfelf commented Feb 12, 2019

Ok so then how does the default value come into play? If you are expecting dimensions({'width':17}) to raise an Invalid are you expecting dimensions({}) to return {'width': 1024, 'height': 576}?

@nickovs
Copy link
Contributor Author

nickovs commented Feb 12, 2019

Yes, that is exactly the behaviour I'm looking for. A schema that uses Inclusive requires the user to specify all or nothing. If they specify nothing then you get the default values. What you don't want is to get inconsistent results because someone half-specified what was needed. In the example above, allowing the default value for height to be used would give a box with a crazy aspect ratio and is likely not what was wanted.

@sfelf
Copy link

sfelf commented Feb 12, 2019

Thank you for the explanation. I wasn't expecting the inclusive check to occur before default created the key with the default value if the key didn't exist. Updating the documentation to explain how the default value works on Inclusive would be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants