Passed
Push — master ( c3c399...6aa0ae )
by Ramon
55s queued 10s
created

typish.functions._instance_of.instance_of()   A

Complexity

Conditions 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nop 2
dl 25
loc 25
rs 9.65
c 0
b 0
f 0
1 View Code Duplication
def instance_of(obj: object, *args: type) -> bool:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
    """
3
    Check whether ``obj`` is an instance of all types in ``args``, while also
4
    considering generics.
5
    :param obj: the object in subject.
6
    :param args: the type(s) of which ``obj`` is an instance or not.
7
    :return: ``True`` if ``obj`` is an instance of all types in ``args``.
8
    """
9
    from typish.classes._literal import LiteralAlias, is_literal_type
10
    from typish.functions._subclass_of import subclass_of
11
    from typish.functions._get_type import get_type
12
13
    try:
14
        return all(isinstance(obj, arg) for arg in args)
15
    except Exception:
16
        ...  # If the regular check didn't work, continue below.
17
18
    if args and is_literal_type(args[0]):
19
        alias = LiteralAlias.from_literal(args[0])
20
        leftovers = args[1:]
21
        return (isinstance(obj, alias)
22
                and (not leftovers or instance_of(obj, leftovers)))
23
24
    type_ = get_type(obj, use_union=True)
25
    return subclass_of(type_, *args)
26