Skip to content

Handle non-subscriptable path in humanize_error#206

Merged
tusharmakkar08 merged 2 commits intoalecthomas:masterfrom
kellerza:humanize
Sep 22, 2016
Merged

Handle non-subscriptable path in humanize_error#206
tusharmakkar08 merged 2 commits intoalecthomas:masterfrom
kellerza:humanize

Conversation

@kellerza
Copy link
Contributor

Description

_nested_getitem currently raises an exception when the error path contains a non-subscriptable entity (e.g. None, int)

data = None, path = ['input_select', 'hello', 'options', 0]

    def _nested_getitem(data, path):
        for item_index in path:
            try:
>               data = data[item_index]
E               TypeError: 'NoneType' object is not subscriptable

../hv/lib64/python3.4/site-packages/voluptuous/humanize.py:11: TypeError

This happens in the following situations:
Data to be validated: {'key1': None} or {'key1': 1}
Schema: vol.Schema('key1': vol.All(ensure_list, [str]))

ensure_list mutates the data into an array, if [str] fails the exception contains [0] as an index in the mutated data, but the exception can only be logged using the original (non-list) data, in which case [0] cannot be applied to a non-subscriptable path like int[0] or None[0].

# The index is not present in the dictionary, list or other indexable
return None
except TypeError: # data is not subscriptable
break
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kellerza Shouldn't it return None instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The advantage of resolving until you hit a failure (by breaking out) is that it helps you in the correct direction.

Happy to comply either way, your call

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it consistent with KeyError and IndexError. You can add it in Line 12 and update the comment correspondingly.

@tusharmakkar08 tusharmakkar08 merged commit 79d7ed8 into alecthomas:master Sep 22, 2016
@tusharmakkar08
Copy link
Collaborator

@kellerza : Merged 🍰

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

Successfully merging this pull request may close these issues.

2 participants