Title: inspect: add tools for inspecting subclasses
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: iomintz, petr.viktorin, yselivanov
Priority: normal Keywords: patch

Created on 2018-09-11 21:35 by iomintz, last changed 2018-09-14 03:46 by iomintz. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9186 closed iomintz, 2018-09-11 21:36
Messages (4)
msg325068 - (view) Author: Io Mintz (iomintz) * Date: 2018-09-11 21:35
Adds inspect.getsubclasses, inspect.getallsubclasses (recursive), and inspect.getsubclasstree (recursive, maintains structure).

See documentation for more details.
msg325298 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2018-09-13 22:04
What's the use case for these?
msg325317 - (view) Author: Io Mintz (iomintz) * Date: 2018-09-14 03:09
I didn't have one in mind. I just noticed that there was a getmro which accessed __mro__ but no corresponding method for __subclasses__.
That got me getsubclasses and getallsubclasses.

Then I thought it would be cool if you could walk the tree of subclasses in code, so I wrote getsubclasstree.
msg325318 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2018-09-14 03:34
__subclasses__ is, as Tim Peters put it [0], "just Python exposing an
internal mechanism for the morbidly curious".
I'd expect that not all alternative implementations of Python have something like __subclasses__ -- it's a detail of the implementation (as opposed to the MRO, which is something any implementation of the Python language needs to be able to compute).
It certainly is cool functionality, but keep in mind we'd have to support it for decades, and it would put pressure on other Python implementations to add something like __subclasses__.

With that in mind, and because there's no real use case, I think it's better to not add this to inspect.

Date User Action Args
2018-09-14 03:46:55iomintzsetstatus: open -> closed
stage: patch review -> resolved
2018-09-14 03:34:53petr.viktorinsetmessages: + msg325318
2018-09-14 03:09:38iomintzsetmessages: + msg325317
2018-09-13 22:04:04petr.viktorinsetnosy: + petr.viktorin
messages: + msg325298
2018-09-11 21:38:49zach.waresetnosy: + yselivanov
2018-09-11 21:36:23iomintzsetkeywords: + patch
stage: patch review
pull_requests: + pull_request8623
2018-09-11 21:35:42iomintzcreate