Skip to content

Commit 7826658

Browse files
bpo-34805: Guarantee that __subclasses__() is in definition order. (GH-23844) (GH-23850)
1 parent eef33e6 commit 7826658

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Doc/library/stdtypes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5196,8 +5196,8 @@ types, where they are relevant. Some of these are not reported by the
51965196
.. method:: class.__subclasses__
51975197

51985198
Each class keeps a list of weak references to its immediate subclasses. This
5199-
method returns a list of all those references still alive.
5200-
Example::
5199+
method returns a list of all those references still alive. The list is in
5200+
definition order. Example::
52015201

52025202
>>> int.__subclasses__()
52035203
[<class 'bool'>]

Lib/test/test_descr.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import itertools
55
import math
66
import pickle
7+
import random
8+
import string
79
import sys
810
import types
911
import unittest
@@ -845,6 +847,14 @@ class Module(types.ModuleType, str):
845847
self.fail("inheriting from ModuleType and str at the same time "
846848
"should fail")
847849

850+
# Issue 34805: Verify that definition order is retained
851+
def random_name():
852+
return ''.join(random.choices(string.ascii_letters, k=10))
853+
class A:
854+
pass
855+
subclasses = [type(random_name(), (A,), {}) for i in range(100)]
856+
self.assertEqual(A.__subclasses__(), subclasses)
857+
848858
def test_multiple_inheritance(self):
849859
# Testing multiple inheritance...
850860
class C(object):

0 commit comments

Comments
 (0)