1 | n/a | """Unit tests for __instancecheck__ and __subclasscheck__.""" |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | |
---|
5 | n/a | |
---|
6 | n/a | class ABC(type): |
---|
7 | n/a | |
---|
8 | n/a | def __instancecheck__(cls, inst): |
---|
9 | n/a | """Implement isinstance(inst, cls).""" |
---|
10 | n/a | return any(cls.__subclasscheck__(c) |
---|
11 | n/a | for c in {type(inst), inst.__class__}) |
---|
12 | n/a | |
---|
13 | n/a | def __subclasscheck__(cls, sub): |
---|
14 | n/a | """Implement issubclass(sub, cls).""" |
---|
15 | n/a | candidates = cls.__dict__.get("__subclass__", set()) | {cls} |
---|
16 | n/a | return any(c in candidates for c in sub.mro()) |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | class Integer(metaclass=ABC): |
---|
20 | n/a | __subclass__ = {int} |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | class SubInt(Integer): |
---|
24 | n/a | pass |
---|
25 | n/a | |
---|
26 | n/a | |
---|
27 | n/a | class TypeChecksTest(unittest.TestCase): |
---|
28 | n/a | |
---|
29 | n/a | def testIsSubclassInternal(self): |
---|
30 | n/a | self.assertEqual(Integer.__subclasscheck__(int), True) |
---|
31 | n/a | self.assertEqual(Integer.__subclasscheck__(float), False) |
---|
32 | n/a | |
---|
33 | n/a | def testIsSubclassBuiltin(self): |
---|
34 | n/a | self.assertEqual(issubclass(int, Integer), True) |
---|
35 | n/a | self.assertEqual(issubclass(int, (Integer,)), True) |
---|
36 | n/a | self.assertEqual(issubclass(float, Integer), False) |
---|
37 | n/a | self.assertEqual(issubclass(float, (Integer,)), False) |
---|
38 | n/a | |
---|
39 | n/a | def testIsInstanceBuiltin(self): |
---|
40 | n/a | self.assertEqual(isinstance(42, Integer), True) |
---|
41 | n/a | self.assertEqual(isinstance(42, (Integer,)), True) |
---|
42 | n/a | self.assertEqual(isinstance(3.14, Integer), False) |
---|
43 | n/a | self.assertEqual(isinstance(3.14, (Integer,)), False) |
---|
44 | n/a | |
---|
45 | n/a | def testIsInstanceActual(self): |
---|
46 | n/a | self.assertEqual(isinstance(Integer(), Integer), True) |
---|
47 | n/a | self.assertEqual(isinstance(Integer(), (Integer,)), True) |
---|
48 | n/a | |
---|
49 | n/a | def testIsSubclassActual(self): |
---|
50 | n/a | self.assertEqual(issubclass(Integer, Integer), True) |
---|
51 | n/a | self.assertEqual(issubclass(Integer, (Integer,)), True) |
---|
52 | n/a | |
---|
53 | n/a | def testSubclassBehavior(self): |
---|
54 | n/a | self.assertEqual(issubclass(SubInt, Integer), True) |
---|
55 | n/a | self.assertEqual(issubclass(SubInt, (Integer,)), True) |
---|
56 | n/a | self.assertEqual(issubclass(SubInt, SubInt), True) |
---|
57 | n/a | self.assertEqual(issubclass(SubInt, (SubInt,)), True) |
---|
58 | n/a | self.assertEqual(issubclass(Integer, SubInt), False) |
---|
59 | n/a | self.assertEqual(issubclass(Integer, (SubInt,)), False) |
---|
60 | n/a | self.assertEqual(issubclass(int, SubInt), False) |
---|
61 | n/a | self.assertEqual(issubclass(int, (SubInt,)), False) |
---|
62 | n/a | self.assertEqual(isinstance(SubInt(), Integer), True) |
---|
63 | n/a | self.assertEqual(isinstance(SubInt(), (Integer,)), True) |
---|
64 | n/a | self.assertEqual(isinstance(SubInt(), SubInt), True) |
---|
65 | n/a | self.assertEqual(isinstance(SubInt(), (SubInt,)), True) |
---|
66 | n/a | self.assertEqual(isinstance(42, SubInt), False) |
---|
67 | n/a | self.assertEqual(isinstance(42, (SubInt,)), False) |
---|
68 | n/a | |
---|
69 | n/a | |
---|
70 | n/a | if __name__ == "__main__": |
---|
71 | n/a | unittest.main() |
---|