test_doccer.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ''' Some tests for the documenting decorator and support functions '''
  2. import sys
  3. import warnings
  4. import pytest
  5. from numpy.testing import assert_equal
  6. from scipy._lib import doccer
  7. # python -OO strips docstrings
  8. DOCSTRINGS_STRIPPED = sys.flags.optimize > 1
  9. docstring = \
  10. """Docstring
  11. %(strtest1)s
  12. %(strtest2)s
  13. %(strtest3)s
  14. """
  15. param_doc1 = \
  16. """Another test
  17. with some indent"""
  18. param_doc2 = \
  19. """Another test, one line"""
  20. param_doc3 = \
  21. """ Another test
  22. with some indent"""
  23. doc_dict = {'strtest1':param_doc1,
  24. 'strtest2':param_doc2,
  25. 'strtest3':param_doc3}
  26. filled_docstring = \
  27. """Docstring
  28. Another test
  29. with some indent
  30. Another test, one line
  31. Another test
  32. with some indent
  33. """
  34. def test_unindent():
  35. with warnings.catch_warnings():
  36. warnings.simplefilter("ignore", category=DeprecationWarning)
  37. assert_equal(doccer.unindent_string(param_doc1), param_doc1)
  38. assert_equal(doccer.unindent_string(param_doc2), param_doc2)
  39. assert_equal(doccer.unindent_string(param_doc3), param_doc1)
  40. def test_unindent_dict():
  41. with warnings.catch_warnings():
  42. warnings.simplefilter("ignore", category=DeprecationWarning)
  43. d2 = doccer.unindent_dict(doc_dict)
  44. assert_equal(d2['strtest1'], doc_dict['strtest1'])
  45. assert_equal(d2['strtest2'], doc_dict['strtest2'])
  46. assert_equal(d2['strtest3'], doc_dict['strtest1'])
  47. def test_docformat():
  48. with warnings.catch_warnings():
  49. warnings.simplefilter("ignore", category=DeprecationWarning)
  50. udd = doccer.unindent_dict(doc_dict)
  51. formatted = doccer.docformat(docstring, udd)
  52. assert_equal(formatted, filled_docstring)
  53. single_doc = 'Single line doc %(strtest1)s'
  54. formatted = doccer.docformat(single_doc, doc_dict)
  55. # Note - initial indent of format string does not
  56. # affect subsequent indent of inserted parameter
  57. assert_equal(formatted, """Single line doc Another test
  58. with some indent""")
  59. @pytest.mark.skipif(DOCSTRINGS_STRIPPED, reason="docstrings stripped")
  60. def test_decorator():
  61. with warnings.catch_warnings():
  62. warnings.simplefilter("ignore", category=DeprecationWarning)
  63. # with unindentation of parameters
  64. decorator = doccer.filldoc(doc_dict, True)
  65. @decorator
  66. def func():
  67. """ Docstring
  68. %(strtest3)s
  69. """
  70. def expected():
  71. """ Docstring
  72. Another test
  73. with some indent
  74. """
  75. assert_equal(func.__doc__, expected.__doc__)
  76. # without unindentation of parameters
  77. # The docstring should be unindented for Python 3.13+
  78. # because of https://github.com/python/cpython/issues/81283
  79. decorator = doccer.filldoc(doc_dict, False if \
  80. sys.version_info < (3, 13) else True)
  81. @decorator
  82. def func():
  83. """ Docstring
  84. %(strtest3)s
  85. """
  86. def expected():
  87. """ Docstring
  88. Another test
  89. with some indent
  90. """
  91. assert_equal(func.__doc__, expected.__doc__)
  92. @pytest.mark.skipif(DOCSTRINGS_STRIPPED, reason="docstrings stripped")
  93. def test_inherit_docstring_from():
  94. with warnings.catch_warnings():
  95. warnings.simplefilter("ignore", category=DeprecationWarning)
  96. class Foo:
  97. def func(self):
  98. '''Do something useful.'''
  99. return
  100. def func2(self):
  101. '''Something else.'''
  102. class Bar(Foo):
  103. @doccer.inherit_docstring_from(Foo)
  104. def func(self):
  105. '''%(super)sABC'''
  106. return
  107. @doccer.inherit_docstring_from(Foo)
  108. def func2(self):
  109. # No docstring.
  110. return
  111. assert_equal(Bar.func.__doc__, Foo.func.__doc__ + 'ABC')
  112. assert_equal(Bar.func2.__doc__, Foo.func2.__doc__)
  113. bar = Bar()
  114. assert_equal(bar.func.__doc__, Foo.func.__doc__ + 'ABC')
  115. assert_equal(bar.func2.__doc__, Foo.func2.__doc__)