def test_enforce_type_hints_defaults(self): def example_function(a: int = 5, b: int = 6) -> int: return a + b new_func = enforce_type_hints(example_function) self.assertEqual(new_func(), 11)
def test_enforce_type_hints_vanilla(self): def example_function(a, b): return a + b new_func = enforce_type_hints(example_function) self.assertEqual(new_func(1, 2), 3)
def test_enforce_type_hints_packed_args_kwargs_vanilla(self): def example_function(*foos, **bars): return sum(foos) >= len(bars) new_func = enforce_type_hints(example_function) self.assertEqual(new_func(1, 2, 3, a="a", b="b", c="c"), True)
def test_enforce_type_hints_packed_args_vanilla(self): def example_function(a, *args, b=5, c=6): return sum([a, *args, b, c]) new_func = enforce_type_hints(example_function) result = new_func(1, 2, 3, b=3, c=2) self.assertEqual(result, 11)
def test_enforce_type_hints_packed_kwargs_vanilla(self): def example_function(a, b, **kwargs): return (a, b, *kwargs.keys(), *kwargs.values()) new_func = enforce_type_hints(example_function) result = new_func(1, 2, y=2, z="v", x=4.5) self.assertEqual((1, 2, "y", "z", "x", 2, "v", 4.5), result)
def test_enforce_type_hints_return_coercibility(self): def example_function(a: float, b: float) -> float: return a + b new_func = enforce_type_hints(example_function) # Result is not cast to a float result = new_func(2, 3) self.assertEqual(result, 5) self.assertIsInstance(result, int)
def test_enforce_type_hints_invalid_defaults(self): def example_function(a: int = 0.5, b: int = 6) -> int: return a + b new_func = enforce_type_hints(example_function) with self.assertRaises(ParameterTypeError) as err: new_func() self.assertEqual( "The 'a' parameter of 'example_function' must be <class 'int'>, " "but a value of 0.5 <class 'float'> was specified.", str(err.exception))
def test_enforce_type_hints_packed_args_kwargs_hint(self): def example_function(*foos: int, **bars: str) -> bool: return sum(foos) >= len(bars) new_func = enforce_type_hints(example_function) self.assertEqual(new_func(1, 2, 3, a="a", b="b", c="c"), True) with self.assertRaises(ParameterTypeError): new_func(2, 3, 5, d=4) with self.assertRaises(ParameterTypeError): new_func(2, "three", 5, e="e")
def test_enforce_type_hints_return_with_hints(self): def example_function(a) -> int: return a new_func = enforce_type_hints(example_function) self.assertEqual(new_func(1), 1) with self.assertRaises(ReturnTypeError) as err: new_func("a") self.assertEqual( "The return type of 'example_function' must be <class 'int'>, " "but a value of 'a' <class 'str'> was returned.", str(err.exception)) self.assertEqual("a", err.exception.return_value)
def test_enforce_type_hints_packed_args_hint(self): def example_function(a: int, b: float, *args: str): return str(a) + str(b) + "".join(str(arg) for arg in args) new_func = enforce_type_hints(example_function) result = new_func(1, 0.5, "a", "b", "c") self.assertEqual(result, "10.5abc") result = new_func(2, 3.0) self.assertEqual(result, "23.0") with self.assertRaises(ParameterTypeError) as err: new_func(1, 0.5, "a", "b", "c", 6) self.assertEqual( "The 'args' parameters of 'example_function' must be " "<class 'str'>, but a value of 6 <class 'int'> was specified.", str(err.exception))
def test_enforce_type_hints_packed_kwargs_hint(self): def example_function(a: float, b: float, **kwargs: str): return (a, b, *kwargs.keys(), *kwargs.values()) new_func = enforce_type_hints(example_function) result = new_func(0.5, 1.0, y="a", word="bird", g="c") self.assertEqual((0.5, 1.0, "y", "word", "g", "a", "bird", "c"), result) result = new_func(0.5, 2.0) self.assertEqual((0.5, 2.0), result) with self.assertRaises(ParameterTypeError) as err: new_func(0.5, 1.0, g="r", word=10) self.assertEqual( "The 'kwargs' keywords of 'example_function' must have values of " "type <class 'str'>, but 'word':10 <class 'int'> was specified.", str(err.exception))
def test_enforce_type_hints_parameters_with_hints(self): def example_function(a: int, b: int) -> int: return a + b new_func = enforce_type_hints(example_function) self.assertEqual(new_func(1, 2), 3) with self.assertRaises(ParameterTypeError) as err: new_func(1.0, 2) self.assertEqual( "The 'a' parameter of 'example_function' must be <class 'int'>, " "but a value of 1.0 <class 'float'> was specified.", str(err.exception)) with self.assertRaises(ParameterTypeError) as err: new_func(b=1.0, a=2) self.assertEqual( "The 'b' parameter of 'example_function' must be <class 'int'>, " "but a value of 1.0 <class 'float'> was specified.", str(err.exception))