Passed
Pull Request — master (#12)
by Ramon
02:10 queued 36s
created

typish.functions._instance_of   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 96 %

Importance

Changes 0
Metric Value
eloc 15
dl 24
loc 25
rs 10
c 0
b 0
f 0
wmc 4

1 Function

Rating   Name   Duplication   Size   Complexity  
A instance_of() 24 24 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 Literal, LiteralAlias
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 issubclass(args[0], Literal):
19
        leftovers = args[1:]
20
        return (LiteralAlias.__instancecheck__(args[0], obj)
21
                and (not leftovers or instance_of(obj, leftovers)))
22
23
    type_ = get_type(obj, use_union=True)
24
    return subclass_of(type_, *args)
25