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

[] should evaluate to an empty list, not any list #198

Closed
tuukkamustonen opened this issue Sep 11, 2016 · 8 comments
Closed

[] should evaluate to an empty list, not any list #198

tuukkamustonen opened this issue Sep 11, 2016 · 8 comments

Comments

@tuukkamustonen
Copy link
Contributor

>>> from voluptuous import Schema
>>> Schema({})({})
{}
>>> Schema({})({'foo': 1})
Traceback (most recent call last):
...
voluptuous.error.MultipleInvalid: extra keys not allowed @ data['foo']
>>> Schema(int)(5)
5
>>> Schema(2)(5)
Traceback (most recent call last):
...
voluptuous.error.MultipleInvalid: not a valid value
>>> Schema([])([])
[]
>>> Schema([])([1])  # shouldn't this throw error?
[1]

Wouldn't it be logical to evaluate list for any list and [] for an exact value (an empty list)?

@tusharmakkar08
Copy link
Collaborator

I also agree with you here. One problem which I could think is the backward compatibility loss in here.
@alecthomas What do you think ?

@alecthomas
Copy link
Owner

Yes, agreed. I don't think there is a significant issue with backwards compatibility, as even following the description of how [...] works you would expect this to be the case. That is, I think relying on [] to allow arbitrary list values is relying on undefined behaviour.

@tusharmakkar08
Copy link
Collaborator

@tuukkamustonen : Please submit the PR for the same. If you need any help, reach out to us.

@tuukkamustonen
Copy link
Contributor Author

tuukkamustonen commented Sep 12, 2016

Should we also change {} to evaluate as is (an empty dict) always (even with extra=True)?

If one wants to define a free-form dict:

  • With extra=False, he needs to use dict anyway.
  • With extra=True, he can use dict instead of {}.

{...} would still be evaluated as nested schema and not as is.

That would break backwards-compatibility but would maybe make logic even more coherent (classes evaluated as types, instances evaluate as exact... except non-empty dict).

@alecthomas @tusharmakkar08 What do you think?

@tuukkamustonen
Copy link
Contributor Author

Ping @alecthomas @tusharmakkar08

@tusharmakkar08
Copy link
Collaborator

Agree on this. It would make voluptuous more consistent.

@tusharmakkar08
Copy link
Collaborator

@tuukkamustonen : Are you working on PR for this issue?

@tuukkamustonen
Copy link
Contributor Author

@tusharmakkar08: Not yet, but I'll take a look at it today...

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

3 participants