Skip to content

Fix unordered dict bug in to_dict()#689

Merged
caphrim007 merged 1 commit intoF5Networks:developmentfrom
caphrim007:bugfix.fix-unordered-dict-errors-py3
Sep 14, 2016
Merged

Fix unordered dict bug in to_dict()#689
caphrim007 merged 1 commit intoF5Networks:developmentfrom
caphrim007:bugfix.fix-unordered-dict-errors-py3

Conversation

@caphrim007
Copy link
Copy Markdown
Contributor

Issues:
Fixes #687

Problem:
On python3 (and also I presume on python2) the test_two_refs test in
test_mixins.py will fail because the returned value is

{"x": [TraversalRecord, "z"], "z": [1, "a"]}

instead of the expected value

{"x": [1, "a"], "z": ["TraversalRecord", "x"]}

Analysis:
I tracked this down to the _traverse_dict() method where it iterates
over the values of the instance_dict variable. The problem is that
because dictionaries are unordered, the first value that is iterated
upon will sometimes return "z" first instead of "x".

This causes the result to become "turned around" and the test to fail.

I never saw this crop up on python 2, but it crops up regularly on
python 3. The fix I applied is to cast the unordered dict in _traverse_dict
into an OrderedDict. Doing this makes the values of that variable always
known, so-to-speak, because they have a defined order. Therefore the
tests will pass as expected.

Tests:

  • f5/bigip/test/test_mixins.py::test_to_refs

Issues:
Fixes F5Networks#687

Problem:
On python3 (and also I presume on python2) the test_two_refs test in
test_mixins.py will fail because the returned value is

    {"x": [TraversalRecord, "z"], "z": [1, "a"]}

instead of the expected value

    {"x": [1, "a"], "z": ["TraversalRecord", "x"]}

Analysis:
I tracked this down to the `_traverse_dict()` method where it iterates
over the values of the `instance_dict` variable. The problem is that
because dictionaries are unordered, the first value that is iterated
upon will sometimes return "z" first instead of "x".

This causes the result to become "turned around" and the test to fail.

I never saw this crop up on python 2, but it crops up regularly on
python 3. The fix I applied is to cast the unordered dict in `_traverse_dict`
into an OrderedDict. Doing this makes the values of that variable always
known, so-to-speak, because they have a defined order. Therefore the
tests will pass as expected.

Tests:
  * f5/bigip/test/test_mixins.py::test_to_refs
@caphrim007 caphrim007 force-pushed the bugfix.fix-unordered-dict-errors-py3 branch from 306e741 to 9100ea9 Compare September 13, 2016 20:15
@caphrim007 caphrim007 closed this Sep 13, 2016
@caphrim007 caphrim007 reopened this Sep 13, 2016
@caphrim007
Copy link
Copy Markdown
Contributor Author

@zancas and @pjbreaux this requires some architectural discussion as it adds a new pip dependency in python 2.6.

@zancas
Copy link
Copy Markdown
Contributor

zancas commented Sep 14, 2016

Nice job @caphrim007 !! Thanks. I'm +1 to merge this... I'm OK with a new pip dependency for <= 2.6.

@zancas zancas removed their assignment Sep 14, 2016
@caphrim007 caphrim007 merged commit 480c982 into F5Networks:development Sep 14, 2016
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.

iterating in to_dict() causes test failures on python3

3 participants