test_deprecate_kwarg.py 2.0 KB

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