File size: 1,787 Bytes
065fee7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
"""
Routines for obtaining the class names
of an object and its parent classes.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, cast
from more_itertools import unique_everseen
if TYPE_CHECKING:
from collections.abc import Iterator
from typing import Any
def all_bases(c: type[object]) -> list[type[Any]]:
"""
return a tuple of all base classes the class c has as a parent.
>>> object in all_bases(list)
True
"""
return c.mro()[1:]
def all_classes(c: type[object]) -> list[type[Any]]:
"""
return a tuple of all classes to which c belongs
>>> list in all_classes(list)
True
"""
return c.mro()
# borrowed from
# http://code.activestate.com/recipes/576949-find-all-subclasses-of-a-given-class/
def iter_subclasses(cls: type[object]) -> Iterator[type[Any]]:
"""
Generator over all subclasses of a given class, in depth-first order.
>>> bool in list(iter_subclasses(int))
True
>>> class A(object): pass
>>> class B(A): pass
>>> class C(A): pass
>>> class D(B,C): pass
>>> class E(D): pass
>>>
>>> for cls in iter_subclasses(A):
... print(cls.__name__)
B
D
E
C
>>> # get ALL classes currently defined
>>> res = [cls.__name__ for cls in iter_subclasses(object)]
>>> 'type' in res
True
>>> 'tuple' in res
True
>>> len(res) > 100
True
"""
return unique_everseen(_iter_all_subclasses(cls))
def _iter_all_subclasses(cls: type[object]) -> Iterator[type[Any]]:
try:
subs = cls.__subclasses__()
except TypeError: # fails only when cls is type
subs = cast('type[type]', cls).__subclasses__(cls)
for sub in subs:
yield sub
yield from iter_subclasses(sub)
|