Get error when I set fields as tuples instead of lists #271

Closed
theosotr opened this Issue Sep 27, 2016 · 3 comments

Comments

Projects
None yet
3 participants
@theosotr

I have the following piece of code.

from cerberus import Validator


schema = {
    'my_field': {
        'type': 'list',
        'schema': {'type': 'string'}
    }
}

validator = Validator(schema)

data = {'my_field': ('foo', 'bar')}

validator.validate(data)

I get the following exception:
TypeError: 'tuple' object does not support item assignment

When I set my_field as list works fine. However, I would like cerberus to support tuple objects as well.

@funkyfuture

This comment has been minimized.

Show comment
Hide comment
@funkyfuture

funkyfuture Sep 27, 2016

Member
Member

funkyfuture commented Sep 27, 2016

@andreymal

This comment has been minimized.

Show comment
Hide comment
@andreymal

andreymal Sep 27, 2016

I'm not author, but I could:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 739, in validate
    self.__normalize_mapping(self.document, self.schema)
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 499, in __normalize_mapping
    self.__normalize_containers(mapping, schema)
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 564, in __normalize_containers
    self.__normalize_sequence(field, mapping, schema)
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 620, in __normalize_sequence
    mapping[field][i] = result[i]
TypeError: 'tuple' object does not support item assignment

Generally, Cerberus modifies input, I think this is very bad:

original = [1, 2]

schema = {
    'my_field': {'type': 'list', 'schema': {'type': 'string', 'coerce': str}}
}

validator = Validator(schema)
validator.validate({'my_field': original})

print(original == [1, 2])  # False — WAT?
print(original == ['1', '2'])  # True — WAAT??!1

andreymal commented Sep 27, 2016

I'm not author, but I could:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 739, in validate
    self.__normalize_mapping(self.document, self.schema)
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 499, in __normalize_mapping
    self.__normalize_containers(mapping, schema)
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 564, in __normalize_containers
    self.__normalize_sequence(field, mapping, schema)
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 620, in __normalize_sequence
    mapping[field][i] = result[i]
TypeError: 'tuple' object does not support item assignment

Generally, Cerberus modifies input, I think this is very bad:

original = [1, 2]

schema = {
    'my_field': {'type': 'list', 'schema': {'type': 'string', 'coerce': str}}
}

validator = Validator(schema)
validator.validate({'my_field': original})

print(original == [1, 2])  # False — WAT?
print(original == ['1', '2'])  # True — WAAT??!1
@funkyfuture

This comment has been minimized.

Show comment
Hide comment
@funkyfuture

funkyfuture Nov 2, 2016

Member

@theosotr support for normalizing tuples would have to be amended. it's certainly welcome. in your case you could also avoid hitting the normalization code by calling validate with normalize=False.

@andreymal this behavior has recently been amended to the docs.

Member

funkyfuture commented Nov 2, 2016

@theosotr support for normalizing tuples would have to be amended. it's certainly welcome. in your case you could also avoid hitting the normalization code by calling validate with normalize=False.

@andreymal this behavior has recently been amended to the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment