annotate Lib/test/test_typechecks.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 6ffcdafa0118
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Unit tests for __instancecheck__ and __subclasscheck__."""
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 import unittest
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 from test import test_support
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 class ABC(type):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 def __instancecheck__(cls, inst):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 """Implement isinstance(inst, cls)."""
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 return any(cls.__subclasscheck__(c)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 for c in set([type(inst), inst.__class__]))
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 def __subclasscheck__(cls, sub):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 """Implement issubclass(sub, cls)."""
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 candidates = cls.__dict__.get("__subclass__", set()) | set([cls])
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 return any(c in candidates for c in sub.mro())
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 class Integer:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 __metaclass__ = ABC
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 __subclass__ = set([int])
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 class SubInt(Integer):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 class TypeChecksTest(unittest.TestCase):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 def testIsSubclassInternal(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 self.assertEqual(Integer.__subclasscheck__(int), True)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 self.assertEqual(Integer.__subclasscheck__(float), False)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 def testIsSubclassBuiltin(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 self.assertEqual(issubclass(int, Integer), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
40 self.assertEqual(issubclass(int, (Integer,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 self.assertEqual(issubclass(float, Integer), False)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
42 self.assertEqual(issubclass(float, (Integer,)), False)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 def testIsInstanceBuiltin(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 self.assertEqual(isinstance(42, Integer), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
46 self.assertEqual(isinstance(42, (Integer,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 self.assertEqual(isinstance(3.14, Integer), False)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
48 self.assertEqual(isinstance(3.14, (Integer,)), False)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 def testIsInstanceActual(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 self.assertEqual(isinstance(Integer(), Integer), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
52 self.assertEqual(isinstance(Integer(), (Integer,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 def testIsSubclassActual(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 self.assertEqual(issubclass(Integer, Integer), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
56 self.assertEqual(issubclass(Integer, (Integer,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def testSubclassBehavior(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 self.assertEqual(issubclass(SubInt, Integer), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
60 self.assertEqual(issubclass(SubInt, (Integer,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 self.assertEqual(issubclass(SubInt, SubInt), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
62 self.assertEqual(issubclass(SubInt, (SubInt,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 self.assertEqual(issubclass(Integer, SubInt), False)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
64 self.assertEqual(issubclass(Integer, (SubInt,)), False)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 self.assertEqual(issubclass(int, SubInt), False)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
66 self.assertEqual(issubclass(int, (SubInt,)), False)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 self.assertEqual(isinstance(SubInt(), Integer), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
68 self.assertEqual(isinstance(SubInt(), (Integer,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 self.assertEqual(isinstance(SubInt(), SubInt), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
70 self.assertEqual(isinstance(SubInt(), (SubInt,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 self.assertEqual(isinstance(42, SubInt), False)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 43327
diff changeset
72 self.assertEqual(isinstance(42, (SubInt,)), False)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
54121
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
74 def test_oldstyle(self):
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
75 # These should just be ignored.
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
76 class X:
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
77 def __instancecheck__(self, inst):
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
78 return True
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
79 def __subclasscheck__(self, cls):
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
80 return True
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
81 class Sub(X): pass
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 54121
diff changeset
82 self.assertNotIsInstance(3, X)
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 54121
diff changeset
83 self.assertIsInstance(X(), X)
54121
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
84 self.assertFalse(issubclass(int, X))
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
85 self.assertTrue(issubclass(Sub, X))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def test_main():
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 test_support.run_unittest(TypeChecksTest)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 if __name__ == "__main__":
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 unittest.main()