Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
MyNamedBidict = namedbidict('MyNamedBidict', 'key', 'val')
MyNamedFrozenBidict = namedbidict('MyNamedBidict', 'key', 'val', base_type=frozenbidict)
NAMEDBIDICT_VALID_NAME = re.compile('[A-z][A-z0-9_]*$')
MUTABLE_BIDICT_TYPES = (
bidict, OrderedBidict, MyNamedBidict)
IMMUTABLE_BIDICT_TYPES = (frozenbidict, FrozenOrderedBidict, MyNamedFrozenBidict)
ORDERED_BIDICT_TYPES = (OrderedBidict, FrozenOrderedBidict)
BIDICT_TYPES = MUTABLE_BIDICT_TYPES + IMMUTABLE_BIDICT_TYPES
MAPPING_TYPES = BIDICT_TYPES + (dict, OrderedDict)
H_BIDICT_TYPES = strat.sampled_from(BIDICT_TYPES)
H_MUTABLE_BIDICT_TYPES = strat.sampled_from(MUTABLE_BIDICT_TYPES)
H_IMMUTABLE_BIDICT_TYPES = strat.sampled_from(IMMUTABLE_BIDICT_TYPES)
H_ORDERED_BIDICT_TYPES = strat.sampled_from(ORDERED_BIDICT_TYPES)
H_MAPPING_TYPES = strat.sampled_from(MAPPING_TYPES)
H_NAMES = strat.sampled_from(('valid1', 'valid2', 'valid3', 'in-valid'))
H_DUP_POLICIES = strat.sampled_from((IGNORE, OVERWRITE, RAISE))
H_BOOLEANS = strat.booleans()
H_TEXT = strat.text()
H_NONE = strat.none()
H_IMMUTABLES = H_BOOLEANS | H_TEXT | H_NONE | strat.integers() | strat.floats(allow_nan=False)
H_NON_MAPPINGS = H_NONE
H_PAIRS = strat.tuples(H_IMMUTABLES, H_IMMUTABLES)
class AbstractBimap(BidirectionalMapping): # pylint: disable=abstract-method
"""Dummy type that explicitly extends BidirectionalMapping
but fails to provide a concrete implementation for the
:attr:`BidirectionalMapping.inverse` :func:`abc.abstractproperty`.
As a result, attempting to create an instance of this class
should result in ``TypeError: Can't instantiate abstract class
AbstractBimap with abstract methods inverse``
"""
__getitem__ = NotImplemented
__iter__ = NotImplemented
__len__ = NotImplemented
BIDICT_TYPES = (bidict, frozenbidict, FrozenOrderedBidict, OrderedBidict)
BIMAP_TYPES = BIDICT_TYPES + (VirtualBimapSubclass, AbstractBimap)
NOT_BIMAP_TYPES = (dict, object)
MUTABLE_BIDICT_TYPES = (bidict, OrderedBidict)
HASHABLE_BIDICT_TYPES = (frozenbidict, FrozenOrderedBidict)
ORDERED_BIDICT_TYPES = (OrderedBidict, FrozenOrderedBidict)
@pytest.mark.parametrize('bi_cls', BIMAP_TYPES)
def test_issubclass_bimap(bi_cls):
"""All bidict types should subclass :class:`BidirectionalMapping`,
and any class conforming to the interface (e.g. VirtualBimapSubclass)
should be considered a (virtual) subclass too.
"""
assert issubclass(bi_cls, BidirectionalMapping)
class OrderedBidictSubcls(OrderedBidict):
pass
# pylint: disable=C0103
items = [('a', 1), ('b', 2)] # use int values so makes sense with Counter
itemsreversed = list(reversed(items))
bidict_items = bidict(items)
frozenbidict_items = FrozenBidict(items)
namedbidict_items = namedbidict('named', 'keys', 'vals')(items)
orderedbidict_items = OrderedBidict(items)
orderedbidict_itemsreversed = OrderedBidict(itemsreversed)
orderedbidictsubcls_items = OrderedBidictSubcls(items)
orderedbidictsubcls_itemsreversed = OrderedBidictSubcls(itemsreversed)
frozenorderedbidict_items = FrozenOrderedBidict(items)
frozenorderedbidict_itemsreversed = FrozenOrderedBidict(itemsreversed)
bidicts = (
bidict_items,
frozenbidict_items,
namedbidict_items,
orderedbidict_items,
orderedbidict_itemsreversed,
orderedbidictsubcls_items,
orderedbidictsubcls_itemsreversed,
frozenorderedbidict_items,
frozenorderedbidict_itemsreversed,
)
dict_itemsreversed = dict(itemsreversed)
counter_itemsreversed = Counter(dict_itemsreversed)
defaultdict_itemsreversed = defaultdict(lambda x: None, itemsreversed)
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""Types for Hypothoses tests."""
from collections import OrderedDict
from collections.abc import ItemsView, KeysView, Mapping
from bidict import bidict, OrderedBidict, frozenbidict, FrozenOrderedBidict, namedbidict
MyNamedBidict = namedbidict('MyNamedBidict', 'key', 'val')
MyNamedFrozenBidict = namedbidict('MyNamedFrozenBidict', 'key', 'val', base_type=frozenbidict)
MyNamedOrderedBidict = namedbidict('MyNamedOrderedBidict', 'key', 'val', base_type=OrderedBidict)
MUTABLE_BIDICT_TYPES = (bidict, OrderedBidict, MyNamedBidict)
FROZEN_BIDICT_TYPES = (frozenbidict, FrozenOrderedBidict, MyNamedFrozenBidict)
ORDERED_BIDICT_TYPES = (OrderedBidict, FrozenOrderedBidict, MyNamedOrderedBidict)
BIDICT_TYPES = tuple(set(MUTABLE_BIDICT_TYPES + FROZEN_BIDICT_TYPES + ORDERED_BIDICT_TYPES))
class _FrozenDict(KeysView, Mapping):
def __init__(self, *args, **kw): # pylint: disable=super-init-not-called
self._mapping = dict(*args, **kw)
def __getitem__(self, key):
return self._mapping[key]
def __hash__(self):
return ItemsView(self._mapping)._hash() # pylint: disable=protected-access
NON_BIDICT_MAPPING_TYPES = (dict, OrderedDict, _FrozenDict)
pass
# pylint: disable=C0103
items = [('a', 1), ('b', 2)] # use int values so makes sense with Counter
itemsreversed = list(reversed(items))
bidict_items = bidict(items)
frozenbidict_items = FrozenBidict(items)
namedbidict_items = namedbidict('named', 'keys', 'vals')(items)
orderedbidict_items = OrderedBidict(items)
orderedbidict_itemsreversed = OrderedBidict(itemsreversed)
orderedbidictsubcls_items = OrderedBidictSubcls(items)
orderedbidictsubcls_itemsreversed = OrderedBidictSubcls(itemsreversed)
frozenorderedbidict_items = FrozenOrderedBidict(items)
frozenorderedbidict_itemsreversed = FrozenOrderedBidict(itemsreversed)
bidicts = (
bidict_items,
frozenbidict_items,
namedbidict_items,
orderedbidict_items,
orderedbidict_itemsreversed,
orderedbidictsubcls_items,
orderedbidictsubcls_itemsreversed,
frozenorderedbidict_items,
frozenorderedbidict_itemsreversed,
)
dict_itemsreversed = dict(itemsreversed)
counter_itemsreversed = Counter(dict_itemsreversed)
defaultdict_itemsreversed = defaultdict(lambda x: None, itemsreversed)
dictsubcls_itemsreversed = DictSubcls(itemsreversed)
class _OrderedBidictSubcls(OrderedBidict):
pass
ITEMS = [('a', 1), ('b', 2)] # use int values so we can use with Counter below
ITEMS_REV = list(reversed(ITEMS))
BIDICT = bidict(ITEMS)
FROZEN = frozenbidict(ITEMS)
NAMED = namedbidict('named', 'keys', 'vals')(ITEMS)
ORDERED = OrderedBidict(ITEMS)
ORDERED_REV = OrderedBidict(ITEMS_REV)
ORDERED_SUB = _OrderedBidictSubcls(ITEMS)
ORDERED_SUB_REV = _OrderedBidictSubcls(ITEMS_REV)
FROZEN_ORDERED = FrozenOrderedBidict(ITEMS)
FROZEN_ORDERED_REV = FrozenOrderedBidict(ITEMS_REV)
BIDICTS = (
BIDICT,
FROZEN,
NAMED,
ORDERED,
ORDERED_REV,
ORDERED_SUB,
ORDERED_SUB_REV,
FROZEN_ORDERED,
FROZEN_ORDERED_REV,
)
DICT_REV = dict(ITEMS_REV)
COUNTER_REV = Counter(DICT_REV)
DEFAULTDICT_REV = defaultdict(lambda x: None, ITEMS_REV)
class _OrderedBidictSubcls(OrderedBidict):
pass
ITEMS = [('a', 1), ('b', 2)] # use int values so we can use with Counter below
ITEMS_REV = list(reversed(ITEMS))
BIDICT = bidict(ITEMS)
FROZEN = frozenbidict(ITEMS)
NAMED = namedbidict('named', 'keys', 'vals')(ITEMS)
ORDERED = OrderedBidict(ITEMS)
ORDERED_REV = OrderedBidict(ITEMS_REV)
ORDERED_SUB = _OrderedBidictSubcls(ITEMS)
ORDERED_SUB_REV = _OrderedBidictSubcls(ITEMS_REV)
FROZEN_ORDERED = FrozenOrderedBidict(ITEMS)
FROZEN_ORDERED_REV = FrozenOrderedBidict(ITEMS_REV)
BIDICTS = (
BIDICT,
FROZEN,
NAMED,
ORDERED,
ORDERED_REV,
ORDERED_SUB,
ORDERED_SUB_REV,
FROZEN_ORDERED,
FROZEN_ORDERED_REV,
)
DICT_REV = dict(ITEMS_REV)
COUNTER_REV = Counter(DICT_REV)
DEFAULTDICT_REV = defaultdict(lambda x: None, ITEMS_REV)
DICT_SUB_REV = _DictSubcls(ITEMS_REV)
The relationships tested here are not guaranteed to hold in the future,
but are still tested so that any unintentional changes won't go unnoticed.
"""
assert not issubclass(bidict, FrozenOrderedBidict)
assert not issubclass(bidict, OrderedBidict)
assert not issubclass(bidict, frozenbidict)
assert not issubclass(FrozenOrderedBidict, OrderedBidict)
assert not issubclass(FrozenOrderedBidict, bidict)
assert not issubclass(FrozenOrderedBidict, frozenbidict)
assert not issubclass(OrderedBidict, FrozenOrderedBidict)
assert not issubclass(OrderedBidict, bidict)
assert not issubclass(OrderedBidict, frozenbidict)
assert not issubclass(frozenbidict, FrozenOrderedBidict)
assert not issubclass(frozenbidict, OrderedBidict)
assert not issubclass(frozenbidict, bidict)