test_deprecate_kwarg.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import pytest
  2. from pandas.util._decorators import deprecate_kwarg
  3. import pandas._testing as tm
  4. @deprecate_kwarg("old", "new")
  5. def _f1(new=False):
  6. return new
  7. _f2_mappings = {"yes": True, "no": False}
  8. @deprecate_kwarg("old", "new", _f2_mappings)
  9. def _f2(new=False):
  10. return new
  11. def _f3_mapping(x):
  12. return x + 1
  13. @deprecate_kwarg("old", "new", _f3_mapping)
  14. def _f3(new=0):
  15. return new
  16. @pytest.mark.parametrize("key,klass", [("old", FutureWarning), ("new", None)])
  17. def test_deprecate_kwarg(key, klass):
  18. x = 78
  19. with tm.assert_produces_warning(klass):
  20. assert _f1(**{key: x}) == x
  21. @pytest.mark.parametrize("key", list(_f2_mappings.keys()))
  22. def test_dict_deprecate_kwarg(key):
  23. with tm.assert_produces_warning(FutureWarning):
  24. assert _f2(old=key) == _f2_mappings[key]
  25. @pytest.mark.parametrize("key", ["bogus", 12345, -1.23])
  26. def test_missing_deprecate_kwarg(key):
  27. with tm.assert_produces_warning(FutureWarning):
  28. assert _f2(old=key) == key
  29. @pytest.mark.parametrize("x", [1, -1.4, 0])
  30. def test_callable_deprecate_kwarg(x):
  31. with tm.assert_produces_warning(FutureWarning):
  32. assert _f3(old=x) == _f3_mapping(x)
  33. def test_callable_deprecate_kwarg_fail():
  34. msg = "((can only|cannot) concatenate)|(must be str)|(Can't convert)"
  35. with pytest.raises(TypeError, match=msg):
  36. _f3(old="hello")
  37. def test_bad_deprecate_kwarg():
  38. msg = "mapping from old to new argument values must be dict or callable!"
  39. with pytest.raises(TypeError, match=msg):
  40. @deprecate_kwarg("old", "new", 0)
  41. def f4(new=None):
  42. return new
  43. @deprecate_kwarg("old", None)
  44. def _f4(old=True, unchanged=True):
  45. return old, unchanged
  46. @pytest.mark.parametrize("key", ["old", "unchanged"])
  47. def test_deprecate_keyword(key):
  48. x = 9
  49. if key == "old":
  50. klass = FutureWarning
  51. expected = (x, True)
  52. else:
  53. klass = None
  54. expected = (True, x)
  55. with tm.assert_produces_warning(klass):
  56. assert _f4(**{key: x}) == expected