test_deprecate.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from textwrap import dedent
  2. import pytest
  3. from pandas.util._decorators import deprecate
  4. import pandas.util.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. pass
  24. def test_deprecate_ok():
  25. depr_func = deprecate('depr_func', new_func, '1.0',
  26. msg='Use new_func instead.')
  27. with tm.assert_produces_warning(FutureWarning):
  28. result = depr_func()
  29. assert result == 'new_func called'
  30. assert depr_func.__doc__ == dedent(new_func_with_deprecation.__doc__)
  31. def test_deprecate_no_docstring():
  32. depr_func = deprecate('depr_func', new_func_no_docstring, '1.0',
  33. msg='Use new_func instead.')
  34. with tm.assert_produces_warning(FutureWarning):
  35. result = depr_func()
  36. assert result == 'new_func_no_docstring called'
  37. def test_deprecate_wrong_docstring():
  38. with pytest.raises(AssertionError, match='deprecate needs a correctly '
  39. 'formatted docstring'):
  40. deprecate('depr_func', new_func_wrong_docstring, '1.0',
  41. msg='Use new_func instead.')