Получить типизацию. Список [X] - PullRequest
0 голосов
/ 15 ноября 2018

Для следующего набора:

import typing
foo: typing.List[int] = []

Как получить тип элементов в списке foo (через подсказку типа)?Результат должен быть <class 'int'>.

1 Ответ

0 голосов
/ 15 ноября 2018

Не уверен, если вы ищете что-то подобное.

import sys
import typing

foo: typing.List[int] = []
foo_one: typing.Tuple[float] = ()

def _get_type_of_item(param):
    """
    typing.get_type_hints(obj[, globals[, locals]]):

    Return a dictionary containing type hints for a function, method, module or class object.
    """
    return typing.get_type_hints(sys.modules[__name__])[param].__args__


print(_get_type_of_item('foo')) # this will print (<class 'int'>,)
print(_get_type_of_item('foo_one')) # this will print (<class 'float'>,)
...