Total Complexity | 4 |
Total Lines | 26 |
Duplicated Lines | 96.15 % |
Changes | 0 |
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: |
|
|
|||
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 |