Fix wrong exception raised in ProtocolBase getitem#138
Merged
cwacek merged 5 commits intocwacek:masterfrom Jun 12, 2018
Merged
Conversation
…ect of additional properties. When trying to use it as dict, for instance testing if a key is inside the dictionary, methods like __contains__ in the ProtocolBase expect getattr to raise a KeyError. For additional properties, the error raised is AttributeError, breaking the expected behaviour. Signed-off-by: Cedric ROMAN <roman@numengo.com>
…ot break expected behaviour of getattr Signed-off-by: Cedric ROMAN <roman@numengo.com>
… getitem Signed-off-by: Cedric ROMAN <roman@numengo.com>
houndci-bot
reviewed
Jun 3, 2018
| assert getattr(t,'not_present',None) == None | ||
|
|
||
| if __name__ == '__main__': | ||
| test_wrong_exception_protocolbase_getitem(base_schema()) |
| assert not 'c' in t.dictLike | ||
| assert getattr(t,'not_present',None) == None | ||
|
|
||
| if __name__ == '__main__': |
There was a problem hiding this comment.
expected 2 blank lines after class or function definition, found 1
| t.validate() | ||
| assert 'a' in t.dictLike | ||
| assert not 'c' in t.dictLike | ||
| assert getattr(t,'not_present',None) == None |
There was a problem hiding this comment.
missing whitespace after ','
comparison to None should be 'if cond is None:'
| t = ns.Example(dictLike={'a': 0,'b': 1}) | ||
| t.validate() | ||
| assert 'a' in t.dictLike | ||
| assert not 'c' in t.dictLike |
There was a problem hiding this comment.
test for membership should be 'not in'
| builder = pjo.ObjectBuilder(base_schema) | ||
| ns = builder.build_classes() | ||
|
|
||
| t = ns.Example(dictLike={'a': 0,'b': 1}) |
| to declare it as an object of additional properties. | ||
| When trying to use it as dict, for instance testing if a key is inside | ||
| the dictionary, methods like __contains__ in the ProtocolBase expect | ||
| __getitem__ to raise a KeyError. getitem calls __getattr__ without any |
| "dictLike": { | ||
| "additionalProperties": { | ||
| "type": "integer" | ||
| }, |
| return { | ||
| 'title': 'example', | ||
| 'type': 'object', | ||
| "additionalProperties": False, |
| import pytest | ||
|
|
||
| import python_jsonschema_objects as pjo | ||
| import json |
Signed-off-by: Cedric ROMAN <roman@numengo.com>
houndci-bot
reviewed
Jun 3, 2018
| t = ns.Example(dictLike={'a': 0, 'b': 1}) | ||
| t.validate() | ||
| assert 'a' in t.dictLike | ||
| assert not 'c' in t.dictLike |
There was a problem hiding this comment.
test for membership should be 'not in'
Signed-off-by: Cedric ROMAN <roman@numengo.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
To declare a dict like object in json-schema, we are supposed to declare it as an object of additional properties. When trying to use it as dict, for instance testing if a key is inside the dictionary, methods like contains in the ProtocolBase expect getitem to raise a KeyError.
getitem was calling getattr without any exception handling, which raises an AttributeError (necessary for proper behaviour of getattr, for instance).
Solution found is to handle AttributeError in getitem and to raise KeyError