summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2020-12-19 00:53:50 (GMT)
committerGitHub <noreply@github.com>2020-12-19 00:53:50 (GMT)
commit51f4688254ebb7b30215de424360ba5c92c63fe8 (patch)
tree1e7d9be57f2092072f97074f363052c75199f8b7
parentfb34096140bbb74c81500dd8bbc3c69c1d24d9ab (diff)
downloadcpython-51f4688254ebb7b30215de424360ba5c92c63fe8.zip
cpython-51f4688254ebb7b30215de424360ba5c92c63fe8.tar.gz
cpython-51f4688254ebb7b30215de424360ba5c92c63fe8.tar.bz2
bpo-34805: Guarantee that __subclasses__() is in definition order. (GH-23844)
-rw-r--r--Doc/library/stdtypes.rst4
-rw-r--r--Lib/test/test_descr.py10
2 files changed, 12 insertions, 2 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index e48f67b..2869378 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 307416c..f0048f4 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -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):