common_ancestor()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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