typish.functions._get_mro.get_mro()   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 27
Code Lines 15

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nop 1
dl 27
loc 27
rs 9.1832
c 0
b 0
f 0
1
import typing
2
from inspect import getmro
3
4
5 View Code Duplication
def get_mro(obj: typing.Any) -> typing.Tuple[type, ...]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
6
    """
7
    Return tuple of base classes (including that of obj) in method resolution
8
    order. Types from typing are supported as well.
9
    :param obj: object or type.
10
    :return: a tuple of base classes.
11
    """
12
    from typish.functions._get_origin import get_origin
13
14
    # Wrapper around ``getmro`` to allow types from ``typing``.
15
    if obj is ...:
16
        return Ellipsis, object
17
    elif obj is typing.Union:
18
        # For Python <3.7, we cannot use mro.
19
        super_cls = getattr(typing, '_GenericAlias',
20
                            getattr(typing, 'GenericMeta', None))
21
        return typing.Union, super_cls, object
22
23
    origin = get_origin(obj)
24
    if origin != obj:
25
        return get_mro(origin)
26
27
    cls = obj
28
    if not isinstance(obj, type):
29
        cls = type(obj)
30
31
    return getmro(cls)
32