Debian Codegraph @

tests/test_domains/test_domain_py_pyobject.py

raw
1 """Tests the Python Domain"""
2
3 from __future__ import annotations
4
5 import pytest
6 from docutils import nodes
7
8 from sphinx import addnodes
9 from sphinx.addnodes import (
10 desc,
11 desc_addname,
12 desc_annotation,
13 desc_content,
14 desc_name,
15 desc_parameterlist,
16 desc_sig_keyword,
17 desc_sig_punctuation,
18 desc_sig_space,
19 desc_signature,
20 pending_xref,
21 )
22 from sphinx.testing import restructuredtext
23 from sphinx.testing.util import assert_node
24
25 from tests.utils import extract_node
26
27
28 @pytest.mark.sphinx('html', testroot='_blank')
29 def test_pyexception_signature(app):
30 text = '.. py:exception:: builtins.IOError'
31 doctree = restructuredtext.parse(app, text)
32 assert_node(
33 doctree,
34 (
35 addnodes.index,
36 [
37 desc,
38 (
39 [
40 desc_signature,
41 (
42 [
43 desc_annotation,
44 ([desc_sig_keyword, 'exception'], desc_sig_space),
45 ],
46 [desc_addname, 'builtins.'],
47 [desc_name, 'IOError'],
48 ),
49 ],
50 desc_content,
51 ),
52 ],
53 ),
54 )
55 assert_node(
56 doctree[1],
57 desc,
58 desctype='exception',
59 domain='py',
60 objtype='exception',
61 no_index=False,
62 )
63
64
65 @pytest.mark.sphinx('html', testroot='_blank')
66 def test_pydata_signature(app):
67 text = '.. py:data:: version\n :type: int\n :value: 1\n'
68 doctree = restructuredtext.parse(app, text)
69 assert_node(
70 doctree,
71 (
72 addnodes.index,
73 [
74 desc,
75 (
76 [
77 desc_signature,
78 (
79 [desc_name, 'version'],
80 [
81 desc_annotation,
82 (
83 [desc_sig_punctuation, ':'],
84 desc_sig_space,
85 [pending_xref, 'int'],
86 ),
87 ],
88 [
89 desc_annotation,
90 (
91 desc_sig_space,
92 [desc_sig_punctuation, '='],
93 desc_sig_space,
94 '1',
95 ),
96 ],
97 ),
98 ],
99 desc_content,
100 ),
101 ],
102 ),
103 )
104 assert_node(
105 doctree[1],
106 addnodes.desc,
107 desctype='data',
108 domain='py',
109 objtype='data',
110 no_index=False,
111 )
112
113
114 @pytest.mark.sphinx('html', testroot='_blank')
115 def test_pydata_signature_old(app):
116 text = '.. py:data:: version\n :annotation: = 1\n'
117 doctree = restructuredtext.parse(app, text)
118 assert_node(
119 doctree,
120 (
121 addnodes.index,
122 [
123 desc,
124 (
125 [
126 desc_signature,
127 (
128 [desc_name, 'version'],
129 [desc_annotation, (desc_sig_space, '= 1')],
130 ),
131 ],
132 desc_content,
133 ),
134 ],
135 ),
136 )
137 assert_node(
138 doctree[1],
139 addnodes.desc,
140 desctype='data',
141 domain='py',
142 objtype='data',
143 no_index=False,
144 )
145
146
147 @pytest.mark.sphinx('html', testroot='_blank')
148 def test_pydata_with_union_type_operator(app):
149 text = '.. py:data:: version\n :type: int | str'
150 doctree = restructuredtext.parse(app, text)
151 assert_node(
152 extract_node(doctree, 1, 0),
153 (
154 [desc_name, 'version'],
155 [
156 desc_annotation,
157 (
158 [desc_sig_punctuation, ':'],
159 desc_sig_space,
160 [pending_xref, 'int'],
161 desc_sig_space,
162 [desc_sig_punctuation, '|'],
163 desc_sig_space,
164 [pending_xref, 'str'],
165 ),
166 ],
167 ),
168 )
169
170
171 @pytest.mark.sphinx('html', testroot='_blank')
172 def test_pyobject_prefix(app):
173 text = (
174 '.. py:class:: Foo\n\n .. py:method:: Foo.say\n .. py:method:: FooBar.say'
175 )
176 doctree = restructuredtext.parse(app, text)
177 assert_node(
178 doctree,
179 (
180 addnodes.index,
181 [
182 desc,
183 (
184 [
185 desc_signature,
186 (
187 [
188 desc_annotation,
189 ([desc_sig_keyword, 'class'], desc_sig_space),
190 ],
191 [desc_name, 'Foo'],
192 ),
193 ],
194 [desc_content, (addnodes.index, desc, addnodes.index, desc)],
195 ),
196 ],
197 ),
198 )
199 # prefix is stripped
200 assert extract_node(doctree, 1, 1, 1).astext().strip() == 'say()'
201 # not stripped
202 assert extract_node(doctree, 1, 1, 3).astext().strip() == 'FooBar.say()'
203
204
205 @pytest.mark.sphinx('html', testroot='_blank')
206 def test_pydata(app):
207 text = '.. py:module:: example\n.. py:data:: var\n :type: int\n'
208 domain = app.env.domains.python_domain
209 doctree = restructuredtext.parse(app, text)
210 assert_node(
211 doctree,
212 (
213 addnodes.index,
214 addnodes.index,
215 nodes.target,
216 [
217 desc,
218 (
219 [
220 desc_signature,
221 (
222 [desc_addname, 'example.'],
223 [desc_name, 'var'],
224 [
225 desc_annotation,
226 (
227 [desc_sig_punctuation, ':'],
228 desc_sig_space,
229 [pending_xref, 'int'],
230 ),
231 ],
232 ),
233 ],
234 [desc_content, ()],
235 ),
236 ],
237 ),
238 )
239 assert_node(
240 extract_node(doctree, 3, 0, 2, 2), pending_xref, **{'py:module': 'example'}
241 )
242 assert 'example.var' in domain.objects
243 assert domain.objects['example.var'] == ('index', 'example.var', 'data', False)
244
245
246 @pytest.mark.sphinx('html', testroot='_blank')
247 def test_pyclass_options(app):
248 text = '.. py:class:: Class1\n.. py:class:: Class2\n :final:\n'
249 domain = app.env.domains.python_domain
250 doctree = restructuredtext.parse(app, text)
251 assert_node(
252 doctree,
253 (
254 addnodes.index,
255 [
256 desc,
257 (
258 [
259 desc_signature,
260 (
261 [
262 desc_annotation,
263 ([desc_sig_keyword, 'class'], desc_sig_space),
264 ],
265 [desc_name, 'Class1'],
266 ),
267 ],
268 [desc_content, ()],
269 ),
270 ],
271 addnodes.index,
272 [
273 desc,
274 (
275 [
276 desc_signature,
277 (
278 [
279 desc_annotation,
280 (
281 [desc_sig_keyword, 'final'],
282 desc_sig_space,
283 [desc_sig_keyword, 'class'],
284 desc_sig_space,
285 ),
286 ],
287 [desc_name, 'Class2'],
288 ),
289 ],
290 [desc_content, ()],
291 ),
292 ],
293 ),
294 )
295
296 # class
297 assert_node(
298 doctree[0],
299 addnodes.index,
300 entries=[('single', 'Class1 (built-in class)', 'Class1', '', None)],
301 )
302 assert 'Class1' in domain.objects
303 assert domain.objects['Class1'] == ('index', 'Class1', 'class', False)
304
305 # :final:
306 assert_node(
307 doctree[2],
308 addnodes.index,
309 entries=[('single', 'Class2 (built-in class)', 'Class2', '', None)],
310 )
311 assert 'Class2' in domain.objects
312 assert domain.objects['Class2'] == ('index', 'Class2', 'class', False)
313
314
315 @pytest.mark.sphinx('html', testroot='_blank')
316 def test_pymethod_options(app):
317 text = (
318 '.. py:class:: Class\n'
319 '\n'
320 ' .. py:method:: meth1\n'
321 ' .. py:method:: meth2\n'
322 ' :classmethod:\n'
323 ' .. py:method:: meth3\n'
324 ' :staticmethod:\n'
325 ' .. py:method:: meth4\n'
326 ' :async:\n'
327 ' .. py:method:: meth5\n'
328 ' :abstractmethod:\n'
329 ' .. py:method:: meth6\n'
330 ' :final:\n'
331 )
332 domain = app.env.domains.python_domain
333 doctree = restructuredtext.parse(app, text)
334 assert_node(
335 doctree,
336 (
337 addnodes.index,
338 [
339 desc,
340 (
341 [
342 desc_signature,
343 (
344 [
345 desc_annotation,
346 ([desc_sig_keyword, 'class'], desc_sig_space),
347 ],
348 [desc_name, 'Class'],
349 ),
350 ],
351 [
352 desc_content,
353 (
354 addnodes.index,
355 desc,
356 addnodes.index,
357 desc,
358 addnodes.index,
359 desc,
360 addnodes.index,
361 desc,
362 addnodes.index,
363 desc,
364 addnodes.index,
365 desc,
366 ),
367 ],
368 ),
369 ],
370 ),
371 )
372
373 # method
374 assert_node(
375 extract_node(doctree, 1, 1, 0),
376 addnodes.index,
377 entries=[('single', 'meth1() (Class method)', 'Class.meth1', '', None)],
378 )
379 assert_node(
380 extract_node(doctree, 1, 1, 1),
381 (
382 [desc_signature, ([desc_name, 'meth1'], [desc_parameterlist, ()])],
383 [desc_content, ()],
384 ),
385 )
386 assert 'Class.meth1' in domain.objects
387 assert domain.objects['Class.meth1'] == ('index', 'Class.meth1', 'method', False)
388
389 # :classmethod:
390 assert_node(
391 extract_node(doctree, 1, 1, 2),
392 addnodes.index,
393 entries=[('single', 'meth2() (Class class method)', 'Class.meth2', '', None)],
394 )
395 assert_node(
396 extract_node(doctree, 1, 1, 3),
397 (
398 [
399 desc_signature,
400 (
401 [
402 desc_annotation,
403 ([desc_sig_keyword, 'classmethod'], desc_sig_space),
404 ],
405 [desc_name, 'meth2'],
406 [desc_parameterlist, ()],
407 ),
408 ],
409 [desc_content, ()],
410 ),
411 )
412 assert 'Class.meth2' in domain.objects
413 assert domain.objects['Class.meth2'] == ('index', 'Class.meth2', 'method', False)
414
415 # :staticmethod:
416 assert_node(
417 extract_node(doctree, 1, 1, 4),
418 addnodes.index,
419 entries=[('single', 'meth3() (Class static method)', 'Class.meth3', '', None)],
420 )
421 assert_node(
422 extract_node(doctree, 1, 1, 5),
423 (
424 [
425 desc_signature,
426 (
427 [desc_annotation, ([desc_sig_keyword, 'static'], desc_sig_space)],
428 [desc_name, 'meth3'],
429 [desc_parameterlist, ()],
430 ),
431 ],
432 [desc_content, ()],
433 ),
434 )
435 assert 'Class.meth3' in domain.objects
436 assert domain.objects['Class.meth3'] == ('index', 'Class.meth3', 'method', False)
437
438 # :async:
439 assert_node(
440 extract_node(doctree, 1, 1, 6),
441 addnodes.index,
442 entries=[('single', 'meth4() (Class method)', 'Class.meth4', '', None)],
443 )
444 assert_node(
445 extract_node(doctree, 1, 1, 7),
446 (
447 [
448 desc_signature,
449 (
450 [desc_annotation, ([desc_sig_keyword, 'async'], desc_sig_space)],
451 [desc_name, 'meth4'],
452 [desc_parameterlist, ()],
453 ),
454 ],
455 [desc_content, ()],
456 ),
457 )
458 assert 'Class.meth4' in domain.objects
459 assert domain.objects['Class.meth4'] == ('index', 'Class.meth4', 'method', False)
460
461 # :abstractmethod:
462 assert_node(
463 extract_node(doctree, 1, 1, 8),
464 addnodes.index,
465 entries=[('single', 'meth5() (Class method)', 'Class.meth5', '', None)],
466 )
467 assert_node(
468 extract_node(doctree, 1, 1, 9),
469 (
470 [
471 desc_signature,
472 (
473 [
474 desc_annotation,
475 ([desc_sig_keyword, 'abstractmethod'], desc_sig_space),
476 ],
477 [desc_name, 'meth5'],
478 [desc_parameterlist, ()],
479 ),
480 ],
481 [desc_content, ()],
482 ),
483 )
484 assert 'Class.meth5' in domain.objects
485 assert domain.objects['Class.meth5'] == ('index', 'Class.meth5', 'method', False)
486
487 # :final:
488 assert_node(
489 extract_node(doctree, 1, 1, 10),
490 addnodes.index,
491 entries=[('single', 'meth6() (Class method)', 'Class.meth6', '', None)],
492 )
493 assert_node(
494 extract_node(doctree, 1, 1, 11),
495 (
496 [
497 desc_signature,
498 (
499 [desc_annotation, ([desc_sig_keyword, 'final'], desc_sig_space)],
500 [desc_name, 'meth6'],
501 [desc_parameterlist, ()],
502 ),
503 ],
504 [desc_content, ()],
505 ),
506 )
507 assert 'Class.meth6' in domain.objects
508 assert domain.objects['Class.meth6'] == ('index', 'Class.meth6', 'method', False)
509
510
511 @pytest.mark.sphinx('html', testroot='_blank')
512 def test_pyclassmethod(app):
513 text = '.. py:class:: Class\n\n .. py:classmethod:: meth\n'
514 domain = app.env.domains.python_domain
515 doctree = restructuredtext.parse(app, text)
516 assert_node(
517 doctree,
518 (
519 addnodes.index,
520 [
521 desc,
522 (
523 [
524 desc_signature,
525 (
526 [
527 desc_annotation,
528 ([desc_sig_keyword, 'class'], desc_sig_space),
529 ],
530 [desc_name, 'Class'],
531 ),
532 ],
533 [desc_content, (addnodes.index, desc)],
534 ),
535 ],
536 ),
537 )
538 assert_node(
539 extract_node(doctree, 1, 1, 0),
540 addnodes.index,
541 entries=[('single', 'meth() (Class class method)', 'Class.meth', '', None)],
542 )
543 assert_node(
544 extract_node(doctree, 1, 1, 1),
545 (
546 [
547 desc_signature,
548 (
549 [
550 desc_annotation,
551 ([desc_sig_keyword, 'classmethod'], desc_sig_space),
552 ],
553 [desc_name, 'meth'],
554 [desc_parameterlist, ()],
555 ),
556 ],
557 [desc_content, ()],
558 ),
559 )
560 assert 'Class.meth' in domain.objects
561 assert domain.objects['Class.meth'] == ('index', 'Class.meth', 'method', False)
562
563
564 @pytest.mark.sphinx('html', testroot='_blank')
565 def test_pystaticmethod(app):
566 text = '.. py:class:: Class\n\n .. py:staticmethod:: meth\n'
567 domain = app.env.domains.python_domain
568 doctree = restructuredtext.parse(app, text)
569 assert_node(
570 doctree,
571 (
572 addnodes.index,
573 [
574 desc,
575 (
576 [
577 desc_signature,
578 (
579 [
580 desc_annotation,
581 ([desc_sig_keyword, 'class'], desc_sig_space),
582 ],
583 [desc_name, 'Class'],
584 ),
585 ],
586 [desc_content, (addnodes.index, desc)],
587 ),
588 ],
589 ),
590 )
591 assert_node(
592 extract_node(doctree, 1, 1, 0),
593 addnodes.index,
594 entries=[('single', 'meth() (Class static method)', 'Class.meth', '', None)],
595 )
596 assert_node(
597 extract_node(doctree, 1, 1, 1),
598 (
599 [
600 desc_signature,
601 (
602 [desc_annotation, ([desc_sig_keyword, 'static'], desc_sig_space)],
603 [desc_name, 'meth'],
604 [desc_parameterlist, ()],
605 ),
606 ],
607 [desc_content, ()],
608 ),
609 )
610 assert 'Class.meth' in domain.objects
611 assert domain.objects['Class.meth'] == ('index', 'Class.meth', 'method', False)
612
613
614 @pytest.mark.sphinx('html', testroot='_blank')
615 def test_pyattribute(app):
616 text = (
617 '.. py:class:: Class\n'
618 '\n'
619 ' .. py:attribute:: attr\n'
620 ' :type: Optional[str]\n'
621 " :value: ''\n"
622 )
623 domain = app.env.domains.python_domain
624 doctree = restructuredtext.parse(app, text)
625 assert_node(
626 doctree,
627 (
628 addnodes.index,
629 [
630 desc,
631 (
632 [
633 desc_signature,
634 (
635 [
636 desc_annotation,
637 ([desc_sig_keyword, 'class'], desc_sig_space),
638 ],
639 [desc_name, 'Class'],
640 ),
641 ],
642 [desc_content, (addnodes.index, desc)],
643 ),
644 ],
645 ),
646 )
647 assert_node(
648 extract_node(doctree, 1, 1, 0),
649 addnodes.index,
650 entries=[('single', 'attr (Class attribute)', 'Class.attr', '', None)],
651 )
652 assert_node(
653 extract_node(doctree, 1, 1, 1),
654 (
655 [
656 desc_signature,
657 (
658 [desc_name, 'attr'],
659 [
660 desc_annotation,
661 (
662 [desc_sig_punctuation, ':'],
663 desc_sig_space,
664 [pending_xref, 'str'],
665 desc_sig_space,
666 [desc_sig_punctuation, '|'],
667 desc_sig_space,
668 [pending_xref, 'None'],
669 ),
670 ],
671 [
672 desc_annotation,
673 (
674 desc_sig_space,
675 [desc_sig_punctuation, '='],
676 desc_sig_space,
677 "''",
678 ),
679 ],
680 ),
681 ],
682 [desc_content, ()],
683 ),
684 )
685 assert_node(
686 extract_node(doctree, 1, 1, 1, 0, 1, 2), pending_xref, **{'py:class': 'Class'}
687 )
688 assert_node(
689 extract_node(doctree, 1, 1, 1, 0, 1, 6), pending_xref, **{'py:class': 'Class'}
690 )
691 assert 'Class.attr' in domain.objects
692 assert domain.objects['Class.attr'] == ('index', 'Class.attr', 'attribute', False)
693
694
695 @pytest.mark.sphinx('html', testroot='_blank')
696 def test_pyproperty(app):
697 text = (
698 '.. py:class:: Class\n'
699 '\n'
700 ' .. py:property:: prop1\n'
701 ' :abstractmethod:\n'
702 ' :type: str\n'
703 '\n'
704 ' .. py:property:: prop2\n'
705 ' :classmethod:\n'
706 ' :type: str\n'
707 )
708 domain = app.env.domains.python_domain
709 doctree = restructuredtext.parse(app, text)
710 assert_node(
711 doctree,
712 (
713 addnodes.index,
714 [
715 desc,
716 (
717 [
718 desc_signature,
719 (
720 [
721 desc_annotation,
722 ([desc_sig_keyword, 'class'], desc_sig_space),
723 ],
724 [desc_name, 'Class'],
725 ),
726 ],
727 [desc_content, (addnodes.index, desc, addnodes.index, desc)],
728 ),
729 ],
730 ),
731 )
732 assert_node(
733 extract_node(doctree, 1, 1, 0),
734 addnodes.index,
735 entries=[('single', 'prop1 (Class property)', 'Class.prop1', '', None)],
736 )
737 assert_node(
738 extract_node(doctree, 1, 1, 1),
739 (
740 [
741 desc_signature,
742 (
743 [
744 desc_annotation,
745 (
746 [desc_sig_keyword, 'abstract'],
747 desc_sig_space,
748 [desc_sig_keyword, 'property'],
749 desc_sig_space,
750 ),
751 ],
752 [desc_name, 'prop1'],
753 [
754 desc_annotation,
755 (
756 [desc_sig_punctuation, ':'],
757 desc_sig_space,
758 [pending_xref, 'str'],
759 ),
760 ],
761 ),
762 ],
763 [desc_content, ()],
764 ),
765 )
766 assert_node(
767 extract_node(doctree, 1, 1, 2),
768 addnodes.index,
769 entries=[('single', 'prop2 (Class property)', 'Class.prop2', '', None)],
770 )
771 assert_node(
772 extract_node(doctree, 1, 1, 3),
773 (
774 [
775 desc_signature,
776 (
777 [
778 desc_annotation,
779 (
780 [desc_sig_keyword, 'class'],
781 desc_sig_space,
782 [desc_sig_keyword, 'property'],
783 desc_sig_space,
784 ),
785 ],
786 [desc_name, 'prop2'],
787 [
788 desc_annotation,
789 (
790 [desc_sig_punctuation, ':'],
791 desc_sig_space,
792 [pending_xref, 'str'],
793 ),
794 ],
795 ),
796 ],
797 [desc_content, ()],
798 ),
799 )
800 assert 'Class.prop1' in domain.objects
801 assert domain.objects['Class.prop1'] == ('index', 'Class.prop1', 'property', False)
802 assert 'Class.prop2' in domain.objects
803 assert domain.objects['Class.prop2'] == ('index', 'Class.prop2', 'property', False)
804
805
806 @pytest.mark.sphinx('html', testroot='_blank')
807 def test_py_type_alias(app):
808 text = (
809 '.. py:module:: example\n'
810 '.. py:type:: Alias1\n'
811 ' :canonical: list[str | int]\n'
812 '\n'
813 '.. py:class:: Class\n'
814 '\n'
815 ' .. py:type:: Alias2\n'
816 ' :canonical: int\n'
817 )
818 domain = app.env.domains.python_domain
819 doctree = restructuredtext.parse(app, text)
820 assert_node(
821 doctree,
822 (
823 addnodes.index,
824 addnodes.index,
825 nodes.target,
826 [
827 desc,
828 (
829 [
830 desc_signature,
831 (
832 [
833 desc_annotation,
834 ([desc_sig_keyword, 'type'], desc_sig_space),
835 ],
836 [desc_addname, 'example.'],
837 [desc_name, 'Alias1'],
838 [
839 desc_annotation,
840 (
841 desc_sig_space,
842 [desc_sig_punctuation, '='],
843 desc_sig_space,
844 [pending_xref, 'list'],
845 [desc_sig_punctuation, '['],
846 [pending_xref, 'str'],
847 desc_sig_space,
848 [desc_sig_punctuation, '|'],
849 desc_sig_space,
850 [pending_xref, 'int'],
851 [desc_sig_punctuation, ']'],
852 ),
853 ],
854 ),
855 ],
856 [desc_content, ()],
857 ),
858 ],
859 addnodes.index,
860 [
861 desc,
862 (
863 [
864 desc_signature,
865 (
866 [
867 desc_annotation,
868 ([desc_sig_keyword, 'class'], desc_sig_space),
869 ],
870 [desc_addname, 'example.'],
871 [desc_name, 'Class'],
872 ),
873 ],
874 [desc_content, (addnodes.index, desc)],
875 ),
876 ],
877 ),
878 )
879 assert_node(
880 extract_node(doctree, 5, 1, 0),
881 addnodes.index,
882 entries=[
883 (
884 'single',
885 'Alias2 (type alias in example.Class)',
886 'example.Class.Alias2',
887 '',
888 None,
889 )
890 ],
891 )
892 assert_node(
893 extract_node(doctree, 5, 1, 1),
894 (
895 [
896 desc_signature,
897 (
898 [desc_annotation, ([desc_sig_keyword, 'type'], desc_sig_space)],
899 [desc_name, 'Alias2'],
900 [
901 desc_annotation,
902 (
903 desc_sig_space,
904 [desc_sig_punctuation, '='],
905 desc_sig_space,
906 [pending_xref, 'int'],
907 ),
908 ],
909 ),
910 ],
911 [desc_content, ()],
912 ),
913 )
914 assert 'example.Alias1' in domain.objects
915 assert domain.objects['example.Alias1'] == (
916 'index',
917 'example.Alias1',
918 'type',
919 False,
920 )
921 assert 'example.Class.Alias2' in domain.objects
922 assert domain.objects['example.Class.Alias2'] == (
923 'index',
924 'example.Class.Alias2',
925 'type',
926 False,
927 )
928
929
930 @pytest.mark.sphinx('html', testroot='domain-py', freshenv=True)
931 def test_domain_py_type_alias(app):
932 app.build(force_all=True)
933
934 content = (app.outdir / 'type_alias.html').read_text(encoding='utf8')
935 assert (
936 '<span class="property"><span class="k"><span class="pre">type</span></span><span class="w"> </span></span>'
937 '<span class="sig-prename descclassname"><span class="pre">module_one.</span></span>'
938 '<span class="sig-name descname"><span class="pre">MyAlias</span></span>'
939 '<span class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span>'
940 '<span class="w"> </span><span class="pre">list</span>'
941 '<span class="p"><span class="pre">[</span></span>'
942 '<span class="pre">int</span><span class="w"> </span>'
943 '<span class="p"><span class="pre">|</span></span><span class="w"> </span>'
944 '<a class="reference internal" href="#module_two.SomeClass" title="module_two.SomeClass">'
945 '<span class="pre">module_two.SomeClass</span></a>'
946 '<span class="p"><span class="pre">]</span></span></span>'
947 ) in content
948 assert app.warning.getvalue() == ''
949
950
951 @pytest.mark.sphinx('html', testroot='_blank')
952 def test_pydecorator_signature(app):
953 text = '.. py:decorator:: deco'
954 domain = app.env.domains.python_domain
955 doctree = restructuredtext.parse(app, text)
956 assert_node(
957 doctree,
958 (
959 addnodes.index,
960 [
961 desc,
962 (
963 [desc_signature, ([desc_addname, '@'], [desc_name, 'deco'])],
964 desc_content,
965 ),
966 ],
967 ),
968 )
969 assert_node(
970 doctree[1],
971 addnodes.desc,
972 desctype='function',
973 domain='py',
974 objtype='function',
975 no_index=False,
976 )
977
978 assert 'deco' in domain.objects
979 assert domain.objects['deco'] == ('index', 'deco', 'function', False)
980
981
982 @pytest.mark.sphinx('html', testroot='_blank')
983 def test_pydecoratormethod_signature(app):
984 text = '.. py:decoratormethod:: deco'
985 domain = app.env.domains.python_domain
986 doctree = restructuredtext.parse(app, text)
987 assert_node(
988 doctree,
989 (
990 addnodes.index,
991 [
992 desc,
993 (
994 [desc_signature, ([desc_addname, '@'], [desc_name, 'deco'])],
995 desc_content,
996 ),
997 ],
998 ),
999 )
1000 assert_node(
1001 doctree[1],
1002 addnodes.desc,
1003 desctype='method',
1004 domain='py',
1005 objtype='method',
1006 no_index=False,
1007 )
1008
1009 assert 'deco' in domain.objects
1010 assert domain.objects['deco'] == ('index', 'deco', 'method', False)
1011
1012
1013 @pytest.mark.sphinx('html', testroot='_blank')
1014 def test_pycurrentmodule(app):
1015 text = (
1016 '.. py:module:: Other\n'
1017 '\n'
1018 '.. py:module:: Module\n'
1019 '.. py:class:: A\n'
1020 '\n'
1021 ' .. py:method:: m1\n'
1022 ' .. py:method:: m2\n'
1023 '\n'
1024 '.. py:currentmodule:: Other\n'
1025 '\n'
1026 '.. py:class:: B\n'
1027 '\n'
1028 ' .. py:method:: m3\n'
1029 ' .. py:method:: m4\n'
1030 )
1031 domain = app.env.domains.python_domain
1032 doctree = restructuredtext.parse(app, text)
1033 print(doctree)
1034 assert_node(
1035 doctree,
1036 (
1037 addnodes.index,
1038 addnodes.index,
1039 addnodes.index,
1040 nodes.target,
1041 nodes.target,
1042 [
1043 desc,
1044 (
1045 [
1046 desc_signature,
1047 (
1048 [
1049 desc_annotation,
1050 ([desc_sig_keyword, 'class'], desc_sig_space),
1051 ],
1052 [desc_addname, 'Module.'],
1053 [desc_name, 'A'],
1054 ),
1055 ],
1056 [
1057 desc_content,
1058 (
1059 addnodes.index,
1060 [
1061 desc,
1062 (
1063 [
1064 desc_signature,
1065 (
1066 [desc_name, 'm1'],
1067 [desc_parameterlist, ()],
1068 ),
1069 ],
1070 [desc_content, ()],
1071 ),
1072 ],
1073 addnodes.index,
1074 [
1075 desc,
1076 (
1077 [
1078 desc_signature,
1079 (
1080 [desc_name, 'm2'],
1081 [desc_parameterlist, ()],
1082 ),
1083 ],
1084 [desc_content, ()],
1085 ),
1086 ],
1087 ),
1088 ],
1089 ),
1090 ],
1091 addnodes.index,
1092 [
1093 desc,
1094 (
1095 [
1096 desc_signature,
1097 (
1098 [
1099 desc_annotation,
1100 ([desc_sig_keyword, 'class'], desc_sig_space),
1101 ],
1102 [desc_addname, 'Other.'],
1103 [desc_name, 'B'],
1104 ),
1105 ],
1106 [
1107 desc_content,
1108 (
1109 addnodes.index,
1110 [
1111 desc,
1112 (
1113 [
1114 desc_signature,
1115 (
1116 [desc_name, 'm3'],
1117 [desc_parameterlist, ()],
1118 ),
1119 ],
1120 [desc_content, ()],
1121 ),
1122 ],
1123 addnodes.index,
1124 [
1125 desc,
1126 (
1127 [
1128 desc_signature,
1129 (
1130 [desc_name, 'm4'],
1131 [desc_parameterlist, ()],
1132 ),
1133 ],
1134 [desc_content, ()],
1135 ),
1136 ],
1137 ),
1138 ],
1139 ),
1140 ],
1141 ),
1142 )
1143 assert 'Module' in domain.objects
1144 assert domain.objects['Module'] == ('index', 'module-Module', 'module', False)
1145 assert 'Other' in domain.objects
1146 assert domain.objects['Other'] == ('index', 'module-Other', 'module', False)
1147 assert 'Module.A' in domain.objects
1148 assert domain.objects['Module.A'] == ('index', 'Module.A', 'class', False)
1149 assert 'Other.B' in domain.objects
1150 assert domain.objects['Other.B'] == ('index', 'Other.B', 'class', False)
1151 assert 'Module.A.m1' in domain.objects
1152 assert domain.objects['Module.A.m1'] == ('index', 'Module.A.m1', 'method', False)
1153 assert 'Module.A.m2' in domain.objects
1154 assert domain.objects['Module.A.m2'] == ('index', 'Module.A.m2', 'method', False)
1155 assert 'Other.B.m3' in domain.objects
1156 assert domain.objects['Other.B.m3'] == ('index', 'Other.B.m3', 'method', False)
1157 assert 'Other.B.m4' in domain.objects
1158 assert domain.objects['Other.B.m4'] == ('index', 'Other.B.m4', 'method', False)