Debian Codegraph @

tests/test_domains/test_domain_py_fields.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_sig_keyword,
16 desc_sig_punctuation,
17 desc_sig_space,
18 desc_signature,
19 pending_xref,
20 )
21 from sphinx.testing import restructuredtext
22 from sphinx.testing.util import assert_node
23
24 from tests.utils import extract_node
25
26
27 @pytest.mark.sphinx('html', testroot='_blank')
28 def test_info_field_list(app):
29 text = (
30 '.. py:module:: example\n'
31 '.. py:class:: Class\n'
32 '\n'
33 ' :meta blah: this meta-field must not show up in the toc-tree\n'
34 ' :param str name: blah blah\n'
35 ' :meta another meta field:\n'
36 ' :param age: blah blah\n'
37 ' :type age: int\n'
38 ' :param items: blah blah\n'
39 ' :type items: Tuple[str, ...]\n'
40 ' :param Dict[str, str] params: blah blah\n'
41 )
42 doctree = restructuredtext.parse(app, text)
43 print(doctree)
44
45 assert_node(
46 doctree,
47 (
48 addnodes.index,
49 addnodes.index,
50 nodes.target,
51 [
52 desc,
53 (
54 [
55 desc_signature,
56 (
57 [
58 desc_annotation,
59 ([desc_sig_keyword, 'class'], desc_sig_space),
60 ],
61 [desc_addname, 'example.'],
62 [desc_name, 'Class'],
63 ),
64 ],
65 [desc_content, nodes.field_list, nodes.field],
66 ),
67 ],
68 ),
69 )
70 assert_node(
71 extract_node(doctree, 3, 1, 0, 0),
72 (
73 [nodes.field_name, 'Parameters'],
74 [
75 nodes.field_body,
76 nodes.bullet_list,
77 (
78 [nodes.list_item, nodes.paragraph],
79 [nodes.list_item, nodes.paragraph],
80 [nodes.list_item, nodes.paragraph],
81 [nodes.list_item, nodes.paragraph],
82 ),
83 ],
84 ),
85 )
86
87 # :param str name:
88 assert_node(
89 extract_node(doctree, 3, 1, 0, 0, 1, 0, 0, 0),
90 (
91 [addnodes.literal_strong, 'name'],
92 ' (',
93 [pending_xref, addnodes.literal_emphasis, 'str'],
94 ')',
95 ' -- ',
96 'blah blah',
97 ),
98 )
99 assert_node(
100 extract_node(doctree, 3, 1, 0, 0, 1, 0, 0, 0, 2),
101 pending_xref,
102 refdomain='py',
103 reftype='class',
104 reftarget='str',
105 **{'py:module': 'example', 'py:class': 'Class'},
106 )
107
108 # :param age: + :type age:
109 assert_node(
110 extract_node(doctree, 3, 1, 0, 0, 1, 0, 1, 0),
111 (
112 [addnodes.literal_strong, 'age'],
113 ' (',
114 [pending_xref, addnodes.literal_emphasis, 'int'],
115 ')',
116 ' -- ',
117 'blah blah',
118 ),
119 )
120 assert_node(
121 extract_node(doctree, 3, 1, 0, 0, 1, 0, 1, 0, 2),
122 pending_xref,
123 refdomain='py',
124 reftype='class',
125 reftarget='int',
126 **{'py:module': 'example', 'py:class': 'Class'},
127 )
128
129 # :param items: + :type items:
130 assert_node(
131 extract_node(doctree, 3, 1, 0, 0, 1, 0, 2, 0),
132 (
133 [addnodes.literal_strong, 'items'],
134 ' (',
135 [pending_xref, addnodes.literal_emphasis, 'Tuple'],
136 [addnodes.literal_emphasis, '['],
137 [pending_xref, addnodes.literal_emphasis, 'str'],
138 [addnodes.literal_emphasis, ', '],
139 [addnodes.literal_emphasis, '...'],
140 [addnodes.literal_emphasis, ']'],
141 ')',
142 ' -- ',
143 'blah blah',
144 ),
145 )
146 assert_node(
147 extract_node(doctree, 3, 1, 0, 0, 1, 0, 2, 0, 2),
148 pending_xref,
149 refdomain='py',
150 reftype='class',
151 reftarget='Tuple',
152 **{'py:module': 'example', 'py:class': 'Class'},
153 )
154 assert_node(
155 extract_node(doctree, 3, 1, 0, 0, 1, 0, 2, 0, 4),
156 pending_xref,
157 refdomain='py',
158 reftype='class',
159 reftarget='str',
160 **{'py:module': 'example', 'py:class': 'Class'},
161 )
162
163 # :param Dict[str, str] params:
164 assert_node(
165 extract_node(doctree, 3, 1, 0, 0, 1, 0, 3, 0),
166 (
167 [addnodes.literal_strong, 'params'],
168 ' (',
169 [pending_xref, addnodes.literal_emphasis, 'Dict'],
170 [addnodes.literal_emphasis, '['],
171 [pending_xref, addnodes.literal_emphasis, 'str'],
172 [addnodes.literal_emphasis, ', '],
173 [pending_xref, addnodes.literal_emphasis, 'str'],
174 [addnodes.literal_emphasis, ']'],
175 ')',
176 ' -- ',
177 'blah blah',
178 ),
179 )
180 assert_node(
181 extract_node(doctree, 3, 1, 0, 0, 1, 0, 3, 0, 2),
182 pending_xref,
183 refdomain='py',
184 reftype='class',
185 reftarget='Dict',
186 **{'py:module': 'example', 'py:class': 'Class'},
187 )
188 assert_node(
189 extract_node(doctree, 3, 1, 0, 0, 1, 0, 3, 0, 4),
190 pending_xref,
191 refdomain='py',
192 reftype='class',
193 reftarget='str',
194 **{'py:module': 'example', 'py:class': 'Class'},
195 )
196 assert_node(
197 extract_node(doctree, 3, 1, 0, 0, 1, 0, 3, 0, 6),
198 pending_xref,
199 refdomain='py',
200 reftype='class',
201 reftarget='str',
202 **{'py:module': 'example', 'py:class': 'Class'},
203 )
204
205
206 @pytest.mark.sphinx('html', testroot='_blank')
207 def test_info_field_list_piped_type(app):
208 text = (
209 '.. py:module:: example\n'
210 '.. py:class:: Class\n'
211 '\n'
212 ' :param age: blah blah\n'
213 ' :type age: int | str\n'
214 )
215 doctree = restructuredtext.parse(app, text)
216
217 assert_node(
218 doctree,
219 (
220 addnodes.index,
221 addnodes.index,
222 nodes.target,
223 [
224 desc,
225 (
226 [
227 desc_signature,
228 (
229 [
230 desc_annotation,
231 ([desc_sig_keyword, 'class'], desc_sig_space),
232 ],
233 [desc_addname, 'example.'],
234 [desc_name, 'Class'],
235 ),
236 ],
237 [
238 desc_content,
239 nodes.field_list,
240 nodes.field,
241 (nodes.field_name, nodes.field_body),
242 ],
243 ),
244 ],
245 ),
246 )
247 assert_node(
248 extract_node(doctree, 3, 1, 0, 0, 1),
249 (
250 [
251 nodes.paragraph,
252 (
253 [addnodes.literal_strong, 'age'],
254 ' (',
255 [pending_xref, addnodes.literal_emphasis, 'int'],
256 [addnodes.literal_emphasis, ' | '],
257 [pending_xref, addnodes.literal_emphasis, 'str'],
258 ')',
259 ' -- ',
260 'blah blah',
261 ),
262 ],
263 ),
264 )
265 assert_node(
266 extract_node(doctree, 3, 1, 0, 0, 1, 0, 2),
267 pending_xref,
268 refdomain='py',
269 reftype='class',
270 reftarget='int',
271 **{'py:module': 'example', 'py:class': 'Class'},
272 )
273 assert_node(
274 extract_node(doctree, 3, 1, 0, 0, 1, 0, 4),
275 pending_xref,
276 refdomain='py',
277 reftype='class',
278 reftarget='str',
279 **{'py:module': 'example', 'py:class': 'Class'},
280 )
281
282
283 @pytest.mark.sphinx('html', testroot='_blank')
284 def test_info_field_list_Literal(app):
285 text = (
286 '.. py:module:: example\n'
287 '.. py:class:: Class\n'
288 '\n'
289 ' :param age: blah blah\n'
290 " :type age: Literal['foo', 'bar', 'baz']\n"
291 )
292 doctree = restructuredtext.parse(app, text)
293
294 assert_node(
295 doctree,
296 (
297 addnodes.index,
298 addnodes.index,
299 nodes.target,
300 [
301 desc,
302 (
303 [
304 desc_signature,
305 (
306 [
307 desc_annotation,
308 ([desc_sig_keyword, 'class'], desc_sig_space),
309 ],
310 [desc_addname, 'example.'],
311 [desc_name, 'Class'],
312 ),
313 ],
314 [
315 desc_content,
316 nodes.field_list,
317 nodes.field,
318 (nodes.field_name, nodes.field_body),
319 ],
320 ),
321 ],
322 ),
323 )
324 assert_node(
325 extract_node(doctree, 3, 1, 0, 0, 1),
326 (
327 [
328 nodes.paragraph,
329 (
330 [addnodes.literal_strong, 'age'],
331 ' (',
332 [pending_xref, addnodes.literal_emphasis, 'Literal'],
333 [addnodes.literal_emphasis, '['],
334 [addnodes.literal_emphasis, "'foo'"],
335 [addnodes.literal_emphasis, ', '],
336 [addnodes.literal_emphasis, "'bar'"],
337 [addnodes.literal_emphasis, ', '],
338 [addnodes.literal_emphasis, "'baz'"],
339 [addnodes.literal_emphasis, ']'],
340 ')',
341 ' -- ',
342 'blah blah',
343 ),
344 ],
345 ),
346 )
347 assert_node(
348 extract_node(doctree, 3, 1, 0, 0, 1, 0, 2),
349 pending_xref,
350 refdomain='py',
351 reftype='class',
352 reftarget='Literal',
353 **{'py:module': 'example', 'py:class': 'Class'},
354 )
355
356
357 @pytest.mark.sphinx('html', testroot='_blank')
358 def test_info_field_list_var(app):
359 text = '.. py:class:: Class\n\n :var int attr: blah blah\n'
360 doctree = restructuredtext.parse(app, text)
361
362 assert_node(
363 doctree,
364 (
365 addnodes.index,
366 [desc, (desc_signature, [desc_content, nodes.field_list, nodes.field])],
367 ),
368 )
369 assert_node(
370 extract_node(doctree, 1, 1, 0, 0),
371 ([nodes.field_name, 'Variables'], [nodes.field_body, nodes.paragraph]),
372 )
373
374 # :var int attr:
375 assert_node(
376 extract_node(doctree, 1, 1, 0, 0, 1, 0),
377 (
378 [addnodes.literal_strong, 'attr'],
379 ' (',
380 [pending_xref, addnodes.literal_emphasis, 'int'],
381 ')',
382 ' -- ',
383 'blah blah',
384 ),
385 )
386 assert_node(
387 extract_node(doctree, 1, 1, 0, 0, 1, 0, 2),
388 pending_xref,
389 refdomain='py',
390 reftype='class',
391 reftarget='int',
392 **{'py:class': 'Class'},
393 )
394
395
396 @pytest.mark.sphinx('html', testroot='_blank')
397 def test_info_field_list_napoleon_deliminator_of(app):
398 text = (
399 '.. py:module:: example\n'
400 '.. py:class:: Class\n'
401 '\n'
402 ' :param list_str_var: example description.\n'
403 ' :type list_str_var: list of str\n'
404 ' :param tuple_int_var: example description.\n'
405 ' :type tuple_int_var: tuple of tuple of int\n'
406 )
407 doctree = restructuredtext.parse(app, text)
408
409 # :param list of str list_str_var:
410 assert_node(
411 extract_node(doctree, 3, 1, 0, 0, 1, 0, 0, 0),
412 (
413 [addnodes.literal_strong, 'list_str_var'],
414 ' (',
415 [pending_xref, addnodes.literal_emphasis, 'list'],
416 [addnodes.literal_emphasis, ' of '],
417 [pending_xref, addnodes.literal_emphasis, 'str'],
418 ')',
419 ' -- ',
420 'example description.',
421 ),
422 )
423
424 # :param tuple of tuple of int tuple_int_var:
425 assert_node(
426 extract_node(doctree, 3, 1, 0, 0, 1, 0, 1, 0),
427 (
428 [addnodes.literal_strong, 'tuple_int_var'],
429 ' (',
430 [pending_xref, addnodes.literal_emphasis, 'tuple'],
431 [addnodes.literal_emphasis, ' of '],
432 [pending_xref, addnodes.literal_emphasis, 'tuple'],
433 [addnodes.literal_emphasis, ' of '],
434 [pending_xref, addnodes.literal_emphasis, 'int'],
435 ')',
436 ' -- ',
437 'example description.',
438 ),
439 )
440
441
442 @pytest.mark.sphinx('html', testroot='_blank')
443 def test_info_field_list_napoleon_deliminator_or(app):
444 text = (
445 '.. py:module:: example\n'
446 '.. py:class:: Class\n'
447 '\n'
448 ' :param bool_str_var: example description.\n'
449 ' :type bool_str_var: bool or str\n'
450 ' :param str_float_int_var: example description.\n'
451 ' :type str_float_int_var: str or float or int\n'
452 )
453 doctree = restructuredtext.parse(app, text)
454
455 # :param bool or str bool_str_var:
456 assert_node(
457 extract_node(doctree, 3, 1, 0, 0, 1, 0, 0, 0),
458 (
459 [addnodes.literal_strong, 'bool_str_var'],
460 ' (',
461 [pending_xref, addnodes.literal_emphasis, 'bool'],
462 [addnodes.literal_emphasis, ' or '],
463 [pending_xref, addnodes.literal_emphasis, 'str'],
464 ')',
465 ' -- ',
466 'example description.',
467 ),
468 )
469
470 # :param str or float or int str_float_int_var:
471 assert_node(
472 extract_node(doctree, 3, 1, 0, 0, 1, 0, 1, 0),
473 (
474 [addnodes.literal_strong, 'str_float_int_var'],
475 ' (',
476 [pending_xref, addnodes.literal_emphasis, 'str'],
477 [addnodes.literal_emphasis, ' or '],
478 [pending_xref, addnodes.literal_emphasis, 'float'],
479 [addnodes.literal_emphasis, ' or '],
480 [pending_xref, addnodes.literal_emphasis, 'int'],
481 ')',
482 ' -- ',
483 'example description.',
484 ),
485 )
486
487
488 @pytest.mark.sphinx('html', testroot='_blank')
489 def test_type_field(app):
490 text = (
491 '.. py:data:: var1\n'
492 ' :type: .int\n'
493 '.. py:data:: var2\n'
494 ' :type: ~builtins.int\n'
495 '.. py:data:: var3\n'
496 ' :type: typing.Optional[typing.Tuple[int, typing.Any]]\n'
497 )
498 doctree = restructuredtext.parse(app, text)
499 assert_node(
500 doctree,
501 (
502 addnodes.index,
503 [
504 desc,
505 (
506 [
507 desc_signature,
508 (
509 [desc_name, 'var1'],
510 [
511 desc_annotation,
512 (
513 [desc_sig_punctuation, ':'],
514 desc_sig_space,
515 [pending_xref, 'int'],
516 ),
517 ],
518 ),
519 ],
520 [desc_content, ()],
521 ),
522 ],
523 addnodes.index,
524 [
525 desc,
526 (
527 [
528 desc_signature,
529 (
530 [desc_name, 'var2'],
531 [
532 desc_annotation,
533 (
534 [desc_sig_punctuation, ':'],
535 desc_sig_space,
536 [pending_xref, 'int'],
537 ),
538 ],
539 ),
540 ],
541 [desc_content, ()],
542 ),
543 ],
544 addnodes.index,
545 [
546 desc,
547 (
548 [
549 desc_signature,
550 (
551 [desc_name, 'var3'],
552 [
553 desc_annotation,
554 (
555 [desc_sig_punctuation, ':'],
556 desc_sig_space,
557 [pending_xref, 'Optional'],
558 [desc_sig_punctuation, '['],
559 [pending_xref, 'Tuple'],
560 [desc_sig_punctuation, '['],
561 [pending_xref, 'int'],
562 [desc_sig_punctuation, ','],
563 desc_sig_space,
564 [pending_xref, 'Any'],
565 [desc_sig_punctuation, ']'],
566 [desc_sig_punctuation, ']'],
567 ),
568 ],
569 ),
570 ],
571 [desc_content, ()],
572 ),
573 ],
574 ),
575 )
576 assert_node(
577 extract_node(doctree, 1, 0, 1, 2),
578 pending_xref,
579 reftarget='int',
580 refspecific=True,
581 )
582 assert_node(
583 extract_node(doctree, 3, 0, 1, 2),
584 pending_xref,
585 reftarget='builtins.int',
586 refspecific=False,
587 )
588 assert_node(
589 extract_node(doctree, 5, 0, 1, 2),
590 pending_xref,
591 reftarget='typing.Optional',
592 refspecific=False,
593 )
594 assert_node(
595 extract_node(doctree, 5, 0, 1, 4),
596 pending_xref,
597 reftarget='typing.Tuple',
598 refspecific=False,
599 )
600 assert_node(
601 extract_node(doctree, 5, 0, 1, 6),
602 pending_xref,
603 reftarget='int',
604 refspecific=False,
605 )
606 assert_node(
607 extract_node(doctree, 5, 0, 1, 9),
608 pending_xref,
609 reftarget='typing.Any',
610 refspecific=False,
611 )