Exemple #1
0
    def is_valid(self) -> bool:
        if not self.is_supported:
            return False
        # value does not match alias type
        if self.value is not self.hint.origin and not isinstance(
                self.value, self.valid_types):
            return False
        # Alias inconsitently returns TypeVar object or None when no parameters given ... stupid python
        # will not validate recursively, still value does match alias type though
        if len(self.hint.types) == 0 or isinstance(self.hint.types[0],
                                                   TypeVar):
            return True

        # validate keys
        hint = TypeHint(self.hint.types[0])
        nullablevalidator = NullableValidator(hint)
        stdtypevalidator = StdtypeValidator(hint)
        unionvalidator = UnionValidator(hint)
        for value in self.value.keys():
            if not (nullablevalidator.validate(value)
                    or stdtypevalidator.validate(value)
                    or unionvalidator.validate(value)):
                return False

        # validate values
        hint = TypeHint(self.hint.types[1])
        # Any is always valid
        if AnyValidator().supports(hint):
            return True
        nullablevalidator = NullableValidator(hint)
        stdtypevalidator = StdtypeValidator(hint)
        classvalidator = ClassValidator(hint)
        callablevalidator = CallableValidator(hint)
        aliasvalidator = AliasValidator(hint)
        listvalidator = ListValidator(hint)
        dictvalidator = DictValidator(hint)
        unionvalidator = UnionValidator(hint)
        for value in self.value.values():
            if not (nullablevalidator.validate(value)
                    or stdtypevalidator.validate(value)
                    or classvalidator.validate(value)
                    or callablevalidator.validate(value)
                    or aliasvalidator.validate(value)
                    or listvalidator.validate(value)
                    or dictvalidator.validate(value)
                    or unionvalidator.validate(value)):
                return False

        return True
Exemple #2
0
 def test_alias_validator_invalid_value_should_return_false(self):
     """==> alias validator invalid value should return False"""
     for item in self.alias_no_argument_fixtures:
         # seems like every object is hashable ... stupid python
         if item.get('alias') != 'Hashable':
             hint = TypeHint(item.get('annotation'))
             validator = AliasValidator(hint, None)
             self.assertFalse(validator)
Exemple #3
0
 def is_valid(self) -> bool:
     if self.is_supported:
         for annotation in self.hint.types:
             hint = TypeHint(annotation)
             if AnyValidator(hint, self.value) or \
                     NullableValidator(hint, self.value) or \
                     StdtypeValidator(hint, self.value) or \
                     ClassValidator(hint, self.value) or \
                     CallableValidator(hint, self.value) or \
                     AliasValidator(hint, self.value) or \
                     ListValidator(hint, self.value) or \
                     DictValidator(hint, self.value) or \
                     UnionValidator(hint, self.value):
                 return True
     return False
Exemple #4
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)))
Exemple #5
0
 def test_alias_validator_valid_value_should_return_true(self):
     """==> alias validator valid value should return True"""
     for item in self.alias_no_argument_fixtures:
         hint = TypeHint(item.get('annotation'))
         validator = AliasValidator(hint, item.get('origin'))
         self.assertTrue(validator)
Exemple #6
0
 def test_alias_validator_invalid_value_should_return_false(self):
     """==> alias validator invalid value should return False"""
     for item in self.alias_fixtures:
         hint = TypeHint(item.get('annotation'))
         validator = AliasValidator(hint, None)
         self.assertFalse(validator)