Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert bi_called == bi_pristine, assertmsg
assert bi_called.inv == bi_pristine.inv, assertmsg
else:
ordered = issubclass(bi_cls, OrderedBidictBase)
dict_cls = OrderedDict if ordered else dict
dict_meth = getattr(dict_cls, methodname, None)
if dict_meth:
compare_dict = dict_cls(init_items)
dict_result = dict_meth(compare_dict, *args)
if isinstance(dict_result, c.Iterable):
collection = list if ordered else set
result = collection(result)
dict_result = collection(dict_result)
assert result == dict_result
# Whether the call failed or succeeded, bi_called should pass consistency checks.
assert len(bi_called) == sum(1 for _ in iteritems(bi_called))
assert len(bi_called.inv) == sum(1 for _ in iteritems(bi_called.inv))
assert bi_called == dict(bi_called)
assert bi_called.inv == dict(bi_called.inv)
assert bi_called == OrderedDict((k, v) for (v, k) in iteritems(bi_called.inv))
assert bi_called.inv == OrderedDict((v, k) for (k, v) in iteritems(bi_called))
def _wrapped(items): # noqa: E306 (expected 1 blank line before a nested definition)
fwd = dict(items)
if key:
assume(len(fwd) < len(items))
if val:
inv = dict((v, k) for (k, v) in items)
assume(len(inv) < len(items))
if key and val:
invinv = dict((v, k) for (k, v) in iteritems(inv))
# If an item has a duplicate key and val, they must duplicate two other distinct items.
assume(len(invinv) < len(fwd))
return items
return _wrapped
assume(init_items != init_unequal)
other_unequal = other_cls(init_unequal)
assert not some_bidict.equals_order_sensitive(other_unequal)
other_unequal_inv = getattr(other_unequal, 'inv',
OrderedDict((v, k) for (k, v) in iteritems(other_unequal)))
assert not some_bidict.inv.equals_order_sensitive(other_unequal_inv)
assume(set(init_items) != set(init_unequal))
assert some_bidict != other_unequal
assert not some_bidict == other_unequal
assert some_bidict.inv != other_unequal_inv
assert not some_bidict.inv == other_unequal_inv
assert collection(iteritems(some_bidict)) != collection(iteritems(other_unequal))
assert collection(iteritems(some_bidict.inv)) != collection(iteritems(other_unequal_inv))
assert not some_bidict == not_a_mapping
assert not some_bidict.inv == not_a_mapping
assert some_bidict != not_a_mapping
assert some_bidict.inv != not_a_mapping
if has_eq_order_sens:
assert not some_bidict.equals_order_sensitive(not_a_mapping)
assert not some_bidict.inv.equals_order_sensitive(not_a_mapping)
def test_eq_ne_hash(bi_cls, other_cls, init_items, init_unequal, not_a_mapping):
"""Test various equality comparisons and hashes between bidicts and other objects."""
# pylint: disable=too-many-locals
some_bidict = bi_cls(init_items)
other_equal = other_cls(init_items)
other_equal_inv = getattr(other_equal, 'inv',
OrderedDict((v, k) for (k, v) in iteritems(other_equal)))
bidict_is_ordered = isinstance(some_bidict, OrderedBidictBase)
other_is_ordered = issubclass(other_cls, (OrderedBidictBase, OrderedDict))
collection = list if bidict_is_ordered and other_is_ordered else set
both_hashable = all(isinstance(i, c.Hashable) for i in (some_bidict, other_equal))
has_eq_order_sens = getattr(bi_cls, 'equals_order_sensitive', None)
other_unequal = other_cls(init_unequal)
other_unequal_inv = getattr(other_unequal, 'inv',
OrderedDict((v, k) for (k, v) in iteritems(other_unequal)))
assert some_bidict == other_equal
assert not some_bidict != other_equal
assert some_bidict.inv == other_equal_inv
assert not some_bidict.inv != other_equal_inv
assert collection(iteritems(some_bidict)) == collection(iteritems(other_equal))
assert collection(iteritems(some_bidict.inv)) == collection(iteritems(other_equal_inv))
if both_hashable:
assert hash(some_bidict) == hash(other_equal)
if has_eq_order_sens and other_is_ordered:
assert some_bidict.equals_order_sensitive(other_equal)
assert some_bidict.inv.equals_order_sensitive(other_equal_inv)
assume(init_items != init_unequal)
other_unequal = other_cls(init_unequal)
assert not some_bidict.equals_order_sensitive(other_unequal)
other_unequal_inv = getattr(other_unequal, 'inv',
OrderedDict((v, k) for (k, v) in iteritems(other_unequal)))
assert not some_bidict.inv.equals_order_sensitive(other_unequal_inv)
assume(set(init_items) != set(init_unequal))
assert some_bidict != other_unequal
assert not some_bidict == other_unequal
assert some_bidict.inv != other_unequal_inv
assert not some_bidict.inv == other_unequal_inv
assert collection(iteritems(some_bidict)) != collection(iteritems(other_unequal))
assert collection(iteritems(some_bidict.inv)) != collection(iteritems(other_unequal_inv))
assert not some_bidict == not_a_mapping
assert not some_bidict.inv == not_a_mapping
assert some_bidict != not_a_mapping
assert some_bidict.inv != not_a_mapping
other_is_ordered = issubclass(other_cls, (OrderedBidictBase, OrderedDict))
collection = list if bidict_is_ordered and other_is_ordered else set
both_hashable = all(isinstance(i, c.Hashable) for i in (some_bidict, other_equal))
has_eq_order_sens = getattr(bi_cls, 'equals_order_sensitive', None)
other_unequal = other_cls(init_unequal)
other_unequal_inv = getattr(other_unequal, 'inv',
OrderedDict((v, k) for (k, v) in iteritems(other_unequal)))
assert some_bidict == other_equal
assert not some_bidict != other_equal
assert some_bidict.inv == other_equal_inv
assert not some_bidict.inv != other_equal_inv
assert collection(iteritems(some_bidict)) == collection(iteritems(other_equal))
assert collection(iteritems(some_bidict.inv)) == collection(iteritems(other_equal_inv))
if both_hashable:
assert hash(some_bidict) == hash(other_equal)
if has_eq_order_sens and other_is_ordered:
assert some_bidict.equals_order_sensitive(other_equal)
assert some_bidict.inv.equals_order_sensitive(other_equal_inv)
assume(init_items != init_unequal)
other_unequal = other_cls(init_unequal)
assert not some_bidict.equals_order_sensitive(other_unequal)
other_unequal_inv = getattr(other_unequal, 'inv',
OrderedDict((v, k) for (k, v) in iteritems(other_unequal)))
assert not some_bidict.inv.equals_order_sensitive(other_unequal_inv)
def __inverted__(self):
"""Get an iterator over the items in :attr:`inv`."""
return iteritems(self.inv)
def __repr__(self):
tmpl = self.__class__.__name__ + '('
if not self:
return tmpl + ')'
tmpl += '%r)'
# If we have a truthy __reversed__ attribute, use an ordered repr.
# (Python doesn't provide an Ordered or OrderedMapping ABC, else we'd
# check that. Must use getattr rather than hasattr since __reversed__
# may be set to None, which signifies non-ordered/-reversible.)
ordered = bool(getattr(self, '__reversed__', False))
delegate = list if ordered else dict
return tmpl % delegate(iteritems(self))