]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-34805: Guarantee that __subclasses__() is in definition order. (GH-23844) (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 19 Dec 2020 01:17:32 +0000 (17:17 -0800)
committerGitHub <noreply@github.com>
Sat, 19 Dec 2020 01:17:32 +0000 (17:17 -0800)
Doc/library/stdtypes.rst
Lib/test/test_descr.py

index a48cfa1327791027f80cfed07633faa6916cae60..59200730a48f5d01b88ec210665a56d180f00ad6 100644 (file)
@@ -5196,8 +5196,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 9e875da37508382ace354031f718be38e76c0d0b..c7a191bccd69c40256dd88dfd3681bd5a4cda979 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):