test_deprecate_nonkeyword_arguments.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """
  2. Tests for the `deprecate_nonkeyword_arguments` decorator
  3. """
  4. import inspect
  5. import warnings
  6. from pandas.util._decorators import deprecate_nonkeyword_arguments
  7. import pandas._testing as tm
  8. @deprecate_nonkeyword_arguments(
  9. version="1.1", allowed_args=["a", "b"], name="f_add_inputs"
  10. )
  11. def f(a, b=0, c=0, d=0):
  12. return a + b + c + d
  13. def test_f_signature():
  14. assert str(inspect.signature(f)) == "(a, b=0, *, c=0, d=0)"
  15. def test_one_argument():
  16. with tm.assert_produces_warning(None):
  17. assert f(19) == 19
  18. def test_one_and_one_arguments():
  19. with tm.assert_produces_warning(None):
  20. assert f(19, d=6) == 25
  21. def test_two_arguments():
  22. with tm.assert_produces_warning(None):
  23. assert f(1, 5) == 6
  24. def test_two_and_two_arguments():
  25. with tm.assert_produces_warning(None):
  26. assert f(1, 3, c=3, d=5) == 12
  27. def test_three_arguments():
  28. with tm.assert_produces_warning(FutureWarning):
  29. assert f(6, 3, 3) == 12
  30. def test_four_arguments():
  31. with tm.assert_produces_warning(FutureWarning):
  32. assert f(1, 2, 3, 4) == 10
  33. def test_three_arguments_with_name_in_warning():
  34. with warnings.catch_warnings(record=True) as w:
  35. warnings.simplefilter("always")
  36. assert f(6, 3, 3) == 12
  37. assert len(w) == 1
  38. for actual_warning in w:
  39. assert actual_warning.category == FutureWarning
  40. assert str(actual_warning.message) == (
  41. "Starting with pandas version 1.1 all arguments of f_add_inputs "
  42. "except for the arguments 'a' and 'b' will be keyword-only."
  43. )
  44. @deprecate_nonkeyword_arguments(version="1.1")
  45. def g(a, b=0, c=0, d=0):
  46. with tm.assert_produces_warning(None):
  47. return a + b + c + d
  48. def test_g_signature():
  49. assert str(inspect.signature(g)) == "(a, *, b=0, c=0, d=0)"
  50. def test_one_and_three_arguments_default_allowed_args():
  51. with tm.assert_produces_warning(None):
  52. assert g(1, b=3, c=3, d=5) == 12
  53. def test_three_arguments_default_allowed_args():
  54. with tm.assert_produces_warning(FutureWarning):
  55. assert g(6, 3, 3) == 12
  56. def test_three_positional_argument_with_warning_message_analysis():
  57. with warnings.catch_warnings(record=True) as w:
  58. warnings.simplefilter("always")
  59. assert g(6, 3, 3) == 12
  60. assert len(w) == 1
  61. for actual_warning in w:
  62. assert actual_warning.category == FutureWarning
  63. assert str(actual_warning.message) == (
  64. "Starting with pandas version 1.1 all arguments of g "
  65. "except for the argument 'a' will be keyword-only."
  66. )
  67. @deprecate_nonkeyword_arguments(version="1.1")
  68. def h(a=0, b=0, c=0, d=0):
  69. return a + b + c + d
  70. def test_h_signature():
  71. assert str(inspect.signature(h)) == "(*, a=0, b=0, c=0, d=0)"
  72. def test_all_keyword_arguments():
  73. with tm.assert_produces_warning(None):
  74. assert h(a=1, b=2) == 3
  75. def test_one_positional_argument():
  76. with tm.assert_produces_warning(FutureWarning):
  77. assert h(23) == 23
  78. def test_one_positional_argument_with_warning_message_analysis():
  79. with warnings.catch_warnings(record=True) as w:
  80. warnings.simplefilter("always")
  81. assert h(19) == 19
  82. assert len(w) == 1
  83. for actual_warning in w:
  84. assert actual_warning.category == FutureWarning
  85. assert str(actual_warning.message) == (
  86. "Starting with pandas version 1.1 all arguments "
  87. "of h will be keyword-only."
  88. )
  89. @deprecate_nonkeyword_arguments(version="1.1")
  90. def i(a=0, /, b=0, *, c=0, d=0):
  91. return a + b + c + d
  92. def test_i_signature():
  93. assert str(inspect.signature(i)) == "(*, a=0, b=0, c=0, d=0)"
  94. class Foo:
  95. @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "bar"])
  96. def baz(self, bar=None, foobar=None): # pylint: disable=disallowed-name
  97. ...
  98. def test_foo_signature():
  99. assert str(inspect.signature(Foo.baz)) == "(self, bar=None, *, foobar=None)"
  100. def test_class():
  101. msg = (
  102. r"In a future version of pandas all arguments of Foo\.baz "
  103. r"except for the argument \'bar\' will be keyword-only"
  104. )
  105. with tm.assert_produces_warning(FutureWarning, match=msg):
  106. Foo().baz("qux", "quox")