[Python-checkins] bpo-34805: Guarantee that __subclasses__() is in definition order. (GH-23844)

rhettinger webhook-mailer at python.org
Fri Dec 18 19:54:10 EST 2020


https://github.com/python/cpython/commit/51f4688254ebb7b30215de424360ba5c92c63fe8
commit: 51f4688254ebb7b30215de424360ba5c92c63fe8
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2020-12-18T16:53:50-08:00
summary:

bpo-34805:  Guarantee that __subclasses__() is in definition order. (GH-23844)

files:
M Doc/library/stdtypes.rst
M Lib/test/test_descr.py

diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index e48f67bfb9636..2869378bbdaf0 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -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'>]
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 307416c3300ae..f0048f42f882b 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -4,6 +4,8 @@
 import itertools
 import math
 import pickle
+import random
+import string
 import sys
 import types
 import unittest
@@ -845,6 +847,14 @@ class Module(types.ModuleType, str):
             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):



More information about the Python-checkins mailing list