def test_multiple_function_parameters_via_kwargs(self): @self.regret.parameter(version="1.2.3", name="y") @self.regret.parameter(version="1.2.3", name="z") def add3(x, **kwargs): return x + kwargs.get("y", 0) + kwargs.get("z", 0) with self.recorder.expect_deprecations( Deprecation( kind=Parameter( callable=add3, parameter=inspect.Parameter( name="y", kind=inspect.Parameter.KEYWORD_ONLY, ), ), ), Deprecation( kind=Parameter( callable=add3, parameter=inspect.Parameter( name="z", kind=inspect.Parameter.KEYWORD_ONLY, ), ), ), ): self.assertEqual(add3(1, y=2, z=3), 6)
def test_multiple_function_parameters_keyword_only(self): @self.regret.parameter(version="1.2.3", name="y") @self.regret.parameter(version="1.2.3", name="z") def add3(x, *, y, z): return x + y + z with self.recorder.expect_deprecations( Deprecation( kind=Parameter( callable=add3, parameter=inspect.Parameter( name="y", kind=inspect.Parameter.KEYWORD_ONLY, ), ), ), Deprecation( kind=Parameter( callable=add3, parameter=inspect.Parameter( name="z", kind=inspect.Parameter.KEYWORD_ONLY, ), ), ), ): self.assertEqual(add3(1, y=2, z=3), 6)
def test_multiple_function_parameters_positionally(self): @self.regret.parameter(version="1.2.3", name="y") @self.regret.parameter(version="1.2.3", name="z") def add3(x, y, z): return x + y + z with self.recorder.expect_deprecations( Deprecation( kind=Parameter( callable=add3, parameter=inspect.Parameter( name="y", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, ), ), ), Deprecation( kind=Parameter( callable=add3, parameter=inspect.Parameter( name="z", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, ), ), ), ): self.assertEqual(add3(1, 2, 3), 6)
def test_multiple_function_parameters_warn_in_definition_order(self): """ No matter what order parameters are deprecated in, warnings are emitted in the definition order that parameters were originally defined in. """ @self.regret.parameter(version="1.2.3", name="z") @self.regret.parameter(version="1.2.3", name="v") @self.regret.parameter(version="1.2.3", name="x") @self.regret.parameter(version="1.2.3", name="y") def add5(v, w, x, y, z): return v + w + x + y + z with self.recorder.expect_deprecations( *[ Deprecation( kind=Parameter( callable=add5, parameter=inspect.Parameter( name=each, kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, ), ), ) for each in "vxyz" ], ): self.assertEqual(add5(1, 2, 3, 4, 5), 15)
def test_multiple_function_parameters_via_kwargs_warns_sorted_order(self): @self.regret.parameter(version="1.2.3", name="z") @self.regret.parameter(version="1.2.3", name="v") @self.regret.parameter(version="1.2.3", name="x") @self.regret.parameter(version="1.2.3", name="y") def add5(w, **kwargs): return w + sum(kwargs.values()) with self.recorder.expect_deprecations( Deprecation( kind=Parameter( callable=add5, parameter=inspect.Parameter( name="v", kind=inspect.Parameter.KEYWORD_ONLY, ), ), ), Deprecation( kind=Parameter( callable=add5, parameter=inspect.Parameter( name="x", kind=inspect.Parameter.KEYWORD_ONLY, ), ), ), Deprecation( kind=Parameter( callable=add5, parameter=inspect.Parameter( name="y", kind=inspect.Parameter.KEYWORD_ONLY, ), ), ), Deprecation( kind=Parameter( callable=add5, parameter=inspect.Parameter( name="z", kind=inspect.Parameter.KEYWORD_ONLY, ), ), ), ): self.assertEqual(add5(1, z=2, y=3, x=4, v=5), 15)
def expect(self, **kwargs): """ Expect a given set of deprecations to be emitted. """ return self.expect_deprecations(Deprecation(**kwargs))