From 782665885c983e88aac12f7e082485cac2df8007 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Fri, 18 Dec 2020 17:17:32 -0800 Subject: [PATCH] bpo-34805: Guarantee that __subclasses__() is in definition order. (GH-23844) (GH-23850) --- Doc/library/stdtypes.rst | 4 ++-- Lib/test/test_descr.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index a48cfa13277..59200730a48 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -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__() [] diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 9e875da3750..c7a191bccd6 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):