typish.functions._get_origin   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 43.59 %

Importance

Changes 0
Metric Value
eloc 26
dl 17
loc 39
rs 10
c 0
b 0
f 0
wmc 4

1 Function

Rating   Name   Duplication   Size   Complexity  
A get_origin() 17 17 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
import typing
2
from collections import deque, defaultdict
3
from collections.abc import Set
4
from inspect import isclass
5
6
from typish.functions._is_from_typing import is_from_typing
7
8
9 View Code Duplication
def get_origin(t: type) -> type:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
10
    """
11
    Return the origin of the given (generic) type. For example, for
12
    ``t=List[str]``, the result would be ``list``.
13
    :param t: the type of which the origin is to be found.
14
    :return: the origin of ``t`` or ``t`` if it is not generic.
15
    """
16
    from typish.functions._get_simple_name import get_simple_name
17
18
    simple_name = get_simple_name(t)
19
    result = _type_per_alias.get(simple_name, None)
20
    if isclass(t) and not is_from_typing(t):
21
        # Get the origin in case of a parameterized generic.
22
        result = getattr(t, '__origin__', t)
23
    elif not result:
24
        result = getattr(typing, simple_name, t)
25
    return result
26
27
28
_type_per_alias = {
29
    'List': list,
30
    'Tuple': tuple,
31
    'Dict': dict,
32
    'Set': set,
33
    'FrozenSet': frozenset,
34
    'Deque': deque,
35
    'DefaultDict': defaultdict,
36
    'Type': type,
37
    'AbstractSet': Set,
38
    'Optional': typing.Union,
39
}
40