typish.functions._get_mro   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 84.38 %

Importance

Changes 0
Metric Value
eloc 19
dl 27
loc 32
rs 10
c 0
b 0
f 0
wmc 5

1 Function

Rating   Name   Duplication   Size   Complexity  
A get_mro() 27 27 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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