Conditions | 4 |
Total Lines | 19 |
Code Lines | 10 |
Lines | 19 |
Ratio | 100 % |
Changes | 0 |
1 | import typing |
||
9 | View Code Duplication | @lru_cache() |
|
|
|||
10 | def get_alias(cls: T) -> typing.Optional[T]: |
||
11 | """ |
||
12 | Return the alias from the ``typing`` module for ``cls``. For example, for |
||
13 | ``cls=list``, the result would be ``typing.List``. If ``cls`` is |
||
14 | parameterized (>=3.9), then a parameterized ``typing`` equivalent is |
||
15 | returned. If no alias exists for ``cls``, then ``None`` is returned. |
||
16 | If ``cls`` already is from ``typing`` it is returned as is. |
||
17 | :param cls: the type for which the ``typing`` equivalent is to be found. |
||
18 | :return: the alias from ``typing``. |
||
19 | """ |
||
20 | if is_from_typing(cls): |
||
21 | return cls |
||
22 | alias = _alias_per_type.get(cls.__name__, None) |
||
23 | if alias: |
||
24 | args = getattr(cls, '__args__', tuple()) |
||
25 | if args: |
||
26 | alias = alias[args] |
||
27 | return alias |
||
28 | |||
41 |