test_deprecate.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from textwrap import dedent
  2. import pytest
  3. from pandas.util._decorators import deprecate
  4. import pandas._testing as tm
  5. def new_func():
  6. """
  7. This is the summary. The deprecate directive goes next.
  8. This is the extended summary. The deprecate directive goes before this.
  9. """
  10. return "new_func called"
  11. def new_func_no_docstring():
  12. return "new_func_no_docstring called"
  13. def new_func_wrong_docstring():
  14. """Summary should be in the next line."""
  15. return "new_func_wrong_docstring called"
  16. def new_func_with_deprecation():
  17. """
  18. This is the summary. The deprecate directive goes next.
  19. .. deprecated:: 1.0
  20. Use new_func instead.
  21. This is the extended summary. The deprecate directive goes before this.
  22. """
  23. def test_deprecate_ok():
  24. depr_func = deprecate("depr_func", new_func, "1.0", msg="Use new_func instead.")
  25. with tm.assert_produces_warning(FutureWarning):
  26. result = depr_func()
  27. assert result == "new_func called"
  28. assert depr_func.__doc__ == dedent(new_func_with_deprecation.__doc__)
  29. def test_deprecate_no_docstring():
  30. depr_func = deprecate(
  31. "depr_func", new_func_no_docstring, "1.0", msg="Use new_func instead."
  32. )
  33. with tm.assert_produces_warning(FutureWarning):
  34. result = depr_func()
  35. assert result == "new_func_no_docstring called"
  36. def test_deprecate_wrong_docstring():
  37. msg = "deprecate needs a correctly formatted docstring"
  38. with pytest.raises(AssertionError, match=msg):
  39. deprecate(
  40. "depr_func", new_func_wrong_docstring, "1.0", msg="Use new_func instead."
  41. )