Debian Codegraph @

tests/test_ext_autodoc/test_ext_autodoc_configs.py

raw
1 """Test the autodoc extension. This tests mainly for config variables"""
2
3 from __future__ import annotations
4
5 import logging
6 import platform
7 import sys
8
9 import pytest
10
11 from sphinx.ext.autodoc._shared import _AutodocConfig
12
13 from tests.test_ext_autodoc.autodoc_util import do_autodoc
14
15 pytestmark = pytest.mark.usefixtures('inject_autodoc_root_into_sys_path')
16
17 IS_PYPY = platform.python_implementation() == 'PyPy'
18
19
20 def test_autoclass_content_class() -> None:
21 config = _AutodocConfig(autoclass_content='class')
22 options = {'members': None}
23 actual = do_autodoc(
24 'module', 'target.autoclass_content', config=config, options=options
25 )
26 assert actual == [
27 '',
28 '.. py:module:: target.autoclass_content',
29 '',
30 '',
31 '.. py:class:: A()',
32 ' :module: target.autoclass_content',
33 '',
34 ' A class having no __init__, no __new__',
35 '',
36 '',
37 '.. py:class:: B()',
38 ' :module: target.autoclass_content',
39 '',
40 ' A class having __init__(no docstring), no __new__',
41 '',
42 '',
43 '.. py:class:: C()',
44 ' :module: target.autoclass_content',
45 '',
46 ' A class having __init__, no __new__',
47 '',
48 '',
49 '.. py:class:: D()',
50 ' :module: target.autoclass_content',
51 '',
52 ' A class having no __init__, __new__(no docstring)',
53 '',
54 '',
55 '.. py:class:: E()',
56 ' :module: target.autoclass_content',
57 '',
58 ' A class having no __init__, __new__',
59 '',
60 '',
61 '.. py:class:: F()',
62 ' :module: target.autoclass_content',
63 '',
64 ' A class having both __init__ and __new__',
65 '',
66 '',
67 '.. py:class:: G()',
68 ' :module: target.autoclass_content',
69 '',
70 ' A class inherits __init__ without docstring.',
71 '',
72 '',
73 '.. py:class:: H()',
74 ' :module: target.autoclass_content',
75 '',
76 ' A class inherits __new__ without docstring.',
77 '',
78 ]
79
80
81 def test_autoclass_content_init() -> None:
82 config = _AutodocConfig(autoclass_content='init')
83 options = {'members': None}
84 actual = do_autodoc(
85 'module', 'target.autoclass_content', config=config, options=options
86 )
87 assert actual == [
88 '',
89 '.. py:module:: target.autoclass_content',
90 '',
91 '',
92 '.. py:class:: A()',
93 ' :module: target.autoclass_content',
94 '',
95 ' A class having no __init__, no __new__',
96 '',
97 '',
98 '.. py:class:: B()',
99 ' :module: target.autoclass_content',
100 '',
101 ' A class having __init__(no docstring), no __new__',
102 '',
103 '',
104 '.. py:class:: C()',
105 ' :module: target.autoclass_content',
106 '',
107 ' __init__ docstring',
108 '',
109 '',
110 '.. py:class:: D()',
111 ' :module: target.autoclass_content',
112 '',
113 ' A class having no __init__, __new__(no docstring)',
114 '',
115 '',
116 '.. py:class:: E()',
117 ' :module: target.autoclass_content',
118 '',
119 ' __new__ docstring',
120 '',
121 '',
122 '.. py:class:: F()',
123 ' :module: target.autoclass_content',
124 '',
125 ' __init__ docstring',
126 '',
127 '',
128 '.. py:class:: G()',
129 ' :module: target.autoclass_content',
130 '',
131 ' __init__ docstring',
132 '',
133 '',
134 '.. py:class:: H()',
135 ' :module: target.autoclass_content',
136 '',
137 ' __new__ docstring',
138 '',
139 ]
140
141
142 def test_autodoc_class_signature_mixed() -> None:
143 config = _AutodocConfig(autodoc_class_signature='mixed')
144 options = {
145 'members': None,
146 'undoc-members': None,
147 }
148 actual = do_autodoc('class', 'target.classes.Bar', config=config, options=options)
149 assert actual == [
150 '',
151 '.. py:class:: Bar(x, y)',
152 ' :module: target.classes',
153 '',
154 ]
155
156
157 def test_autodoc_class_signature_separated_init() -> None:
158 config = _AutodocConfig(autodoc_class_signature='separated')
159 options = {
160 'members': None,
161 'undoc-members': None,
162 }
163 actual = do_autodoc('class', 'target.classes.Bar', config=config, options=options)
164 assert actual == [
165 '',
166 '.. py:class:: Bar',
167 ' :module: target.classes',
168 '',
169 '',
170 ' .. py:method:: Bar.__init__(x, y)',
171 ' :module: target.classes',
172 '',
173 ]
174
175
176 def test_autodoc_class_signature_separated_new() -> None:
177 config = _AutodocConfig(autodoc_class_signature='separated')
178 options = {
179 'members': None,
180 'undoc-members': None,
181 }
182 actual = do_autodoc('class', 'target.classes.Baz', config=config, options=options)
183 assert actual == [
184 '',
185 '.. py:class:: Baz',
186 ' :module: target.classes',
187 '',
188 '',
189 ' .. py:method:: Baz.__new__(cls, x, y)',
190 ' :module: target.classes',
191 ' :staticmethod:',
192 '',
193 ]
194
195
196 def test_autoclass_content_both() -> None:
197 config = _AutodocConfig(autoclass_content='both')
198 options = {'members': None}
199 actual = do_autodoc(
200 'module', 'target.autoclass_content', config=config, options=options
201 )
202 assert actual == [
203 '',
204 '.. py:module:: target.autoclass_content',
205 '',
206 '',
207 '.. py:class:: A()',
208 ' :module: target.autoclass_content',
209 '',
210 ' A class having no __init__, no __new__',
211 '',
212 '',
213 '.. py:class:: B()',
214 ' :module: target.autoclass_content',
215 '',
216 ' A class having __init__(no docstring), no __new__',
217 '',
218 '',
219 '.. py:class:: C()',
220 ' :module: target.autoclass_content',
221 '',
222 ' A class having __init__, no __new__',
223 '',
224 ' __init__ docstring',
225 '',
226 '',
227 '.. py:class:: D()',
228 ' :module: target.autoclass_content',
229 '',
230 ' A class having no __init__, __new__(no docstring)',
231 '',
232 '',
233 '.. py:class:: E()',
234 ' :module: target.autoclass_content',
235 '',
236 ' A class having no __init__, __new__',
237 '',
238 ' __new__ docstring',
239 '',
240 '',
241 '.. py:class:: F()',
242 ' :module: target.autoclass_content',
243 '',
244 ' A class having both __init__ and __new__',
245 '',
246 ' __init__ docstring',
247 '',
248 '',
249 '.. py:class:: G()',
250 ' :module: target.autoclass_content',
251 '',
252 ' A class inherits __init__ without docstring.',
253 '',
254 ' __init__ docstring',
255 '',
256 '',
257 '.. py:class:: H()',
258 ' :module: target.autoclass_content',
259 '',
260 ' A class inherits __new__ without docstring.',
261 '',
262 ' __new__ docstring',
263 '',
264 ]
265
266
267 def test_autodoc_inherit_docstrings() -> None:
268 config = _AutodocConfig()
269 assert config.autodoc_inherit_docstrings is True # default
270 actual = do_autodoc(
271 'method', 'target.inheritance.Derived.inheritedmeth', config=config
272 )
273 assert actual == [
274 '',
275 '.. py:method:: Derived.inheritedmeth()',
276 ' :module: target.inheritance',
277 '',
278 ' Inherited function.',
279 '',
280 ]
281
282 # disable autodoc_inherit_docstrings
283 config = _AutodocConfig(autodoc_inherit_docstrings=False)
284 actual = do_autodoc(
285 'method', 'target.inheritance.Derived.inheritedmeth', config=config
286 )
287 assert actual == [
288 '',
289 '.. py:method:: Derived.inheritedmeth()',
290 ' :module: target.inheritance',
291 '',
292 ]
293
294
295 def test_autodoc_inherit_docstrings_for_inherited_members() -> None:
296 config = _AutodocConfig()
297 options = {
298 'members': None,
299 'inherited-members': None,
300 }
301
302 assert config.autodoc_inherit_docstrings is True # default
303 actual = do_autodoc(
304 'class', 'target.inheritance.Derived', config=config, options=options
305 )
306 assert actual == [
307 '',
308 '.. py:class:: Derived()',
309 ' :module: target.inheritance',
310 '',
311 '',
312 ' .. py:method:: Derived.another_inheritedmeth()',
313 ' :module: target.inheritance',
314 '',
315 ' Another inherited function.',
316 '',
317 '',
318 ' .. py:attribute:: Derived.inheritedattr',
319 ' :module: target.inheritance',
320 ' :value: None',
321 '',
322 ' docstring',
323 '',
324 '',
325 ' .. py:method:: Derived.inheritedclassmeth()',
326 ' :module: target.inheritance',
327 ' :classmethod:',
328 '',
329 ' Inherited class method.',
330 '',
331 '',
332 ' .. py:method:: Derived.inheritedmeth()',
333 ' :module: target.inheritance',
334 '',
335 ' Inherited function.',
336 '',
337 '',
338 ' .. py:method:: Derived.inheritedstaticmeth(cls)',
339 ' :module: target.inheritance',
340 ' :staticmethod:',
341 '',
342 ' Inherited static method.',
343 '',
344 ]
345
346 # disable autodoc_inherit_docstrings
347 config = _AutodocConfig(autodoc_inherit_docstrings=False)
348 actual = do_autodoc(
349 'class', 'target.inheritance.Derived', config=config, options=options
350 )
351 assert actual == [
352 '',
353 '.. py:class:: Derived()',
354 ' :module: target.inheritance',
355 '',
356 '',
357 ' .. py:method:: Derived.another_inheritedmeth()',
358 ' :module: target.inheritance',
359 '',
360 ' Another inherited function.',
361 '',
362 '',
363 ' .. py:method:: Derived.inheritedclassmeth()',
364 ' :module: target.inheritance',
365 ' :classmethod:',
366 '',
367 ' Inherited class method.',
368 '',
369 '',
370 ' .. py:method:: Derived.inheritedstaticmeth(cls)',
371 ' :module: target.inheritance',
372 ' :staticmethod:',
373 '',
374 ' Inherited static method.',
375 '',
376 ]
377
378
379 def test_autodoc_docstring_signature() -> None:
380 options = {
381 'members': None,
382 'special-members': '__init__, __new__',
383 }
384 actual = do_autodoc('class', 'target.DocstringSig', options=options)
385 assert actual == [
386 '',
387 # FIXME: Ideally this would instead be: `DocstringSig(d, e=1)` but
388 # currently `ClassDocumenter` does not apply the docstring signature
389 # logic when extracting a signature from a __new__ or __init__ method.
390 '.. py:class:: DocstringSig(*new_args, **new_kwargs)',
391 ' :module: target',
392 '',
393 '',
394 ' .. py:method:: DocstringSig.__init__(self, a, b=1) -> None',
395 ' :module: target',
396 '',
397 ' First line of docstring',
398 '',
399 ' rest of docstring',
400 '',
401 '',
402 ' .. py:method:: DocstringSig.__new__(cls, d, e=1) -> DocstringSig',
403 ' :module: target',
404 ' :staticmethod:',
405 '',
406 ' First line of docstring',
407 '',
408 ' rest of docstring',
409 '',
410 '',
411 ' .. py:method:: DocstringSig.meth(FOO, BAR=1) -> BAZ',
412 ' :module: target',
413 '',
414 ' First line of docstring',
415 '',
416 ' rest of docstring',
417 '',
418 '',
419 ' .. py:method:: DocstringSig.meth2()',
420 ' :module: target',
421 '',
422 ' First line, no signature',
423 ' Second line followed by indentation::',
424 '',
425 ' indented line',
426 '',
427 '',
428 ' .. py:property:: DocstringSig.prop1',
429 ' :module: target',
430 '',
431 ' First line of docstring',
432 '',
433 '',
434 ' .. py:property:: DocstringSig.prop2',
435 ' :module: target',
436 '',
437 ' First line of docstring',
438 ' Second line of docstring',
439 '',
440 ]
441
442 # disable autodoc_docstring_signature
443 config = _AutodocConfig(autodoc_docstring_signature=False)
444 actual = do_autodoc('class', 'target.DocstringSig', config=config, options=options)
445 assert actual == [
446 '',
447 '.. py:class:: DocstringSig(*new_args, **new_kwargs)',
448 ' :module: target',
449 '',
450 '',
451 ' .. py:method:: DocstringSig.__init__(*init_args, **init_kwargs)',
452 ' :module: target',
453 '',
454 ' __init__(self, a, b=1) -> None',
455 ' First line of docstring',
456 '',
457 ' rest of docstring',
458 '',
459 '',
460 ' .. py:method:: DocstringSig.__new__(cls, *new_args, **new_kwargs)',
461 ' :module: target',
462 ' :staticmethod:',
463 '',
464 ' __new__(cls, d, e=1) -> DocstringSig',
465 ' First line of docstring',
466 '',
467 ' rest of docstring',
468 '',
469 '',
470 ' .. py:method:: DocstringSig.meth()',
471 ' :module: target',
472 '',
473 ' meth(FOO, BAR=1) -> BAZ',
474 ' First line of docstring',
475 '',
476 ' rest of docstring',
477 '',
478 '',
479 ' .. py:method:: DocstringSig.meth2()',
480 ' :module: target',
481 '',
482 ' First line, no signature',
483 ' Second line followed by indentation::',
484 '',
485 ' indented line',
486 '',
487 '',
488 ' .. py:property:: DocstringSig.prop1',
489 ' :module: target',
490 '',
491 ' DocstringSig.prop1(self)',
492 ' First line of docstring',
493 '',
494 '',
495 ' .. py:property:: DocstringSig.prop2',
496 ' :module: target',
497 '',
498 ' First line of docstring',
499 ' Second line of docstring',
500 '',
501 ]
502
503
504 def test_autoclass_content_and_docstring_signature_class() -> None:
505 config = _AutodocConfig(autoclass_content='class')
506 options = {
507 'members': None,
508 'undoc-members': None,
509 }
510 actual = do_autodoc(
511 'module', 'target.docstring_signature', config=config, options=options
512 )
513 assert actual == [
514 '',
515 '.. py:module:: target.docstring_signature',
516 '',
517 '',
518 '.. py:class:: A(foo, bar)',
519 ' :module: target.docstring_signature',
520 '',
521 '',
522 '.. py:class:: B(foo, bar)',
523 ' :module: target.docstring_signature',
524 '',
525 '',
526 '.. py:class:: C(foo, bar)',
527 ' :module: target.docstring_signature',
528 '',
529 '',
530 '.. py:class:: D()',
531 ' :module: target.docstring_signature',
532 '',
533 '',
534 '.. py:class:: E()',
535 ' :module: target.docstring_signature',
536 '',
537 '',
538 '.. py:class:: F()',
539 ' :module: target.docstring_signature',
540 '',
541 ]
542
543
544 def test_autoclass_content_and_docstring_signature_init() -> None:
545 config = _AutodocConfig(autoclass_content='init')
546 options = {
547 'members': None,
548 'undoc-members': None,
549 }
550 actual = do_autodoc(
551 'module', 'target.docstring_signature', config=config, options=options
552 )
553 assert actual == [
554 '',
555 '.. py:module:: target.docstring_signature',
556 '',
557 '',
558 '.. py:class:: A(foo, bar)',
559 ' :module: target.docstring_signature',
560 '',
561 '',
562 '.. py:class:: B(foo, bar, baz)',
563 ' :module: target.docstring_signature',
564 '',
565 '',
566 '.. py:class:: C(foo, bar, baz)',
567 ' :module: target.docstring_signature',
568 '',
569 '',
570 '.. py:class:: D(foo, bar, baz)',
571 ' :module: target.docstring_signature',
572 '',
573 '',
574 '.. py:class:: E(foo: int, bar: int, baz: int)',
575 ' E(foo: str, bar: str, baz: str)',
576 ' E(foo: float, bar: float, baz: float)',
577 ' :module: target.docstring_signature',
578 '',
579 '',
580 '.. py:class:: F(foo: int, bar: int, baz: int)',
581 ' F(foo: str, bar: str, baz: str)',
582 ' F(foo: float, bar: float, baz: float)',
583 ' :module: target.docstring_signature',
584 '',
585 ]
586
587
588 def test_autoclass_content_and_docstring_signature_both() -> None:
589 config = _AutodocConfig(autoclass_content='both')
590 options = {
591 'members': None,
592 'undoc-members': None,
593 }
594 actual = do_autodoc(
595 'module', 'target.docstring_signature', config=config, options=options
596 )
597 assert actual == [
598 '',
599 '.. py:module:: target.docstring_signature',
600 '',
601 '',
602 '.. py:class:: A(foo, bar)',
603 ' :module: target.docstring_signature',
604 '',
605 '',
606 '.. py:class:: B(foo, bar)',
607 ' :module: target.docstring_signature',
608 '',
609 ' B(foo, bar, baz)',
610 '',
611 '',
612 '.. py:class:: C(foo, bar)',
613 ' :module: target.docstring_signature',
614 '',
615 ' C(foo, bar, baz)',
616 '',
617 '',
618 '.. py:class:: D(foo, bar, baz)',
619 ' :module: target.docstring_signature',
620 '',
621 '',
622 '.. py:class:: E(foo: int, bar: int, baz: int)',
623 ' E(foo: str, bar: str, baz: str)',
624 ' E(foo: float, bar: float, baz: float)',
625 ' :module: target.docstring_signature',
626 '',
627 '',
628 '.. py:class:: F(foo: int, bar: int, baz: int)',
629 ' F(foo: str, bar: str, baz: str)',
630 ' F(foo: float, bar: float, baz: float)',
631 ' :module: target.docstring_signature',
632 '',
633 ]
634
635
636 @pytest.mark.usefixtures('rollback_sysmodules')
637 def test_mocked_module_imports(caplog: pytest.LogCaptureFixture) -> None:
638 # work around sphinx.util.logging.setup()
639 logger = logging.getLogger('sphinx')
640 logger.handlers[:] = [caplog.handler]
641 caplog.set_level(logging.WARNING)
642
643 sys.modules.pop('target', None) # unload target module to clear the module cache
644
645 # no autodoc_mock_imports
646 options = {'members': 'TestAutodoc,decorated_function,func,Alias'}
647 actual = do_autodoc(
648 'module', 'target.need_mocks', expect_import_error=True, options=options
649 )
650 assert actual == []
651 assert len(set(caplog.messages)) == 1
652 assert "autodoc: failed to import 'need_mocks'" in caplog.messages[0]
653
654 # with autodoc_mock_imports
655 config = _AutodocConfig(
656 autodoc_mock_imports=[
657 'missing_module',
658 'missing_package1',
659 'missing_package2',
660 'missing_package3',
661 'sphinx.missing_module4',
662 ]
663 )
664
665 caplog.clear()
666 actual = do_autodoc('module', 'target.need_mocks', config=config, options=options)
667 assert actual == [
668 '',
669 '.. py:module:: target.need_mocks',
670 '',
671 '',
672 '.. py:data:: Alias',
673 ' :module: target.need_mocks',
674 '',
675 ' docstring',
676 '',
677 '',
678 '.. py:class:: TestAutodoc()',
679 ' :module: target.need_mocks',
680 '',
681 ' TestAutodoc docstring.',
682 '',
683 '',
684 ' .. py:attribute:: TestAutodoc.Alias',
685 ' :module: target.need_mocks',
686 '',
687 ' docstring',
688 '',
689 '',
690 ' .. py:method:: TestAutodoc.decorated_method()',
691 ' :module: target.need_mocks',
692 '',
693 ' TestAutodoc::decorated_method docstring',
694 '',
695 '',
696 '.. py:function:: decorated_function()',
697 ' :module: target.need_mocks',
698 '',
699 ' decorated_function docstring',
700 '',
701 '',
702 '.. py:function:: func(arg: missing_module.Class)',
703 ' :module: target.need_mocks',
704 '',
705 ' a function takes mocked object as an argument',
706 '',
707 ]
708 assert len(caplog.records) == 0
709
710
711 def test_autodoc_type_aliases() -> None:
712 # default
713 options = {'members': None}
714 actual = do_autodoc('module', 'target.autodoc_type_aliases', options=options)
715 assert actual == [
716 '',
717 '.. py:module:: target.autodoc_type_aliases',
718 '',
719 '',
720 '.. py:class:: Foo()',
721 ' :module: target.autodoc_type_aliases',
722 '',
723 ' docstring',
724 '',
725 '',
726 ' .. py:attribute:: Foo.attr1',
727 ' :module: target.autodoc_type_aliases',
728 ' :type: int',
729 '',
730 ' docstring',
731 '',
732 '',
733 ' .. py:attribute:: Foo.attr2',
734 ' :module: target.autodoc_type_aliases',
735 ' :type: int',
736 '',
737 ' docstring',
738 '',
739 '',
740 '.. py:function:: mult(x: int, y: int) -> int',
741 ' mult(x: float, y: float) -> float',
742 ' :module: target.autodoc_type_aliases',
743 '',
744 ' docstring',
745 '',
746 '',
747 '.. py:function:: read(r: ~io.BytesIO) -> ~io.StringIO',
748 ' :module: target.autodoc_type_aliases',
749 '',
750 ' docstring',
751 '',
752 '',
753 '.. py:function:: sum(x: int, y: int) -> int',
754 ' :module: target.autodoc_type_aliases',
755 '',
756 ' docstring',
757 '',
758 '',
759 '.. py:data:: variable',
760 ' :module: target.autodoc_type_aliases',
761 ' :type: int',
762 '',
763 ' docstring',
764 '',
765 '',
766 '.. py:data:: variable2',
767 ' :module: target.autodoc_type_aliases',
768 ' :type: int',
769 ' :value: None',
770 '',
771 ' docstring',
772 '',
773 '',
774 '.. py:data:: variable3',
775 ' :module: target.autodoc_type_aliases',
776 ' :type: int | None',
777 '',
778 ' docstring',
779 '',
780 ]
781
782 # define aliases
783 config = _AutodocConfig(
784 autodoc_type_aliases={
785 'myint': 'myint',
786 'io.StringIO': 'my.module.StringIO',
787 }
788 )
789 actual = do_autodoc(
790 'module', 'target.autodoc_type_aliases', config=config, options=options
791 )
792 assert actual == [
793 '',
794 '.. py:module:: target.autodoc_type_aliases',
795 '',
796 '',
797 '.. py:class:: Foo()',
798 ' :module: target.autodoc_type_aliases',
799 '',
800 ' docstring',
801 '',
802 '',
803 ' .. py:attribute:: Foo.attr1',
804 ' :module: target.autodoc_type_aliases',
805 ' :type: myint',
806 '',
807 ' docstring',
808 '',
809 '',
810 ' .. py:attribute:: Foo.attr2',
811 ' :module: target.autodoc_type_aliases',
812 ' :type: myint',
813 '',
814 ' docstring',
815 '',
816 '',
817 '.. py:function:: mult(x: myint, y: myint) -> myint',
818 ' mult(x: float, y: float) -> float',
819 ' :module: target.autodoc_type_aliases',
820 '',
821 ' docstring',
822 '',
823 '',
824 '.. py:function:: read(r: ~io.BytesIO) -> my.module.StringIO',
825 ' :module: target.autodoc_type_aliases',
826 '',
827 ' docstring',
828 '',
829 '',
830 '.. py:function:: sum(x: myint, y: myint) -> myint',
831 ' :module: target.autodoc_type_aliases',
832 '',
833 ' docstring',
834 '',
835 '',
836 '.. py:data:: variable',
837 ' :module: target.autodoc_type_aliases',
838 ' :type: myint',
839 '',
840 ' docstring',
841 '',
842 '',
843 '.. py:data:: variable2',
844 ' :module: target.autodoc_type_aliases',
845 ' :type: myint',
846 ' :value: None',
847 '',
848 ' docstring',
849 '',
850 '',
851 '.. py:data:: variable3',
852 ' :module: target.autodoc_type_aliases',
853 ' :type: myint | None',
854 '',
855 ' docstring',
856 '',
857 ]
858
859
860 def test_autodoc_default_options() -> None:
861 if sys.version_info[:3] >= (3, 12, 1):
862 list_of_weak_references = ' list of weak references to the object'
863 else:
864 list_of_weak_references = ' list of weak references to the object (if defined)' # fmt: skip
865
866 # no settings
867 actual = do_autodoc('class', 'target.enums.EnumCls')
868 assert ' .. py:attribute:: EnumCls.val1' not in actual
869 assert ' .. py:attribute:: EnumCls.val4' not in actual
870 actual = do_autodoc('class', 'target.CustomIter')
871 assert ' .. py:method:: target.CustomIter' not in actual
872 actual = do_autodoc('module', 'target')
873 assert '.. py:function:: function_to_be_imported(app)' not in actual
874
875 # with :members:
876 config = _AutodocConfig(autodoc_default_options={'members': True})
877 actual = do_autodoc('class', 'target.enums.EnumCls', config=config)
878 assert ' .. py:attribute:: EnumCls.val1' in actual
879 assert ' .. py:attribute:: EnumCls.val4' not in actual
880
881 # with :members: = True
882 config = _AutodocConfig(autodoc_default_options={'members': True})
883 actual = do_autodoc('class', 'target.enums.EnumCls', config=config)
884 assert ' .. py:attribute:: EnumCls.val1' in actual
885 assert ' .. py:attribute:: EnumCls.val4' not in actual
886
887 # with :members: and :undoc-members:
888 config = _AutodocConfig(
889 autodoc_default_options={'members': True, 'undoc-members': True}
890 )
891 actual = do_autodoc('class', 'target.enums.EnumCls', config=config)
892 assert ' .. py:attribute:: EnumCls.val1' in actual
893 assert ' .. py:attribute:: EnumCls.val4' in actual
894
895 # with :special-members:
896 # Note that :members: must be *on* for :special-members: to work.
897 config = _AutodocConfig(
898 autodoc_default_options={'members': True, 'special-members': True}
899 )
900 actual = do_autodoc('class', 'target.CustomIter', config=config)
901 assert ' .. py:method:: CustomIter.__init__()' in actual
902 assert ' Create a new `CustomIter`.' in actual
903 assert ' .. py:method:: CustomIter.__iter__()' in actual
904 assert ' Iterate squares of each value.' in actual
905 if not IS_PYPY:
906 assert ' .. py:attribute:: CustomIter.__weakref__' in actual
907 assert list_of_weak_references in actual
908
909 # :exclude-members: None - has no effect. Unlike :members:,
910 # :special-members:, etc. where None == "include all", here None means
911 # "no/false/off".
912 config = _AutodocConfig(
913 autodoc_default_options={'members': True, 'exclude-members': True}
914 )
915 actual = do_autodoc('class', 'target.enums.EnumCls', config=config)
916 assert ' .. py:attribute:: EnumCls.val1' in actual
917 assert ' .. py:attribute:: EnumCls.val4' not in actual
918 config = _AutodocConfig(
919 autodoc_default_options={
920 'members': True,
921 'special-members': True,
922 'exclude-members': True,
923 }
924 )
925 actual = do_autodoc('class', 'target.CustomIter', config=config)
926 assert ' .. py:method:: CustomIter.__init__()' in actual
927 assert ' Create a new `CustomIter`.' in actual
928 assert ' .. py:method:: CustomIter.__iter__()' in actual
929 assert ' Iterate squares of each value.' in actual
930 if not IS_PYPY:
931 assert ' .. py:attribute:: CustomIter.__weakref__' in actual
932 assert list_of_weak_references in actual
933 assert ' .. py:method:: CustomIter.snafucate()' in actual
934 assert ' Makes this snafucated.' in actual
935
936
937 def test_autodoc_default_options_with_values() -> None:
938 if sys.version_info[:3] >= (3, 12, 1):
939 list_of_weak_references = ' list of weak references to the object'
940 else:
941 list_of_weak_references = ' list of weak references to the object (if defined)' # fmt: skip
942
943 # with :members:
944 config = _AutodocConfig(autodoc_default_options={'members': 'val1,val2'})
945 actual = do_autodoc('class', 'target.enums.EnumCls', config=config)
946 assert ' .. py:attribute:: EnumCls.val1' in actual
947 assert ' .. py:attribute:: EnumCls.val2' in actual
948 assert ' .. py:attribute:: EnumCls.val3' not in actual
949 assert ' .. py:attribute:: EnumCls.val4' not in actual
950
951 # with :member-order:
952 config = _AutodocConfig(
953 autodoc_default_options={'members': True, 'member-order': 'bysource'}
954 )
955 actual = do_autodoc('class', 'target.Class', config=config)
956 assert [line for line in actual if '::' in line] == [
957 '.. py:class:: Class(arg)',
958 ' .. py:method:: Class.meth()',
959 ' .. py:method:: Class.skipmeth()',
960 ' .. py:method:: Class.excludemeth()',
961 ' .. py:attribute:: Class.attr',
962 ' .. py:attribute:: Class.docattr',
963 ' .. py:attribute:: Class.udocattr',
964 ' .. py:attribute:: Class.mdocattr',
965 ' .. py:method:: Class.moore(a, e, f) -> happiness',
966 ' .. py:attribute:: Class.inst_attr_inline',
967 ' .. py:attribute:: Class.inst_attr_comment',
968 ' .. py:attribute:: Class.inst_attr_string',
969 ]
970
971 # with :special-members:
972 config = _AutodocConfig(
973 autodoc_default_options={'special-members': '__init__,__iter__'}
974 )
975 actual = do_autodoc('class', 'target.CustomIter', config=config)
976 assert ' .. py:method:: CustomIter.__init__()' in actual
977 assert ' Create a new `CustomIter`.' in actual
978 assert ' .. py:method:: CustomIter.__iter__()' in actual
979 assert ' Iterate squares of each value.' in actual
980 if not IS_PYPY:
981 assert ' .. py:attribute:: CustomIter.__weakref__' not in actual
982 assert list_of_weak_references not in actual
983
984 # with :exclude-members:
985 config = _AutodocConfig(
986 autodoc_default_options={'members': True, 'exclude-members': 'val1'}
987 )
988 actual = do_autodoc('class', 'target.enums.EnumCls', config=config)
989 assert ' .. py:attribute:: EnumCls.val1' not in actual
990 assert ' .. py:attribute:: EnumCls.val2' in actual
991 assert ' .. py:attribute:: EnumCls.val3' in actual
992 assert ' .. py:attribute:: EnumCls.val4' not in actual
993 config = _AutodocConfig(
994 autodoc_default_options={
995 'members': True,
996 'special-members': True,
997 'exclude-members': '__weakref__,snafucate',
998 }
999 )
1000 actual = do_autodoc('class', 'target.CustomIter', config=config)
1001 assert ' .. py:method:: CustomIter.__init__()' in actual
1002 assert ' Create a new `CustomIter`.' in actual
1003 assert ' .. py:method:: CustomIter.__iter__()' in actual
1004 assert ' Iterate squares of each value.' in actual
1005 if not IS_PYPY:
1006 assert ' .. py:attribute:: CustomIter.__weakref__' not in actual
1007 assert list_of_weak_references not in actual
1008 assert ' .. py:method:: CustomIter.snafucate()' not in actual
1009 assert ' Makes this snafucated.' not in actual