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

typish.functions._get_alias   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A get_alias() 0 10 1
1
import typing
2
from functools import lru_cache
3
4
from typish._types import T
5
6
7
@lru_cache()
8
def get_alias(cls: T) -> typing.Optional[T]:
9
    """
10
    Return the alias from the ``typing`` module for ``cls``. For example, for
11
    ``cls=list``, the result would be ``typing.List``. If no alias exists for
12
    ``cls``, then ``None`` is returned.
13
    :param cls: the type for which the ``typing`` equivalent is to be found.
14
    :return: the alias from ``typing``.
15
    """
16
    return _alias_per_type.get(cls.__name__, None)
17
18
19
_alias_per_type = {
20
    'list': typing.List,
21
    'tuple': typing.Tuple,
22
    'dict': typing.Dict,
23
    'set': typing.Set,
24
    'frozenset': typing.FrozenSet,
25
    'deque': typing.Deque,
26
    'defaultdict': typing.DefaultDict,
27
    'type': typing.Type,
28
    'Set': typing.AbstractSet,
29
}
30