typish.functions._common_ancestor   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 37
Duplicated Lines 40.54 %

Importance

Changes 0
Metric Value
eloc 18
dl 15
loc 37
rs 10
c 0
b 0
f 0
wmc 10

3 Functions

Rating   Name   Duplication   Size   Complexity  
A common_ancestor_of_types() 0 7 1
A common_ancestor() 0 7 1
B _common_ancestor() 15 15 8

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
3
4
def common_ancestor(*args: object) -> type:
5
    """
6
    Get the closest common ancestor of the given objects.
7
    :param args: any objects.
8
    :return: the ``type`` of the closest common ancestor of the given ``args``.
9
    """
10
    return _common_ancestor(args, False)
11
12
13
def common_ancestor_of_types(*args: type) -> type:
14
    """
15
    Get the closest common ancestor of the given classes.
16
    :param args: any classes.
17
    :return: the ``type`` of the closest common ancestor of the given ``args``.
18
    """
19
    return _common_ancestor(args, True)
20
21
22 View Code Duplication
def _common_ancestor(args: typing.Sequence[object], types: bool) -> type:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
23
    from typish.functions._get_type import get_type
24
    from typish.functions._get_mro import get_mro
25
26
    if len(args) < 1:
27
        raise TypeError('common_ancestor() requires at least 1 argument')
28
    tmap = (lambda x: x) if types else get_type
29
    mros = [get_mro(tmap(elem)) for elem in args]
30
    for cls in mros[0]:
31
        for mro in mros:
32
            if cls not in mro:
33
                break
34
        else:
35
            # cls is in every mro; a common ancestor is found!
36
            return cls
37