test_kwargs_and_defaults.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import pytest
  2. from pybind11_tests import kwargs_and_defaults as m
  3. def test_function_signatures(doc):
  4. assert doc(m.kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
  5. assert doc(m.kw_func1) == "kw_func1(x: int, y: int) -> str"
  6. assert doc(m.kw_func2) == "kw_func2(x: int=100, y: int=200) -> str"
  7. assert doc(m.kw_func3) == "kw_func3(data: str='Hello world!') -> None"
  8. assert doc(m.kw_func4) == "kw_func4(myList: List[int]=[13, 17]) -> str"
  9. assert doc(m.kw_func_udl) == "kw_func_udl(x: int, y: int=300) -> str"
  10. assert doc(m.kw_func_udl_z) == "kw_func_udl_z(x: int, y: int=0) -> str"
  11. assert doc(m.args_function) == "args_function(*args) -> tuple"
  12. assert doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
  13. assert doc(m.KWClass.foo0) == \
  14. "foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
  15. assert doc(m.KWClass.foo1) == \
  16. "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
  17. def test_named_arguments(msg):
  18. assert m.kw_func0(5, 10) == "x=5, y=10"
  19. assert m.kw_func1(5, 10) == "x=5, y=10"
  20. assert m.kw_func1(5, y=10) == "x=5, y=10"
  21. assert m.kw_func1(y=10, x=5) == "x=5, y=10"
  22. assert m.kw_func2() == "x=100, y=200"
  23. assert m.kw_func2(5) == "x=5, y=200"
  24. assert m.kw_func2(x=5) == "x=5, y=200"
  25. assert m.kw_func2(y=10) == "x=100, y=10"
  26. assert m.kw_func2(5, 10) == "x=5, y=10"
  27. assert m.kw_func2(x=5, y=10) == "x=5, y=10"
  28. with pytest.raises(TypeError) as excinfo:
  29. # noinspection PyArgumentList
  30. m.kw_func2(x=5, y=10, z=12)
  31. assert excinfo.match(
  32. r'(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))' + '{3}$')
  33. assert m.kw_func4() == "{13 17}"
  34. assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
  35. assert m.kw_func_udl(x=5, y=10) == "x=5, y=10"
  36. assert m.kw_func_udl_z(x=5) == "x=5, y=0"
  37. def test_arg_and_kwargs():
  38. args = 'arg1_value', 'arg2_value', 3
  39. assert m.args_function(*args) == args
  40. args = 'a1', 'a2'
  41. kwargs = dict(arg3='a3', arg4=4)
  42. assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
  43. def test_mixed_args_and_kwargs(msg):
  44. mpa = m.mixed_plus_args
  45. mpk = m.mixed_plus_kwargs
  46. mpak = m.mixed_plus_args_kwargs
  47. mpakd = m.mixed_plus_args_kwargs_defaults
  48. assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
  49. assert mpa(1, 2.5) == (1, 2.5, ())
  50. with pytest.raises(TypeError) as excinfo:
  51. assert mpa(1)
  52. assert msg(excinfo.value) == """
  53. mixed_plus_args(): incompatible function arguments. The following argument types are supported:
  54. 1. (arg0: int, arg1: float, *args) -> tuple
  55. Invoked with: 1
  56. """ # noqa: E501 line too long
  57. with pytest.raises(TypeError) as excinfo:
  58. assert mpa()
  59. assert msg(excinfo.value) == """
  60. mixed_plus_args(): incompatible function arguments. The following argument types are supported:
  61. 1. (arg0: int, arg1: float, *args) -> tuple
  62. Invoked with:
  63. """ # noqa: E501 line too long
  64. assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (-2, 3.5, {'e': 2.71828, 'pi': 3.14159})
  65. assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
  66. 7, 7.7, (7.77, 7.777, 7.7777), {'minusseven': -7})
  67. assert mpakd() == (1, 3.14159, (), {})
  68. assert mpakd(3) == (3, 3.14159, (), {})
  69. assert mpakd(j=2.71828) == (1, 2.71828, (), {})
  70. assert mpakd(k=42) == (1, 3.14159, (), {'k': 42})
  71. assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
  72. 1, 1, (2, 3, 5, 8), {'then': 13, 'followedby': 21})
  73. # Arguments specified both positionally and via kwargs should fail:
  74. with pytest.raises(TypeError) as excinfo:
  75. assert mpakd(1, i=1)
  76. assert msg(excinfo.value) == """
  77. mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
  78. 1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
  79. Invoked with: 1; kwargs: i=1
  80. """ # noqa: E501 line too long
  81. with pytest.raises(TypeError) as excinfo:
  82. assert mpakd(1, 2, j=1)
  83. assert msg(excinfo.value) == """
  84. mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
  85. 1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
  86. Invoked with: 1, 2; kwargs: j=1
  87. """ # noqa: E501 line too long
  88. def test_args_refcount():
  89. """Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
  90. arguments"""
  91. refcount = m.arg_refcount_h
  92. myval = 54321
  93. expected = refcount(myval)
  94. assert m.arg_refcount_h(myval) == expected
  95. assert m.arg_refcount_o(myval) == expected + 1
  96. assert m.arg_refcount_h(myval) == expected
  97. assert refcount(myval) == expected
  98. assert m.mixed_plus_args(1, 2.0, "a", myval) == (1, 2.0, ("a", myval))
  99. assert refcount(myval) == expected
  100. assert m.mixed_plus_kwargs(3, 4.0, a=1, b=myval) == (3, 4.0, {"a": 1, "b": myval})
  101. assert refcount(myval) == expected
  102. assert m.args_function(-1, myval) == (-1, myval)
  103. assert refcount(myval) == expected
  104. assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == (5, 6.0, (myval,), {"a": myval})
  105. assert refcount(myval) == expected
  106. assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == \
  107. ((7, 8, myval), {"a": 1, "b": myval})
  108. assert refcount(myval) == expected
  109. exp3 = refcount(myval, myval, myval)
  110. assert m.args_refcount(myval, myval, myval) == (exp3, exp3, exp3)
  111. assert refcount(myval) == expected
  112. # This function takes the first arg as a `py::object` and the rest as a `py::args`. Unlike the
  113. # previous case, when we have both positional and `py::args` we need to construct a new tuple
  114. # for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
  115. # tuple without having to inc_ref the individual elements, but here we can't, hence the extra
  116. # refs.
  117. assert m.mixed_args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3)