Skip to content

Fix wrong exception raised in ProtocolBase getitem#138

Merged
cwacek merged 5 commits intocwacek:masterfrom
RomanCedric:fix/wrong-exception-protocolbase-getitem
Jun 12, 2018
Merged

Fix wrong exception raised in ProtocolBase getitem#138
cwacek merged 5 commits intocwacek:masterfrom
RomanCedric:fix/wrong-exception-protocolbase-getitem

Conversation

@RomanCedric
Copy link
Copy Markdown
Contributor

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

…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>
assert getattr(t,'not_present',None) == None

if __name__ == '__main__':
test_wrong_exception_protocolbase_getitem(base_schema())
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

trailing whitespace

assert not 'c' in t.dictLike
assert getattr(t,'not_present',None) == None

if __name__ == '__main__':
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

test for membership should be 'not in'

builder = pjo.ObjectBuilder(base_schema)
ns = builder.build_classes()

t = ns.Example(dictLike={'a': 0,'b': 1})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

missing whitespace after ','

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

trailing whitespace

"dictLike": {
"additionalProperties": {
"type": "integer"
},
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

trailing whitespace

return {
'title': 'example',
'type': 'object',
"additionalProperties": False,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

trailing whitespace

import pytest

import python_jsonschema_objects as pjo
import json
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'json' imported but unused

Signed-off-by: Cedric ROMAN <roman@numengo.com>
t = ns.Example(dictLike={'a': 0, 'b': 1})
t.validate()
assert 'a' in t.dictLike
assert not 'c' in t.dictLike
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

test for membership should be 'not in'

Signed-off-by: Cedric ROMAN <roman@numengo.com>
@cwacek cwacek merged commit d19b87b into cwacek:master Jun 12, 2018
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.

3 participants