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

This commit is contained in:
Raymond Hettinger 2020-12-18 16:53:50 -08:00 committed by GitHub
parent fb34096140
commit 51f4688254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -5353,8 +5353,8 @@ types, where they are relevant. Some of these are not reported by the
.. method:: class.__subclasses__ .. method:: class.__subclasses__
Each class keeps a list of weak references to its immediate subclasses. This Each class keeps a list of weak references to its immediate subclasses. This
method returns a list of all those references still alive. method returns a list of all those references still alive. The list is in
Example:: definition order. Example::
>>> int.__subclasses__() >>> int.__subclasses__()
[<class 'bool'>] [<class 'bool'>]

View File

@ -4,6 +4,8 @@ import gc
import itertools import itertools
import math import math
import pickle import pickle
import random
import string
import sys import sys
import types import types
import unittest import unittest
@ -845,6 +847,14 @@ class ClassPropertiesAndMethods(unittest.TestCase):
self.fail("inheriting from ModuleType and str at the same time " self.fail("inheriting from ModuleType and str at the same time "
"should fail") "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): def test_multiple_inheritance(self):
# Testing multiple inheritance... # Testing multiple inheritance...
class C(object): class C(object):