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

typish.functions._get_alias.get_alias()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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