]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-34805: Guarantee that __subclasses__() is in definition order. (GH-23844)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Sat, 19 Dec 2020 00:53:50 +0000 (16:53 -0800)
committerGitHub <noreply@github.com>
Sat, 19 Dec 2020 00:53:50 +0000 (16:53 -0800)
Doc/library/stdtypes.rst
Lib/test/test_descr.py

index e48f67bfb96367815c5a9874750e483c0d0d7be3..2869378bbdaf0e66927fa79b9eec8e211f943c14 100644 (file)
@@ -5353,8 +5353,8 @@ types, where they are relevant.  Some of these are not reported by the
 .. method:: class.__subclasses__
 
    Each class keeps a list of weak references to its immediate subclasses.  This
-   method returns a list of all those references still alive.
-   Example::
+   method returns a list of all those references still alive.  The list is in
+   definition order.  Example::
 
       >>> int.__subclasses__()
       [<class 'bool'>]
index 307416c3300ae306ab6cf89562a5c0d6f8c85952..f0048f42f882b0d3c6de2d3c0ae13b8e8156cc5a 100644 (file)
@@ -4,6 +4,8 @@ import gc
 import itertools
 import math
 import pickle
+import random
+import string
 import sys
 import types
 import unittest
@@ -845,6 +847,14 @@ class ClassPropertiesAndMethods(unittest.TestCase):
             self.fail("inheriting from ModuleType and str at the same time "
                       "should fail")
 
+        # Issue 34805: Verify that definition order is retained
+        def random_name():
+            return ''.join(random.choices(string.ascii_letters, k=10))
+        class A:
+            pass
+        subclasses = [type(random_name(), (A,), {}) for i in range(100)]
+        self.assertEqual(A.__subclasses__(), subclasses)
+
     def test_multiple_inheritance(self):
         # Testing multiple inheritance...
         class C(object):