Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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'>]
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import itertools
import math
import pickle
import random
import string
import sys
import types
import unittest
Expand Down Expand Up @@ -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):
Expand Down