def test_nested_any_in_dict_should_return_true(self):
     """==> nested any in dict should return True"""
     hint = TypeHint(typing.Dict[int, typing.Any])
     validator = DictValidator(hint, self.nested_union_expected_valid)
     self.assertTrue(validator)
     validator = DictValidator(hint, self.nested_union_expected_invalid)
     self.assertTrue(validator)
Example #2
0
def _type_check(function: Callable, fixed_kwargs: Dict[str, Any]) -> None:
    if not callable(function):
        raise TypeError('_type_check: function must be callable')
    if not isinstance(fixed_kwargs, dict):
        raise TypeError(
            '_type_check: fixed_kwargs must be set to a dictionary')
    # get type hints (typing.get_type_hints method requires to import unnecessary module)
    for arg_name, arg_hint in function.__annotations__.items():
        # ignore "return" (will raise "KeyError" when accessing "fixed_kwargs[arg_name]")
        if arg_name == 'return':
            continue
        hint = TypeHint(arg_hint)
        # there is no way to validate custom property
        if hint.category == 'property':
            continue
        value = fixed_kwargs[arg_name]
        if not (NullableValidator(hint, value) or StdtypeValidator(
                hint, value) or ClassValidator(hint, value)
                or CallableValidator(hint, value)
                or AliasValidator(hint, value) or ListValidator(hint, value)
                or DictValidator(hint, value) or UnionValidator(hint, value)):
            raise TypeError('{} must be set to {}, {} given'.format(
                function.__qualname__, hint.types, type(value)))
 def test_nested_union_should_return_true(self):
     """==> nested union should return True"""
     for item in self.nested_union_fixtures:
         hint = TypeHint(item.get('annotation'))
         validator = DictValidator(hint, self.nested_union_expected_valid)
         self.assertTrue(validator)
 def test_nested_dict_validator_should_return_true(self):
     """==> nested dict validator should return True"""
     for item in self.nested_dict_fixtures:
         self.assertTrue(
             DictValidator(TypeHint(item.get('annotation')),
                           self.nested_dict_expected_valid))
 def test_dict_validator_alias_with_no_parameter_should_return_true(self):
     """==> dict validator alias with no parameter should return True"""
     for item in self.dict_no_parameter_fixtures:
         hint = TypeHint(item.get('annotation'))
         validator = DictValidator(hint, self.dict_expected_valid)
         self.assertTrue(validator)
 def test_dict_validator_should_return_false(self):
     """==> dict validator should return false"""
     for item in self.dict_fixtures:
         self.assertFalse(
             DictValidator(TypeHint(item.get('annotation')),
                           self.dict_expected_invalid))