Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DotDict behaves inconsistent on equality checks. x == y != not x != y and not x != y == not x == y #4956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Snooz82 opened this issue Nov 23, 2023 · 0 comments
Milestone

Comments

@Snooz82
Copy link
Member

Snooz82 commented Nov 23, 2023

As found in #4955 == and != do behave differently on DotDict.

While == behaves like dict the != does behave like OrderedDict.

Quote:
This not a == b vs a != b was the reason the utest fail in the branch.
And i would consider this an actual bug in DotDict implementation?

from collections import OrderedDict
from robot.utils import DotDict

# Normal dict: the order isn't relevant
norm_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7}
norm_dict_reverse = {'g': 7, 'f': 6, 'e': 5, 'd': 4, 'c': 3, 'b': 2, 'a': 1}

norm_dict == norm_dict_reverse    # True
norm_dict != norm_dict_reverse    # False
not norm_dict == norm_dict_reverse    # False
not norm_dict != norm_dict_reverse    # True

# OrderedDict: the order is important
order_dict = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
order_dict_reverse = OrderedDict([('g', 7), ('f', 6), ('e', 5), ('d', 4), ('c', 3), ('b', 2), ('a', 1)])

order_dict == order_dict_reverse    # False
order_dict != order_dict_reverse    # True
not order_dict == order_dict_reverse    # True
not order_dict != order_dict_reverse    # False

#DotDict : the order isn't relevant with == but is relevant with !=
dot_dict = DotDict({'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7})
dot_dict_reverse = DotDict({'g': 7, 'f': 6, 'e': 5, 'd': 4, 'c': 3, 'b': 2, 'a': 1})
dot_dict == dot_dict_reverse    # True because order is not relevant with ==
dot_dict != dot_dict_reverse    # True because order is relevant with !=
not dot_dict == dot_dict_reverse    # False
not dot_dict != dot_dict_reverse    # False

This also appears in Robot Framework Usage:

DotDict
image

OrderedDict
image

Normal python dict
image

Fix should just be adding this to DotDict:

    def __ne__(self, other):
        return not self == other

I think it is easiest if you quickly add this and a test for it yourself.

@Snooz82 Snooz82 added this to the v7.0 milestone Nov 23, 2023
Snooz82 added a commit that referenced this issue Nov 27, 2023
@pekkaklarck pekkaklarck added the acknowledge To be acknowledged in release notes label Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants