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

Remove value enumeration when validating empty list #434

Merged
merged 2 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ commands =
--with-coverage3 \
--cover3-package=voluptuous \
--cover3-branch \
--verbose
--verbose {posargs}

[testenv:flake8]
deps = flake8
Expand Down
4 changes: 2 additions & 2 deletions voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,11 @@ def validate_sequence(path, data):
if not isinstance(data, seq_type):
raise er.SequenceTypeInvalid('expected a %s' % seq_type_name, path)

# Empty seq schema, allow any data.
# Empty seq schema, reject any data.
if not schema:
if data:
raise er.MultipleInvalid([
er.ValueInvalid('not a valid value', [value]) for value in data
er.ValueInvalid('not a valid value', path if path else data)
])
return data

Expand Down
14 changes: 14 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,3 +1528,17 @@ def test_any_with_discriminant():
assert_equal(str(e), 'expected bool for dictionary value @ data[\'implementation\'][\'c-value\']')
else:
assert False, "Did not raise correct Invalid"


def test_empty_list_raises_error_of_key_not_values():
""" https://github.com/alecthomas/voluptuous/issues/397 """
schema = Schema({
Required('variables', default=[]): []
})

try:
schema({'variables': ['x']})
except MultipleInvalid as e:
assert_equal(str(e), "not a valid value for dictionary value @ data['variables']")
else:
assert False, "Did not raise correct Invalid"