def test_dict(self) -> None: typ = self.hints['dict'] dgood = {'key': 22} dbad = {self.Blah(): 22} dempty: Dict[Any, Any] = {} self.assertFalse(is_type_instance(None, typ)) self.assertFalse(is_type_instance('nope', typ)) self.assertTrue(is_type_instance(dgood, typ)) self.assertTrue(is_type_instance(dempty, typ)) self.assertFalse(is_type_instance(dbad, typ))
def test_any(self) -> None: typ = self.hints['any'] #print() #print(typ) #print(get_origin(typ)) #print(get_args(typ)) #print() self.assertTrue(is_type_instance(None, typ)) self.assertTrue(is_type_instance('nope', typ)) self.assertTrue(is_type_instance(self.Blah, typ)) self.assertTrue(is_type_instance(int, typ))
def test_tuple_with_ellipsis(self) -> None: typ = self.hints['tupell'] self.assertFalse(is_type_instance(None, typ)) self.assertFalse(is_type_instance('nope', typ)) self.assertFalse(is_type_instance(22, typ)) self.assertTrue(is_type_instance((), typ)) self.assertFalse(is_type_instance(('nope',), typ)) self.assertFalse(is_type_instance(('yes', 22), typ)) self.assertTrue(is_type_instance((21,), typ)) self.assertTrue(is_type_instance((21, 22), typ)) self.assertTrue(is_type_instance((21, 22, 23), typ)) self.assertFalse(is_type_instance((21, 22, 23, 'nope'), typ))
def test_tuple(self) -> None: typ = self.hints['tup'] self.assertFalse(is_type_instance(None, typ)) self.assertFalse(is_type_instance('nope', typ)) self.assertFalse(is_type_instance(22, typ)) self.assertFalse(is_type_instance((), typ)) self.assertFalse(is_type_instance(('nope',), typ)) self.assertTrue(is_type_instance(('yes', 22), typ)) self.assertFalse(is_type_instance(('nope', 'nope'), typ)) self.assertFalse(is_type_instance((22, 'nope'), typ))
def test_type_with_parameter(self) -> None: typ = self.hints['type1'] self.assertFalse(is_type_instance(None, typ)) self.assertFalse(is_type_instance('nope', typ)) self.assertTrue(is_type_instance(self.Blah, typ)) self.assertTrue(is_type_instance(int, typ)) typ = self.hints['type2'] self.assertFalse(is_type_instance(None, typ)) self.assertFalse(is_type_instance('nope', typ)) self.assertTrue(is_type_instance(self.Blah, typ)) self.assertFalse(is_type_instance(int, typ))
def mk_func_args(cls, func: Callable, args: Args) -> Dict[str, Any]: d: Dict[str, Any] = {} args = as_argsmap(args) for param_name, param_type in cls.params_of(func): value = args.get(param_name, None) if not is_type_instance(value, param_type): # TODO Distinguish between missing argument and argument of # wrong type? lo('WRONGTYPE', param_name, param_type, value) print('???') lo('ARGS', type(args), short(args)) raise NotImplementedError # TODO MissingArgument else: d[param_name] = value return d
def mk_func_args(func: Callable, args: ArgsMap) -> Dict[str, Any]: d: Dict[str, Any] = {} for param_name, param_type in params_of(func): value = args.get(param_name, None) if not is_type_instance(value, param_type): # TODO Distinguish between missing argument and argument of # wrong type? raise MissingArgument(func=func, param_name=param_name, param_type=param_type, value=value, actual_type=type(value)) else: d[param_name] = value return d
def test_union(self) -> None: typ = self.hints['uni'] self.assertTrue(is_type_instance(None, typ)) self.assertTrue(is_type_instance(self.Blah(), typ)) self.assertFalse(is_type_instance(2, typ))
def test_optional(self) -> None: typ = self.hints['opt'] self.assertTrue(is_type_instance(None, typ)) self.assertTrue(is_type_instance(self.Blah(), typ)) self.assertFalse(is_type_instance(2, typ))
def test_none(self) -> None: none = self.hints['none'] self.assertTrue(is_type_instance(None, none)) self.assertFalse(is_type_instance(self.Blah(), none))
def test_class(self) -> None: blah = self.hints['blah'] b = self.Blah() self.assertTrue(is_type_instance(b, blah)) self.assertFalse(is_type_instance(None, blah))
def test_int(self) -> None: i = self.hints['int'] self.assertTrue(is_type_instance(2, i)) self.assertFalse(is_type_instance('foo', i)) self.assertFalse(is_type_instance(None, i))