Exemplo n.º 1
0
    def test_get_type_deep(self):
        self.assertEqual(List[List[int]], get_type([[1, 2], [3]]))
        self.assertEqual(List[List[object]], get_type([[1, 2], ['3']]))

        deep = [[[1, 2]], [[3]], [[4], [5, '6']]]

        self.assertEqual(List[List[List[object]]], get_type(deep))
        self.assertEqual(Dict[str, List[List[List[object]]]],
                         get_type({'a': deep}))
Exemplo n.º 2
0
 def like(obj: Any, exclude_privates: bool = True) -> 'Something':
     """
     Return a ``Something`` for the given ``obj``.
     :param obj: the object of which a ``Something`` is to be made.
     :param exclude_privates: if ``True``, private variables are excluded.
     :return: a ``Something`` that corresponds to ``obj``.
     """
     signature = {attr: get_type(getattr(obj, attr)) for attr in dir(obj)
                  if not exclude_privates or not attr.startswith('_')}
     return Something[signature]
Exemplo n.º 3
0
 def __getitem__(self, item: Any) -> Any:
     """
     Return the value of the first encounter of a key for which
     ``is_instance(item, key)`` holds ``True``.
     :param item: any item.
     :return: the value of which the type corresponds with item.
     """
     item_type = get_type(item, use_union=True)
     for key, value in self.items():
         if subclass_of(item_type, key):
             return value
     raise KeyError('No match for {}'.format(item))
Exemplo n.º 4
0
    def test_get_type_callable(self):
        def func1(x: int, y: str) -> object:
            pass

        def func2() -> int:
            pass

        def func3(x: int):
            pass

        def func4():
            pass

        def func5(x):
            pass

        def func6(l: List[List[List[int]]]):
            pass

        self.assertEqual(Callable[[int, str], object], get_type((func1)))
        self.assertEqual(Callable[[], int], get_type(func2))
        self.assertEqual(Callable[[int], NoneType], get_type(func3))
        self.assertEqual(Callable, get_type(func4))
        self.assertEqual(Callable[[Any], NoneType], get_type(func5))
        self.assertEqual(Callable[[List[List[List[int]]]], NoneType],
                         get_type(func6))
Exemplo n.º 5
0
    def test_get_type_some_class(self):
        class SomeRandomClass:
            pass

        self.assertEqual(SomeRandomClass, get_type(SomeRandomClass()))
Exemplo n.º 6
0
 def test_get_type_type(self):
     self.assertEqual(Type[int], get_type(int))
Exemplo n.º 7
0
 def test_get_type_list(self):
     self.assertEqual(List[int], get_type([1, 2, 3]))
     self.assertEqual(List[object], get_type([1, 2, '3']))
     self.assertEqual(List[Unknown], get_type([]))
Exemplo n.º 8
0
 def test_get_type_lambda(self):
     self.assertEqual(Callable[[Unknown, Unknown], Unknown],
                      get_type(lambda x, y: 42))
     self.assertEqual(Callable[[], Unknown], get_type(lambda: 42))
Exemplo n.º 9
0
    def test_get_type_async(self):
        async def func(x: int) -> str:
            return '42'

        self.assertEqual(Callable[[int], Awaitable[str]], get_type(func))
Exemplo n.º 10
0
 def test_get_type_tuple(self):
     self.assertEqual(Tuple[int, int, int], get_type((1, 2, 3)))
     self.assertEqual(Tuple[int, str, str], get_type((1, '2', '3')))
Exemplo n.º 11
0
 def test_get_type_dict(self):
     self.assertEqual(Dict[str, int], get_type({'a': 1, 'b': 2}))
     self.assertEqual(Dict[int, object], get_type({1: 'a', 2: 2}))
     self.assertEqual(Dict[Unknown, Unknown], get_type({}))
Exemplo n.º 12
0
 def test_get_type_set(self):
     self.assertEqual(Set[int], get_type({1, 2, 3}))
     self.assertEqual(Set[object], get_type({1, 2, '3'}))
     self.assertEqual(Set[Unknown], get_type(set([])))