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

typish.functions._instance_of   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 96.15 %

Importance

Changes 0
Metric Value
eloc 16
dl 25
loc 26
rs 10
c 0
b 0
f 0
wmc 4

1 Function

Rating   Name   Duplication   Size   Complexity  
A instance_of() 25 25 4

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 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