bidict/tests/test_subclasshook.py

39 lines
1.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2018-01-01 03:51:19 +00:00
# Copyright 2018 Joshua Bronson. All Rights Reserved.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Test that if foreign code provides a class that conforms to
BidirectionalMapping's interface, it is automatically a subclass.
"""
from bidict import BidirectionalMapping
class MyBidirectionalMapping(dict):
"""Dummy type implementing the BidirectionalMapping interface."""
def __inverted__(self):
for (key, val) in self.items():
yield (val, key)
@property
def inv(self):
"""Like :attr:`bidict.bidict.inv`."""
return MyBidirectionalMapping(self.__inverted__())
class OldStyleClass: # pylint: disable=old-style-class,no-init
"""In Python 2 this is an old-style class (not derived from object)."""
2017-03-13 17:22:56 +00:00
def test_bidi_mapping_subclasshook():
"""Ensure issubclass(foo, BidirectionalMapping) works as expected."""
assert issubclass(MyBidirectionalMapping, BidirectionalMapping)
assert not issubclass(dict, BidirectionalMapping)
# Make sure this works with old-style classes as expected.
assert not issubclass(OldStyleClass, BidirectionalMapping)