_deprecatetests.py.3only 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. import inspect
  4. from twisted.python.deprecate import _passedSignature
  5. from twisted.trial.unittest import SynchronousTestCase
  6. class KeywordOnlyTests(SynchronousTestCase):
  7. """
  8. Keyword only arguments (PEP 3102).
  9. """
  10. def checkPassed(self, func, *args, **kw):
  11. """
  12. Test an invocation of L{passed} with the given function, arguments, and
  13. keyword arguments.
  14. @param func: A function whose argspec to pass to L{_passed}.
  15. @type func: A callable.
  16. @param args: The arguments which could be passed to L{func}.
  17. @param kw: The keyword arguments which could be passed to L{func}.
  18. @return: L{_passed}'s return value
  19. @rtype: L{dict}
  20. """
  21. return _passedSignature(inspect.signature(func), args, kw)
  22. def test_passedKeywordOnly(self):
  23. """
  24. Keyword only arguments follow varargs.
  25. They are specified in PEP 3102.
  26. """
  27. def func1(*a, b=True):
  28. """
  29. b is a keyword-only argument, with a default value.
  30. """
  31. def func2(*a, b=True, c, d, e):
  32. """
  33. b, c, d, e are keyword-only arguments.
  34. b has a default value.
  35. """
  36. self.assertEqual(self.checkPassed(func1, 1, 2, 3),
  37. dict(a=(1, 2, 3), b=True))
  38. self.assertEqual(self.checkPassed(func1, 1, 2, 3, b=False),
  39. dict(a=(1, 2, 3), b=False))
  40. self.assertEqual(self.checkPassed(func2,
  41. 1, 2, b=False, c=1, d=2, e=3),
  42. dict(a=(1, 2), b=False, c=1, d=2, e=3))
  43. self.assertRaises(TypeError, self.checkPassed,
  44. func2, 1, 2, b=False, c=1, d=2)