2014-07-12 06:50:15 -05:00
|
|
|
|
"""Tests the Python Domain"""
|
|
|
|
|
|
2023-07-23 17:22:40 -05:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2021-01-10 12:12:39 -06:00
|
|
|
|
import re
|
2019-03-07 08:35:36 -06:00
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
2022-06-23 15:48:32 -05:00
|
|
|
|
import docutils.utils
|
2017-02-25 14:14:41 -06:00
|
|
|
|
import pytest
|
2018-02-19 07:39:14 -06:00
|
|
|
|
from docutils import nodes
|
2017-01-29 10:16:10 -06:00
|
|
|
|
|
2014-07-12 06:50:15 -05:00
|
|
|
|
from sphinx import addnodes
|
2020-11-11 05:00:27 -06:00
|
|
|
|
from sphinx.addnodes import (
|
|
|
|
|
desc,
|
|
|
|
|
desc_annotation,
|
|
|
|
|
desc_content,
|
|
|
|
|
desc_name,
|
|
|
|
|
desc_parameter,
|
|
|
|
|
desc_parameterlist,
|
|
|
|
|
desc_returns,
|
2021-09-25 06:55:08 -05:00
|
|
|
|
desc_sig_keyword,
|
|
|
|
|
desc_sig_literal_number,
|
|
|
|
|
desc_sig_literal_string,
|
|
|
|
|
desc_sig_name,
|
|
|
|
|
desc_sig_operator,
|
|
|
|
|
desc_sig_punctuation,
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
desc_signature,
|
2023-07-23 17:22:40 -05:00
|
|
|
|
desc_type_parameter,
|
|
|
|
|
desc_type_parameter_list,
|
2021-09-25 06:55:08 -05:00
|
|
|
|
pending_xref,
|
|
|
|
|
)
|
2019-05-19 06:10:06 -05:00
|
|
|
|
from sphinx.domains import IndexEntry
|
2024-01-18 21:41:31 -06:00
|
|
|
|
from sphinx.domains.python import PythonDomain, PythonModuleIndex
|
|
|
|
|
from sphinx.domains.python._annotations import _parse_annotation, _pseudo_parse_arglist
|
|
|
|
|
from sphinx.domains.python._object import py_sig_re
|
2019-03-23 03:00:54 -05:00
|
|
|
|
from sphinx.testing import restructuredtext
|
2017-05-07 02:46:44 -05:00
|
|
|
|
from sphinx.testing.util import assert_node
|
2023-05-11 08:28:57 -05:00
|
|
|
|
from sphinx.writers.text import STDINDENT
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
2014-07-12 06:50:15 -05:00
|
|
|
|
|
|
|
|
|
def parse(sig):
|
|
|
|
|
m = py_sig_re.match(sig)
|
|
|
|
|
if m is None:
|
|
|
|
|
raise ValueError
|
2023-07-23 17:22:40 -05:00
|
|
|
|
name_prefix, tp_list, name, arglist, retann = m.groups()
|
2014-07-12 06:50:15 -05:00
|
|
|
|
signode = addnodes.desc_signature(sig, '')
|
|
|
|
|
_pseudo_parse_arglist(signode, arglist)
|
|
|
|
|
return signode.astext()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_function_signatures():
|
|
|
|
|
rv = parse('func(a=1) -> int object')
|
2020-05-14 12:41:17 -05:00
|
|
|
|
assert rv == '(a=1)'
|
2014-07-12 06:50:15 -05:00
|
|
|
|
|
|
|
|
|
rv = parse('func(a=1, [b=None])')
|
2020-05-14 12:41:17 -05:00
|
|
|
|
assert rv == '(a=1, [b=None])'
|
2014-07-12 06:50:15 -05:00
|
|
|
|
|
|
|
|
|
rv = parse('func(a=1[, b=None])')
|
2020-05-14 12:41:17 -05:00
|
|
|
|
assert rv == '(a=1, [b=None])'
|
2014-07-12 06:50:15 -05:00
|
|
|
|
|
|
|
|
|
rv = parse("compile(source : string, filename, symbol='file')")
|
2020-05-14 12:41:17 -05:00
|
|
|
|
assert rv == "(source : string, filename, symbol='file')"
|
2014-07-12 06:50:15 -05:00
|
|
|
|
|
|
|
|
|
rv = parse('func(a=[], [b=None])')
|
2020-05-14 12:41:17 -05:00
|
|
|
|
assert rv == '(a=[], [b=None])'
|
2014-07-12 06:50:15 -05:00
|
|
|
|
|
|
|
|
|
rv = parse('func(a=[][, b=None])')
|
2020-05-14 12:41:17 -05:00
|
|
|
|
assert rv == '(a=[], [b=None])'
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('dummy', testroot='domain-py')
|
2024-07-23 09:35:55 -05:00
|
|
|
|
def test_domain_py_xrefs(app):
|
2017-02-25 14:14:41 -06:00
|
|
|
|
"""Domain objects have correct prefixes when looking up xrefs"""
|
2024-01-16 20:38:46 -06:00
|
|
|
|
app.build(force_all=True)
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
def assert_refnode(
|
|
|
|
|
node, module_name, class_name, target, reftype=None, domain='py'
|
|
|
|
|
):
|
2017-02-25 14:14:41 -06:00
|
|
|
|
attributes = {
|
|
|
|
|
'refdomain': domain,
|
|
|
|
|
'reftarget': target,
|
|
|
|
|
}
|
|
|
|
|
if reftype is not None:
|
|
|
|
|
attributes['reftype'] = reftype
|
|
|
|
|
if module_name is not False:
|
|
|
|
|
attributes['py:module'] = module_name
|
|
|
|
|
if class_name is not False:
|
|
|
|
|
attributes['py:class'] = class_name
|
|
|
|
|
assert_node(node, **attributes)
|
|
|
|
|
|
|
|
|
|
doctree = app.env.get_doctree('roles')
|
2022-01-02 10:05:46 -06:00
|
|
|
|
refnodes = list(doctree.findall(pending_xref))
|
2018-12-15 08:02:28 -06:00
|
|
|
|
assert_refnode(refnodes[0], None, None, 'TopLevel', 'class')
|
|
|
|
|
assert_refnode(refnodes[1], None, None, 'top_level', 'meth')
|
2024-07-11 06:15:54 -05:00
|
|
|
|
assert_refnode(refnodes[2], None, None, 'TopLevelType', 'type')
|
|
|
|
|
assert_refnode(refnodes[3], None, 'NestedParentA', 'child_1', 'meth')
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_refnode(
|
|
|
|
|
refnodes[4], None, 'NestedParentA', 'NestedChildA.subchild_2', 'meth'
|
|
|
|
|
)
|
2024-07-11 06:15:54 -05:00
|
|
|
|
assert_refnode(refnodes[5], None, 'NestedParentA', 'child_2', 'meth')
|
|
|
|
|
assert_refnode(refnodes[6], False, 'NestedParentA', 'any_child', domain='')
|
|
|
|
|
assert_refnode(refnodes[7], None, 'NestedParentA', 'NestedChildA', 'class')
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_refnode(
|
|
|
|
|
refnodes[8], None, 'NestedParentA.NestedChildA', 'subchild_2', 'meth'
|
|
|
|
|
)
|
|
|
|
|
assert_refnode(
|
|
|
|
|
refnodes[9], None, 'NestedParentA.NestedChildA', 'NestedParentA.child_1', 'meth'
|
|
|
|
|
)
|
|
|
|
|
assert_refnode(
|
|
|
|
|
refnodes[10], None, 'NestedParentA', 'NestedChildA.subchild_1', 'meth'
|
|
|
|
|
)
|
2024-07-11 06:15:54 -05:00
|
|
|
|
assert_refnode(refnodes[11], None, 'NestedParentB', 'child_1', 'meth')
|
|
|
|
|
assert_refnode(refnodes[12], None, 'NestedParentB', 'NestedParentB', 'class')
|
|
|
|
|
assert_refnode(refnodes[13], None, None, 'NestedParentA.NestedChildA', 'class')
|
|
|
|
|
assert_refnode(refnodes[14], None, None, 'NestedParentA.NestedTypeA', 'type')
|
|
|
|
|
assert len(refnodes) == 15
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
|
|
|
|
doctree = app.env.get_doctree('module')
|
2022-01-02 10:05:46 -06:00
|
|
|
|
refnodes = list(doctree.findall(pending_xref))
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_refnode(refnodes[0], 'module_a.submodule', None, 'ModTopLevel', 'class')
|
|
|
|
|
assert_refnode(
|
|
|
|
|
refnodes[1], 'module_a.submodule', 'ModTopLevel', 'mod_child_1', 'meth'
|
|
|
|
|
)
|
|
|
|
|
assert_refnode(
|
|
|
|
|
refnodes[2],
|
|
|
|
|
'module_a.submodule',
|
|
|
|
|
'ModTopLevel',
|
|
|
|
|
'ModTopLevel.mod_child_1',
|
|
|
|
|
'meth',
|
|
|
|
|
)
|
|
|
|
|
assert_refnode(
|
|
|
|
|
refnodes[3], 'module_a.submodule', 'ModTopLevel', 'mod_child_2', 'meth'
|
|
|
|
|
)
|
|
|
|
|
assert_refnode(
|
|
|
|
|
refnodes[4],
|
|
|
|
|
'module_a.submodule',
|
|
|
|
|
'ModTopLevel',
|
|
|
|
|
'module_a.submodule.ModTopLevel.mod_child_1',
|
|
|
|
|
'meth',
|
|
|
|
|
)
|
|
|
|
|
assert_refnode(refnodes[5], 'module_a.submodule', 'ModTopLevel', 'prop', 'attr')
|
|
|
|
|
assert_refnode(refnodes[6], 'module_a.submodule', 'ModTopLevel', 'prop', 'meth')
|
|
|
|
|
assert_refnode(refnodes[7], 'module_b.submodule', None, 'ModTopLevel', 'class')
|
|
|
|
|
assert_refnode(
|
|
|
|
|
refnodes[8], 'module_b.submodule', 'ModTopLevel', 'ModNoModule', 'class'
|
|
|
|
|
)
|
2020-01-24 22:55:27 -06:00
|
|
|
|
assert_refnode(refnodes[9], False, False, 'int', 'class')
|
|
|
|
|
assert_refnode(refnodes[10], False, False, 'tuple', 'class')
|
|
|
|
|
assert_refnode(refnodes[11], False, False, 'str', 'class')
|
|
|
|
|
assert_refnode(refnodes[12], False, False, 'float', 'class')
|
|
|
|
|
assert_refnode(refnodes[13], False, False, 'list', 'class')
|
|
|
|
|
assert_refnode(refnodes[14], False, False, 'ModTopLevel', 'class')
|
|
|
|
|
assert_refnode(refnodes[15], False, False, 'index', 'doc', domain='std')
|
2024-01-21 08:31:04 -06:00
|
|
|
|
assert_refnode(refnodes[16], False, False, 'typing.Literal', 'obj', domain='py')
|
|
|
|
|
assert_refnode(refnodes[17], False, False, 'typing.Literal', 'obj', domain='py')
|
2024-07-11 06:15:54 -05:00
|
|
|
|
assert_refnode(refnodes[18], False, False, 'list', 'class', domain='py')
|
|
|
|
|
assert_refnode(refnodes[19], False, False, 'int', 'class', domain='py')
|
|
|
|
|
assert_refnode(refnodes[20], False, False, 'str', 'class', domain='py')
|
|
|
|
|
assert len(refnodes) == 21
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
2017-10-21 07:21:37 -05:00
|
|
|
|
doctree = app.env.get_doctree('module_option')
|
2022-01-02 10:05:46 -06:00
|
|
|
|
refnodes = list(doctree.findall(pending_xref))
|
2017-10-21 07:21:37 -05:00
|
|
|
|
print(refnodes)
|
|
|
|
|
print(refnodes[0])
|
|
|
|
|
print(refnodes[1])
|
|
|
|
|
assert_refnode(refnodes[0], 'test.extra', 'B', 'foo', 'meth')
|
|
|
|
|
assert_refnode(refnodes[1], 'test.extra', 'B', 'foo', 'meth')
|
|
|
|
|
assert len(refnodes) == 2
|
|
|
|
|
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
2021-01-10 12:12:39 -06:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='domain-py')
|
2024-07-23 09:35:55 -05:00
|
|
|
|
def test_domain_py_xrefs_abbreviations(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
|
app.build(force_all=True)
|
2021-01-10 12:12:39 -06:00
|
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
|
content = (app.outdir / 'abbr.html').read_text(encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert re.search(
|
|
|
|
|
r'normal: <a .* href="module.html#module_a.submodule.ModTopLevel.'
|
|
|
|
|
r'mod_child_1" .*><.*>module_a.submodule.ModTopLevel.mod_child_1\(\)'
|
|
|
|
|
r'<.*></a>',
|
|
|
|
|
content,
|
|
|
|
|
)
|
|
|
|
|
assert re.search(
|
|
|
|
|
r'relative: <a .* href="module.html#module_a.submodule.ModTopLevel.'
|
|
|
|
|
r'mod_child_1" .*><.*>ModTopLevel.mod_child_1\(\)<.*></a>',
|
|
|
|
|
content,
|
|
|
|
|
)
|
|
|
|
|
assert re.search(
|
|
|
|
|
r'short name: <a .* href="module.html#module_a.submodule.ModTopLevel.'
|
|
|
|
|
r'mod_child_1" .*><.*>mod_child_1\(\)<.*></a>',
|
|
|
|
|
content,
|
|
|
|
|
)
|
|
|
|
|
assert re.search(
|
|
|
|
|
r'relative \+ short name: <a .* href="module.html#module_a.submodule.'
|
|
|
|
|
r'ModTopLevel.mod_child_1" .*><.*>mod_child_1\(\)<.*></a>',
|
|
|
|
|
content,
|
|
|
|
|
)
|
|
|
|
|
assert re.search(
|
|
|
|
|
r'short name \+ relative: <a .* href="module.html#module_a.submodule.'
|
|
|
|
|
r'ModTopLevel.mod_child_1" .*><.*>mod_child_1\(\)<.*></a>',
|
|
|
|
|
content,
|
|
|
|
|
)
|
2021-01-10 12:12:39 -06:00
|
|
|
|
|
|
|
|
|
|
2017-02-25 14:14:41 -06:00
|
|
|
|
@pytest.mark.sphinx('dummy', testroot='domain-py')
|
2024-07-23 09:35:55 -05:00
|
|
|
|
def test_domain_py_objects(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
|
app.build(force_all=True)
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
2024-09-17 21:50:27 -05:00
|
|
|
|
modules = app.env.domains.python_domain.data['modules']
|
|
|
|
|
objects = app.env.domains.python_domain.data['objects']
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
|
|
|
|
assert 'module_a.submodule' in modules
|
|
|
|
|
assert 'module_a.submodule' in objects
|
|
|
|
|
assert 'module_b.submodule' in modules
|
|
|
|
|
assert 'module_b.submodule' in objects
|
|
|
|
|
|
2020-03-01 09:45:50 -06:00
|
|
|
|
assert objects['module_a.submodule.ModTopLevel'][2] == 'class'
|
|
|
|
|
assert objects['module_a.submodule.ModTopLevel.mod_child_1'][2] == 'method'
|
|
|
|
|
assert objects['module_a.submodule.ModTopLevel.mod_child_2'][2] == 'method'
|
2017-02-25 22:07:16 -06:00
|
|
|
|
assert 'ModTopLevel.ModNoModule' not in objects
|
2020-03-01 09:45:50 -06:00
|
|
|
|
assert objects['ModNoModule'][2] == 'class'
|
|
|
|
|
assert objects['module_b.submodule.ModTopLevel'][2] == 'class'
|
|
|
|
|
|
|
|
|
|
assert objects['TopLevel'][2] == 'class'
|
|
|
|
|
assert objects['top_level'][2] == 'method'
|
2024-07-11 06:15:54 -05:00
|
|
|
|
assert objects['TopLevelType'][2] == 'type'
|
2020-03-01 09:45:50 -06:00
|
|
|
|
assert objects['NestedParentA'][2] == 'class'
|
2024-07-11 06:15:54 -05:00
|
|
|
|
assert objects['NestedParentA.NestedTypeA'][2] == 'type'
|
2020-03-01 09:45:50 -06:00
|
|
|
|
assert objects['NestedParentA.child_1'][2] == 'method'
|
|
|
|
|
assert objects['NestedParentA.any_child'][2] == 'method'
|
|
|
|
|
assert objects['NestedParentA.NestedChildA'][2] == 'class'
|
|
|
|
|
assert objects['NestedParentA.NestedChildA.subchild_1'][2] == 'method'
|
|
|
|
|
assert objects['NestedParentA.NestedChildA.subchild_2'][2] == 'method'
|
|
|
|
|
assert objects['NestedParentA.child_2'][2] == 'method'
|
|
|
|
|
assert objects['NestedParentB'][2] == 'class'
|
|
|
|
|
assert objects['NestedParentB.child_1'][2] == 'method'
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
|
|
|
|
|
2020-01-24 22:55:27 -06:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='domain-py')
|
2024-07-23 09:35:55 -05:00
|
|
|
|
def test_resolve_xref_for_properties(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
|
app.build(force_all=True)
|
2020-01-24 22:55:27 -06:00
|
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
|
content = (app.outdir / 'module.html').read_text(encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert (
|
|
|
|
|
'Link to <a class="reference internal" href="#module_a.submodule.ModTopLevel.prop"'
|
|
|
|
|
' title="module_a.submodule.ModTopLevel.prop">'
|
|
|
|
|
'<code class="xref py py-attr docutils literal notranslate"><span class="pre">'
|
|
|
|
|
'prop</span> <span class="pre">attribute</span></code></a>'
|
|
|
|
|
) in content
|
|
|
|
|
assert (
|
|
|
|
|
'Link to <a class="reference internal" href="#module_a.submodule.ModTopLevel.prop"'
|
|
|
|
|
' title="module_a.submodule.ModTopLevel.prop">'
|
|
|
|
|
'<code class="xref py py-meth docutils literal notranslate"><span class="pre">'
|
|
|
|
|
'prop</span> <span class="pre">method</span></code></a>'
|
|
|
|
|
) in content
|
|
|
|
|
assert (
|
|
|
|
|
'Link to <a class="reference internal" href="#module_a.submodule.ModTopLevel.prop"'
|
|
|
|
|
' title="module_a.submodule.ModTopLevel.prop">'
|
|
|
|
|
'<code class="xref py py-attr docutils literal notranslate"><span class="pre">'
|
|
|
|
|
'prop</span> <span class="pre">attribute</span></code></a>'
|
|
|
|
|
) in content
|
2020-01-24 22:55:27 -06:00
|
|
|
|
|
|
|
|
|
|
2017-02-25 14:14:41 -06:00
|
|
|
|
@pytest.mark.sphinx('dummy', testroot='domain-py')
|
2024-07-23 09:35:55 -05:00
|
|
|
|
def test_domain_py_find_obj(app):
|
2017-02-25 14:14:41 -06:00
|
|
|
|
def find_obj(modname, prefix, obj_name, obj_type, searchmode=0):
|
2024-09-17 21:50:27 -05:00
|
|
|
|
return app.env.domains.python_domain.find_obj(
|
2024-08-11 08:58:56 -05:00
|
|
|
|
app.env, modname, prefix, obj_name, obj_type, searchmode
|
|
|
|
|
)
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
2024-01-16 20:38:46 -06:00
|
|
|
|
app.build(force_all=True)
|
2017-02-25 14:14:41 -06:00
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert find_obj(None, None, 'NONEXISTANT', 'class') == []
|
|
|
|
|
assert find_obj(None, None, 'NestedParentA', 'class') == [
|
|
|
|
|
(
|
|
|
|
|
'NestedParentA',
|
|
|
|
|
('roles', 'NestedParentA', 'class', False),
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
assert find_obj(None, None, 'NestedParentA.NestedTypeA', 'type') == [
|
|
|
|
|
(
|
|
|
|
|
'NestedParentA.NestedTypeA',
|
|
|
|
|
('roles', 'NestedParentA.NestedTypeA', 'type', False),
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
assert find_obj(None, None, 'NestedParentA.NestedChildA', 'class') == [
|
|
|
|
|
(
|
|
|
|
|
'NestedParentA.NestedChildA',
|
|
|
|
|
('roles', 'NestedParentA.NestedChildA', 'class', False),
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
assert find_obj(None, 'NestedParentA', 'NestedChildA', 'class') == [
|
|
|
|
|
(
|
|
|
|
|
'NestedParentA.NestedChildA',
|
|
|
|
|
('roles', 'NestedParentA.NestedChildA', 'class', False),
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
assert find_obj(None, None, 'NestedParentA.NestedChildA.subchild_1', 'meth') == [
|
|
|
|
|
(
|
|
|
|
|
'NestedParentA.NestedChildA.subchild_1',
|
|
|
|
|
('roles', 'NestedParentA.NestedChildA.subchild_1', 'method', False),
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
assert find_obj(None, 'NestedParentA', 'NestedChildA.subchild_1', 'meth') == [
|
|
|
|
|
(
|
|
|
|
|
'NestedParentA.NestedChildA.subchild_1',
|
|
|
|
|
('roles', 'NestedParentA.NestedChildA.subchild_1', 'method', False),
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
assert find_obj(None, 'NestedParentA.NestedChildA', 'subchild_1', 'meth') == [
|
|
|
|
|
(
|
|
|
|
|
'NestedParentA.NestedChildA.subchild_1',
|
|
|
|
|
('roles', 'NestedParentA.NestedChildA.subchild_1', 'method', False),
|
|
|
|
|
)
|
|
|
|
|
]
|
2017-01-29 10:16:10 -06:00
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2017-01-29 10:16:10 -06:00
|
|
|
|
def test_get_full_qualified_name():
|
|
|
|
|
env = Mock(domaindata={})
|
|
|
|
|
domain = PythonDomain(env)
|
|
|
|
|
|
|
|
|
|
# non-python references
|
|
|
|
|
node = nodes.reference()
|
|
|
|
|
assert domain.get_full_qualified_name(node) is None
|
|
|
|
|
|
|
|
|
|
# simple reference
|
|
|
|
|
node = nodes.reference(reftarget='func')
|
|
|
|
|
assert domain.get_full_qualified_name(node) == 'func'
|
|
|
|
|
|
|
|
|
|
# with py:module context
|
|
|
|
|
kwargs = {'py:module': 'module1'}
|
|
|
|
|
node = nodes.reference(reftarget='func', **kwargs)
|
|
|
|
|
assert domain.get_full_qualified_name(node) == 'module1.func'
|
|
|
|
|
|
|
|
|
|
# with py:class context
|
|
|
|
|
kwargs = {'py:class': 'Class'}
|
|
|
|
|
node = nodes.reference(reftarget='func', **kwargs)
|
|
|
|
|
assert domain.get_full_qualified_name(node) == 'Class.func'
|
|
|
|
|
|
|
|
|
|
# with both py:module and py:class context
|
|
|
|
|
kwargs = {'py:module': 'module1', 'py:class': 'Class'}
|
|
|
|
|
node = nodes.reference(reftarget='func', **kwargs)
|
|
|
|
|
assert domain.get_full_qualified_name(node) == 'module1.Class.func'
|
2019-03-23 03:00:54 -05:00
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2020-07-08 11:49:34 -05:00
|
|
|
|
def test_parse_annotation(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
doctree = _parse_annotation('int', app.env)
|
|
|
|
|
assert_node(doctree, ([pending_xref, 'int'],))
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree[0], pending_xref, refdomain='py', reftype='class', reftarget='int'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
doctree = _parse_annotation('List[int]', app.env)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'List'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
doctree = _parse_annotation('Tuple[int, int]', app.env)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Tuple'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
doctree = _parse_annotation('Tuple[()]', app.env)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Tuple'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[desc_sig_punctuation, '('],
|
|
|
|
|
[desc_sig_punctuation, ')'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
doctree = _parse_annotation('Tuple[int, ...]', app.env)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Tuple'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_punctuation, '...'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
doctree = _parse_annotation('Callable[[int, int], int]', app.env)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Callable'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
doctree = _parse_annotation('Callable[[], int]', app.env)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Callable'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
doctree = _parse_annotation('List[None]', app.env)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'List'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'None'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
2020-07-29 11:41:18 -05:00
|
|
|
|
|
2020-04-09 12:07:02 -05:00
|
|
|
|
# None type makes an object-reference (not a class reference)
|
2024-08-11 08:58:56 -05:00
|
|
|
|
doctree = _parse_annotation('None', app.env)
|
|
|
|
|
assert_node(doctree, ([pending_xref, 'None'],))
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree[0], pending_xref, refdomain='py', reftype='obj', reftarget='None'
|
|
|
|
|
)
|
2020-04-09 12:07:02 -05:00
|
|
|
|
|
2021-12-19 10:53:20 -06:00
|
|
|
|
# Literal type makes an object-reference (not a class reference)
|
2021-12-19 10:52:50 -06:00
|
|
|
|
doctree = _parse_annotation("typing.Literal['a', 'b']", app.env)
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Literal'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[desc_sig_literal_string, "'a'"],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_literal_string, "'b'"],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree[0],
|
|
|
|
|
pending_xref,
|
|
|
|
|
refdomain='py',
|
|
|
|
|
reftype='obj',
|
|
|
|
|
reftarget='typing.Literal',
|
|
|
|
|
)
|
2021-12-19 10:52:50 -06:00
|
|
|
|
|
2024-07-20 05:16:33 -05:00
|
|
|
|
# Annotated type with callable gets parsed
|
2024-08-11 08:58:56 -05:00
|
|
|
|
doctree = _parse_annotation(
|
|
|
|
|
'Annotated[Optional[str], annotated_types.MaxLen(max_length=10)]', app.env
|
|
|
|
|
)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Annotated'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'str'],
|
|
|
|
|
[desc_sig_space, ' '],
|
|
|
|
|
[desc_sig_punctuation, '|'],
|
|
|
|
|
[desc_sig_space, ' '],
|
|
|
|
|
[pending_xref, 'None'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
[desc_sig_space, ' '],
|
|
|
|
|
[pending_xref, 'annotated_types.MaxLen'],
|
|
|
|
|
[desc_sig_punctuation, '('],
|
|
|
|
|
[desc_sig_name, 'max_length'],
|
|
|
|
|
[desc_sig_operator, '='],
|
|
|
|
|
[desc_sig_literal_number, '10'],
|
|
|
|
|
[desc_sig_punctuation, ')'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
2024-07-20 05:16:33 -05:00
|
|
|
|
|
2020-04-09 12:07:02 -05:00
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2021-11-29 10:54:19 -06:00
|
|
|
|
def test_parse_annotation_suppress(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
doctree = _parse_annotation('~typing.Dict[str, str]', app.env)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Dict'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'str'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[pending_xref, 'str'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree[0], pending_xref, refdomain='py', reftype='obj', reftarget='typing.Dict'
|
|
|
|
|
)
|
2021-11-29 10:54:19 -06:00
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2021-09-01 12:25:31 -05:00
|
|
|
|
def test_parse_annotation_Literal(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
doctree = _parse_annotation('Literal[True, False]', app.env)
|
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Literal'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[desc_sig_keyword, 'True'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_keyword, 'False'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
2021-09-01 12:25:31 -05:00
|
|
|
|
|
|
|
|
|
doctree = _parse_annotation("typing.Literal[0, 1, 'abc']", app.env)
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Literal'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[desc_sig_literal_number, '0'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_literal_number, '1'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_literal_string, "'abc'"],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
)
|
2021-09-01 12:25:31 -05:00
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root', freshenv=True)
|
2019-05-19 06:10:06 -05:00
|
|
|
|
def test_module_index(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
text = (
|
|
|
|
|
'.. py:module:: docutils\n'
|
|
|
|
|
'.. py:module:: sphinx\n'
|
|
|
|
|
'.. py:module:: sphinx.config\n'
|
|
|
|
|
'.. py:module:: sphinx.builders\n'
|
|
|
|
|
'.. py:module:: sphinx.builders.html\n'
|
|
|
|
|
'.. py:module:: sphinx_intl\n'
|
|
|
|
|
)
|
2019-05-19 06:10:06 -05:00
|
|
|
|
restructuredtext.parse(app, text)
|
2024-09-17 21:50:27 -05:00
|
|
|
|
index = PythonModuleIndex(app.env.domains.python_domain)
|
2019-05-19 06:10:06 -05:00
|
|
|
|
assert index.generate() == (
|
2024-08-11 08:58:56 -05:00
|
|
|
|
[
|
|
|
|
|
('d', [IndexEntry('docutils', 0, 'index', 'module-docutils', '', '', '')]),
|
|
|
|
|
(
|
|
|
|
|
's',
|
|
|
|
|
[
|
|
|
|
|
IndexEntry('sphinx', 1, 'index', 'module-sphinx', '', '', ''),
|
|
|
|
|
IndexEntry(
|
|
|
|
|
'sphinx.builders',
|
|
|
|
|
2,
|
|
|
|
|
'index',
|
|
|
|
|
'module-sphinx.builders',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
),
|
|
|
|
|
IndexEntry(
|
|
|
|
|
'sphinx.builders.html',
|
|
|
|
|
2,
|
|
|
|
|
'index',
|
|
|
|
|
'module-sphinx.builders.html',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
),
|
|
|
|
|
IndexEntry(
|
|
|
|
|
'sphinx.config', 2, 'index', 'module-sphinx.config', '', '', ''
|
|
|
|
|
),
|
|
|
|
|
IndexEntry(
|
|
|
|
|
'sphinx_intl', 0, 'index', 'module-sphinx_intl', '', '', ''
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
2023-02-17 16:11:14 -06:00
|
|
|
|
False,
|
2019-05-19 06:10:06 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root', freshenv=True)
|
2019-05-19 06:10:06 -05:00
|
|
|
|
def test_module_index_submodule(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
text = '.. py:module:: sphinx.config\n'
|
2019-05-19 06:10:06 -05:00
|
|
|
|
restructuredtext.parse(app, text)
|
2024-09-17 21:50:27 -05:00
|
|
|
|
index = PythonModuleIndex(app.env.domains.python_domain)
|
2019-05-19 06:10:06 -05:00
|
|
|
|
assert index.generate() == (
|
2024-08-11 08:58:56 -05:00
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
's',
|
|
|
|
|
[
|
|
|
|
|
IndexEntry('sphinx', 1, '', '', '', '', ''),
|
|
|
|
|
IndexEntry(
|
|
|
|
|
'sphinx.config', 2, 'index', 'module-sphinx.config', '', '', ''
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
],
|
2023-02-17 16:11:14 -06:00
|
|
|
|
False,
|
2019-05-19 06:10:06 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root', freshenv=True)
|
2019-05-19 06:10:06 -05:00
|
|
|
|
def test_module_index_not_collapsed(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
text = '.. py:module:: docutils\n.. py:module:: sphinx\n'
|
2019-05-19 06:10:06 -05:00
|
|
|
|
restructuredtext.parse(app, text)
|
2024-09-17 21:50:27 -05:00
|
|
|
|
index = PythonModuleIndex(app.env.domains.python_domain)
|
2019-05-19 06:10:06 -05:00
|
|
|
|
assert index.generate() == (
|
2024-08-11 08:58:56 -05:00
|
|
|
|
[
|
|
|
|
|
('d', [IndexEntry('docutils', 0, 'index', 'module-docutils', '', '', '')]),
|
|
|
|
|
('s', [IndexEntry('sphinx', 0, 'index', 'module-sphinx', '', '', '')]),
|
|
|
|
|
],
|
2023-02-17 16:11:14 -06:00
|
|
|
|
True,
|
2019-05-19 06:10:06 -05:00
|
|
|
|
)
|
2020-02-05 06:13:14 -06:00
|
|
|
|
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
@pytest.mark.sphinx(
|
2024-08-12 16:34:03 -05:00
|
|
|
|
'html',
|
|
|
|
|
testroot='root',
|
2024-08-11 08:58:56 -05:00
|
|
|
|
freshenv=True,
|
|
|
|
|
confoverrides={'modindex_common_prefix': ['sphinx.']},
|
|
|
|
|
)
|
2020-02-05 06:13:14 -06:00
|
|
|
|
def test_modindex_common_prefix(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
text = (
|
|
|
|
|
'.. py:module:: docutils\n'
|
|
|
|
|
'.. py:module:: sphinx\n'
|
|
|
|
|
'.. py:module:: sphinx.config\n'
|
|
|
|
|
'.. py:module:: sphinx.builders\n'
|
|
|
|
|
'.. py:module:: sphinx.builders.html\n'
|
|
|
|
|
'.. py:module:: sphinx_intl\n'
|
|
|
|
|
)
|
2020-02-05 06:13:14 -06:00
|
|
|
|
restructuredtext.parse(app, text)
|
2024-09-17 21:50:27 -05:00
|
|
|
|
index = PythonModuleIndex(app.env.domains.python_domain)
|
2020-02-05 06:13:14 -06:00
|
|
|
|
assert index.generate() == (
|
2024-08-11 08:58:56 -05:00
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
'b',
|
|
|
|
|
[
|
|
|
|
|
IndexEntry(
|
|
|
|
|
'sphinx.builders',
|
|
|
|
|
1,
|
|
|
|
|
'index',
|
|
|
|
|
'module-sphinx.builders',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
),
|
|
|
|
|
IndexEntry(
|
|
|
|
|
'sphinx.builders.html',
|
|
|
|
|
2,
|
|
|
|
|
'index',
|
|
|
|
|
'module-sphinx.builders.html',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
'c',
|
|
|
|
|
[
|
|
|
|
|
IndexEntry(
|
|
|
|
|
'sphinx.config', 0, 'index', 'module-sphinx.config', '', '', ''
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
('d', [IndexEntry('docutils', 0, 'index', 'module-docutils', '', '', '')]),
|
|
|
|
|
(
|
|
|
|
|
's',
|
|
|
|
|
[
|
|
|
|
|
IndexEntry('sphinx', 0, 'index', 'module-sphinx', '', '', ''),
|
|
|
|
|
IndexEntry(
|
|
|
|
|
'sphinx_intl', 0, 'index', 'module-sphinx_intl', '', '', ''
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
2023-02-17 16:11:14 -06:00
|
|
|
|
True,
|
2020-02-05 06:13:14 -06:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-07-28 16:30:26 -05:00
|
|
|
|
def test_no_index_entry(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
text = '.. py:function:: f()\n.. py:function:: g()\n :no-index-entry:\n'
|
2020-07-04 13:16:04 -05:00
|
|
|
|
doctree = restructuredtext.parse(app, text)
|
|
|
|
|
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc))
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_node(
|
|
|
|
|
doctree[0],
|
|
|
|
|
addnodes.index,
|
|
|
|
|
entries=[('pair', 'built-in function; f()', 'f', '', None)],
|
|
|
|
|
)
|
2020-07-04 13:16:04 -05:00
|
|
|
|
assert_node(doctree[2], addnodes.index, entries=[])
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
text = '.. py:class:: f\n.. py:class:: g\n :no-index-entry:\n'
|
2020-07-04 13:16:04 -05:00
|
|
|
|
doctree = restructuredtext.parse(app, text)
|
|
|
|
|
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc))
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_node(
|
|
|
|
|
doctree[0],
|
|
|
|
|
addnodes.index,
|
|
|
|
|
entries=[('single', 'f (built-in class)', 'f', '', None)],
|
|
|
|
|
)
|
2020-07-04 13:16:04 -05:00
|
|
|
|
assert_node(doctree[2], addnodes.index, entries=[])
|
2020-08-08 06:06:46 -05:00
|
|
|
|
|
|
|
|
|
|
2020-11-15 08:15:01 -06:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='domain-py-python_use_unqualified_type_names')
|
2024-07-23 09:35:55 -05:00
|
|
|
|
def test_python_python_use_unqualified_type_names(app):
|
2020-11-15 08:15:01 -06:00
|
|
|
|
app.build()
|
2022-04-26 21:04:19 -05:00
|
|
|
|
content = (app.outdir / 'index.html').read_text(encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert (
|
|
|
|
|
'<span class="n"><a class="reference internal" href="#foo.Name" title="foo.Name">'
|
|
|
|
|
'<span class="pre">Name</span></a></span>'
|
|
|
|
|
) in content
|
2020-11-15 08:15:01 -06:00
|
|
|
|
assert '<span class="n"><span class="pre">foo.Age</span></span>' in content
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert (
|
|
|
|
|
'<p><strong>name</strong> (<a class="reference internal" href="#foo.Name" '
|
|
|
|
|
'title="foo.Name"><em>Name</em></a>) – blah blah</p>'
|
|
|
|
|
) in content
|
2021-06-02 09:30:29 -05:00
|
|
|
|
assert '<p><strong>age</strong> (<em>foo.Age</em>) – blah blah</p>' in content
|
2020-11-15 08:15:01 -06:00
|
|
|
|
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
@pytest.mark.sphinx(
|
|
|
|
|
'html',
|
|
|
|
|
testroot='domain-py-python_use_unqualified_type_names',
|
|
|
|
|
confoverrides={'python_use_unqualified_type_names': False},
|
|
|
|
|
)
|
2024-07-23 09:35:55 -05:00
|
|
|
|
def test_python_python_use_unqualified_type_names_disabled(app):
|
2020-11-15 08:15:01 -06:00
|
|
|
|
app.build()
|
2022-04-26 21:04:19 -05:00
|
|
|
|
content = (app.outdir / 'index.html').read_text(encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert (
|
|
|
|
|
'<span class="n"><a class="reference internal" href="#foo.Name" title="foo.Name">'
|
|
|
|
|
'<span class="pre">foo.Name</span></a></span>'
|
|
|
|
|
) in content
|
2020-11-15 08:15:01 -06:00
|
|
|
|
assert '<span class="n"><span class="pre">foo.Age</span></span>' in content
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert (
|
|
|
|
|
'<p><strong>name</strong> (<a class="reference internal" href="#foo.Name" '
|
|
|
|
|
'title="foo.Name"><em>foo.Name</em></a>) – blah blah</p>'
|
|
|
|
|
) in content
|
2021-06-02 09:30:29 -05:00
|
|
|
|
assert '<p><strong>age</strong> (<em>foo.Age</em>) – blah blah</p>' in content
|
2020-11-15 08:15:01 -06:00
|
|
|
|
|
|
|
|
|
|
2020-08-08 06:06:46 -05:00
|
|
|
|
@pytest.mark.sphinx('dummy', testroot='domain-py-xref-warning')
|
2024-07-23 09:35:55 -05:00
|
|
|
|
def test_warn_missing_reference(app):
|
2020-08-08 06:06:46 -05:00
|
|
|
|
app.build()
|
2024-07-23 09:35:55 -05:00
|
|
|
|
assert "index.rst:6: WARNING: undefined label: 'no-label'" in app.warning.getvalue()
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert (
|
|
|
|
|
'index.rst:6: WARNING: Failed to create a cross reference. '
|
|
|
|
|
"A title or caption not found: 'existing-label'"
|
|
|
|
|
) in app.warning.getvalue()
|
2022-06-23 15:48:32 -05:00
|
|
|
|
|
|
|
|
|
|
2023-07-28 01:03:53 -05:00
|
|
|
|
@pytest.mark.parametrize('include_options', [True, False])
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root', confoverrides={'nitpicky': True})
|
2022-06-23 15:48:32 -05:00
|
|
|
|
def test_signature_line_number(app, include_options):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
text = '.. py:function:: foo(bar : string)\n' + (
|
|
|
|
|
' :no-index-entry:\n' if include_options else ''
|
|
|
|
|
)
|
2022-06-23 15:48:32 -05:00
|
|
|
|
doc = restructuredtext.parse(app, text)
|
|
|
|
|
xrefs = list(doc.findall(condition=addnodes.pending_xref))
|
|
|
|
|
assert len(xrefs) == 1
|
|
|
|
|
source, line = docutils.utils.get_source_line(xrefs[0])
|
|
|
|
|
assert 'index.rst' in source
|
|
|
|
|
assert line == 1
|
2023-02-14 23:45:28 -06:00
|
|
|
|
|
|
|
|
|
|
2023-05-11 08:28:57 -05:00
|
|
|
|
@pytest.mark.sphinx(
|
|
|
|
|
'html',
|
2024-08-12 16:34:03 -05:00
|
|
|
|
testroot='root',
|
2023-05-11 08:28:57 -05:00
|
|
|
|
confoverrides={
|
2024-08-11 08:58:56 -05:00
|
|
|
|
'python_maximum_signature_line_length': len('hello(name: str) -> str'),
|
2023-05-11 08:28:57 -05:00
|
|
|
|
'maximum_signature_line_length': 1,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
def test_python_maximum_signature_line_length_overrides_global(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
text = '.. py:function:: hello(name: str) -> str'
|
2023-05-11 08:28:57 -05:00
|
|
|
|
doctree = restructuredtext.parse(app, text)
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_doctree = (
|
|
|
|
|
addnodes.index,
|
|
|
|
|
[
|
|
|
|
|
desc,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
desc_signature,
|
|
|
|
|
(
|
|
|
|
|
[desc_name, 'hello'],
|
|
|
|
|
desc_parameterlist,
|
|
|
|
|
[desc_returns, pending_xref, 'str'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
desc_content,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert_node(doctree, expected_doctree)
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_node(
|
|
|
|
|
doctree[1],
|
|
|
|
|
addnodes.desc,
|
|
|
|
|
desctype='function',
|
|
|
|
|
domain='py',
|
|
|
|
|
objtype='function',
|
|
|
|
|
no_index=False,
|
|
|
|
|
)
|
|
|
|
|
signame_node = [desc_sig_name, 'name']
|
|
|
|
|
expected_sig = [
|
|
|
|
|
desc_parameterlist,
|
|
|
|
|
desc_parameter,
|
|
|
|
|
(
|
|
|
|
|
signame_node,
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[nodes.inline, pending_xref, 'str'],
|
|
|
|
|
),
|
|
|
|
|
]
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert_node(doctree[1][0][1], expected_sig)
|
|
|
|
|
assert_node(doctree[1][0][1], desc_parameterlist, multi_line_parameter_list=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(
|
2024-08-11 08:58:56 -05:00
|
|
|
|
'html',
|
|
|
|
|
testroot='domain-py-python_maximum_signature_line_length',
|
2023-05-11 08:28:57 -05:00
|
|
|
|
)
|
2024-07-23 09:35:55 -05:00
|
|
|
|
def test_domain_py_python_maximum_signature_line_length_in_html(app):
|
2023-05-11 08:28:57 -05:00
|
|
|
|
app.build()
|
|
|
|
|
content = (app.outdir / 'index.html').read_text(encoding='utf8')
|
|
|
|
|
expected_parameter_list_hello = """\
|
|
|
|
|
|
|
|
|
|
<dl>
|
|
|
|
|
<dd>\
|
|
|
|
|
<em class="sig-param">\
|
|
|
|
|
<span class="n"><span class="pre">name</span></span>\
|
|
|
|
|
<span class="p"><span class="pre">:</span></span>\
|
|
|
|
|
<span class="w"> </span>\
|
|
|
|
|
<span class="n"><span class="pre">str</span></span>\
|
|
|
|
|
</em>,\
|
|
|
|
|
</dd>
|
|
|
|
|
</dl>
|
|
|
|
|
|
|
|
|
|
<span class="sig-paren">)</span> \
|
|
|
|
|
<span class="sig-return">\
|
|
|
|
|
<span class="sig-return-icon">→</span> \
|
|
|
|
|
<span class="sig-return-typehint"><span class="pre">str</span></span>\
|
|
|
|
|
</span>\
|
2023-08-11 23:21:16 -05:00
|
|
|
|
<a class="headerlink" href="#hello" title="Link to this definition">¶</a>\
|
2023-05-11 08:28:57 -05:00
|
|
|
|
</dt>\
|
|
|
|
|
"""
|
|
|
|
|
assert expected_parameter_list_hello in content
|
|
|
|
|
|
|
|
|
|
param_line_fmt = '<dd>{}</dd>\n'
|
|
|
|
|
param_name_fmt = (
|
|
|
|
|
'<em class="sig-param"><span class="n"><span class="pre">{}</span></span></em>'
|
|
|
|
|
)
|
|
|
|
|
optional_fmt = '<span class="optional">{}</span>'
|
|
|
|
|
|
|
|
|
|
expected_a = param_line_fmt.format(
|
2024-08-11 08:58:56 -05:00
|
|
|
|
optional_fmt.format('[')
|
|
|
|
|
+ param_name_fmt.format('a')
|
|
|
|
|
+ ','
|
|
|
|
|
+ optional_fmt.format('['),
|
2023-05-11 08:28:57 -05:00
|
|
|
|
)
|
|
|
|
|
assert expected_a in content
|
|
|
|
|
|
|
|
|
|
expected_b = param_line_fmt.format(
|
2024-08-11 08:58:56 -05:00
|
|
|
|
param_name_fmt.format('b')
|
|
|
|
|
+ ','
|
|
|
|
|
+ optional_fmt.format(']')
|
|
|
|
|
+ optional_fmt.format(']'),
|
2023-05-11 08:28:57 -05:00
|
|
|
|
)
|
|
|
|
|
assert expected_b in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_c = param_line_fmt.format(param_name_fmt.format('c') + ',')
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert expected_c in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_d = param_line_fmt.format(
|
|
|
|
|
param_name_fmt.format('d') + optional_fmt.format('[') + ','
|
|
|
|
|
)
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert expected_d in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_e = param_line_fmt.format(param_name_fmt.format('e') + ',')
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert expected_e in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_f = param_line_fmt.format(
|
|
|
|
|
param_name_fmt.format('f') + ',' + optional_fmt.format(']')
|
|
|
|
|
)
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert expected_f in content
|
|
|
|
|
|
|
|
|
|
expected_parameter_list_foo = """\
|
|
|
|
|
|
|
|
|
|
<dl>
|
|
|
|
|
{}{}{}{}{}{}</dl>
|
|
|
|
|
|
|
|
|
|
<span class="sig-paren">)</span>\
|
2023-08-11 23:21:16 -05:00
|
|
|
|
<a class="headerlink" href="#foo" title="Link to this definition">¶</a>\
|
2023-05-11 08:28:57 -05:00
|
|
|
|
</dt>\
|
|
|
|
|
""".format(expected_a, expected_b, expected_c, expected_d, expected_e, expected_f)
|
|
|
|
|
assert expected_parameter_list_foo in content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(
|
2024-08-11 08:58:56 -05:00
|
|
|
|
'text',
|
|
|
|
|
testroot='domain-py-python_maximum_signature_line_length',
|
2023-05-11 08:28:57 -05:00
|
|
|
|
)
|
2024-07-23 09:35:55 -05:00
|
|
|
|
def test_domain_py_python_maximum_signature_line_length_in_text(app):
|
2023-05-11 08:28:57 -05:00
|
|
|
|
app.build()
|
|
|
|
|
content = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
|
param_line_fmt = STDINDENT * ' ' + '{}\n'
|
2023-05-11 08:28:57 -05:00
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_parameter_list_hello = '(\n{}) -> str'.format(
|
|
|
|
|
param_line_fmt.format('name: str,')
|
|
|
|
|
)
|
2023-05-11 08:28:57 -05:00
|
|
|
|
|
|
|
|
|
assert expected_parameter_list_hello in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_a = param_line_fmt.format('[a,[')
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert expected_a in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_b = param_line_fmt.format('b,]]')
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert expected_b in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_c = param_line_fmt.format('c,')
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert expected_c in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_d = param_line_fmt.format('d[,')
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert expected_d in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_e = param_line_fmt.format('e,')
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert expected_e in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_f = param_line_fmt.format('f,]')
|
2023-05-11 08:28:57 -05:00
|
|
|
|
assert expected_f in content
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
expected_parameter_list_foo = '(\n{}{}{}{}{}{})'.format(
|
|
|
|
|
expected_a,
|
|
|
|
|
expected_b,
|
|
|
|
|
expected_c,
|
|
|
|
|
expected_d,
|
|
|
|
|
expected_e,
|
|
|
|
|
expected_f,
|
2023-05-11 08:28:57 -05:00
|
|
|
|
)
|
|
|
|
|
assert expected_parameter_list_foo in content
|
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-02-14 23:45:28 -06:00
|
|
|
|
def test_module_content_line_number(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
|
text = '.. py:module:: foo\n\n Some link here: :ref:`abc`\n'
|
2023-02-14 23:45:28 -06:00
|
|
|
|
doc = restructuredtext.parse(app, text)
|
|
|
|
|
xrefs = list(doc.findall(condition=addnodes.pending_xref))
|
|
|
|
|
assert len(xrefs) == 1
|
|
|
|
|
source, line = docutils.utils.get_source_line(xrefs[0])
|
|
|
|
|
assert 'index.rst' in source
|
|
|
|
|
assert line == 3
|
2023-04-05 07:07:25 -05:00
|
|
|
|
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
@pytest.mark.sphinx(
|
2024-08-12 16:34:03 -05:00
|
|
|
|
'html',
|
|
|
|
|
testroot='root',
|
2024-08-11 08:58:56 -05:00
|
|
|
|
freshenv=True,
|
|
|
|
|
confoverrides={'python_display_short_literal_types': True},
|
|
|
|
|
)
|
2023-04-05 07:07:25 -05:00
|
|
|
|
def test_short_literal_types(app):
|
|
|
|
|
text = """\
|
|
|
|
|
.. py:function:: literal_ints(x: Literal[1, 2, 3] = 1) -> None
|
|
|
|
|
.. py:function:: literal_union(x: Union[Literal["a"], Literal["b"], Literal["c"]]) -> None
|
|
|
|
|
"""
|
|
|
|
|
doctree = restructuredtext.parse(app, text)
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
addnodes.index,
|
|
|
|
|
[
|
|
|
|
|
desc,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
desc_signature,
|
|
|
|
|
(
|
|
|
|
|
[desc_name, 'literal_ints'],
|
|
|
|
|
[
|
|
|
|
|
desc_parameterlist,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
desc_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'x'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[
|
|
|
|
|
desc_sig_name,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_literal_number, '1'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_punctuation, '|'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_literal_number, '2'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_punctuation, '|'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_literal_number, '3'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_operator, '='],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[nodes.inline, '1'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_returns, pending_xref, 'None'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_content, ()],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
addnodes.index,
|
|
|
|
|
[
|
|
|
|
|
desc,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
desc_signature,
|
|
|
|
|
(
|
|
|
|
|
[desc_name, 'literal_union'],
|
|
|
|
|
[
|
|
|
|
|
desc_parameterlist,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
desc_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'x'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[
|
|
|
|
|
desc_sig_name,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_literal_string, "'a'"],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_punctuation, '|'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_literal_string, "'b'"],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_punctuation, '|'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_literal_string, "'c'"],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_returns, pending_xref, 'None'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_content, ()],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
)
|
2023-07-23 17:22:40 -05:00
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-07-23 17:22:40 -05:00
|
|
|
|
def test_function_pep_695(app):
|
|
|
|
|
text = """.. py:function:: func[\
|
|
|
|
|
S,\
|
|
|
|
|
T: int,\
|
|
|
|
|
U: (int, str),\
|
2024-07-14 23:17:36 -05:00
|
|
|
|
R: int | str,\
|
2023-07-23 17:22:40 -05:00
|
|
|
|
A: int | Annotated[int, ctype("char")],\
|
|
|
|
|
*V,\
|
|
|
|
|
**P\
|
|
|
|
|
]
|
|
|
|
|
"""
|
|
|
|
|
doctree = restructuredtext.parse(app, text)
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
addnodes.index,
|
|
|
|
|
[
|
|
|
|
|
desc,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
desc_signature,
|
|
|
|
|
(
|
|
|
|
|
[desc_name, 'func'],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter_list,
|
|
|
|
|
(
|
|
|
|
|
[desc_type_parameter, ([desc_sig_name, 'S'])],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'T'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_name, ([pending_xref, 'int'])],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'U'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_punctuation, '('],
|
|
|
|
|
[
|
|
|
|
|
desc_sig_name,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[pending_xref, 'str'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_sig_punctuation, ')'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'R'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[
|
|
|
|
|
desc_sig_name,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_punctuation, '|'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[pending_xref, 'str'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'A'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[
|
|
|
|
|
desc_sig_name,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_space, ' '],
|
|
|
|
|
[desc_sig_punctuation, '|'],
|
|
|
|
|
[desc_sig_space, ' '],
|
|
|
|
|
[pending_xref, 'Annotated'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'int'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
[desc_sig_space, ' '],
|
|
|
|
|
[pending_xref, 'ctype'],
|
|
|
|
|
[desc_sig_punctuation, '('],
|
|
|
|
|
[desc_sig_literal_string, "'char'"],
|
|
|
|
|
[desc_sig_punctuation, ')'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_operator, '*'],
|
|
|
|
|
[desc_sig_name, 'V'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_operator, '**'],
|
|
|
|
|
[desc_sig_name, 'P'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_parameterlist, ()],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_content, ()],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
)
|
2023-07-23 17:22:40 -05:00
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-07-23 17:22:40 -05:00
|
|
|
|
def test_class_def_pep_695(app):
|
|
|
|
|
# Non-concrete unbound generics are allowed at runtime but type checkers
|
|
|
|
|
# should fail (https://peps.python.org/pep-0695/#type-parameter-scopes)
|
|
|
|
|
text = """.. py:class:: Class[S: Sequence[T], T, KT, VT](Dict[KT, VT])"""
|
|
|
|
|
doctree = restructuredtext.parse(app, text)
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
addnodes.index,
|
|
|
|
|
[
|
|
|
|
|
desc,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
desc_signature,
|
|
|
|
|
(
|
|
|
|
|
[desc_annotation, ('class', desc_sig_space)],
|
|
|
|
|
[desc_name, 'Class'],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter_list,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'S'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[
|
|
|
|
|
desc_sig_name,
|
|
|
|
|
(
|
|
|
|
|
[pending_xref, 'Sequence'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'T'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_type_parameter, ([desc_sig_name, 'T'])],
|
|
|
|
|
[desc_type_parameter, ([desc_sig_name, 'KT'])],
|
|
|
|
|
[desc_type_parameter, ([desc_sig_name, 'VT'])],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_parameterlist, ([desc_parameter, 'Dict[KT, VT]'])],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_content, ()],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
)
|
2023-07-23 17:22:40 -05:00
|
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-07-23 17:22:40 -05:00
|
|
|
|
def test_class_def_pep_696(app):
|
|
|
|
|
# test default values for type variables without using PEP 696 AST parser
|
|
|
|
|
text = """.. py:class:: Class[\
|
|
|
|
|
T, KT, VT,\
|
|
|
|
|
J: int,\
|
|
|
|
|
K = list,\
|
|
|
|
|
S: str = str,\
|
|
|
|
|
L: (T, tuple[T, ...], collections.abc.Iterable[T]) = set[T],\
|
|
|
|
|
Q: collections.abc.Mapping[KT, VT] = dict[KT, VT],\
|
|
|
|
|
*V = *tuple[*Ts, bool],\
|
|
|
|
|
**P = [int, Annotated[int, ValueRange(3, 10), ctype("char")]]\
|
|
|
|
|
](Other[T, KT, VT, J, S, L, Q, *V, **P])
|
|
|
|
|
"""
|
|
|
|
|
doctree = restructuredtext.parse(app, text)
|
2024-08-11 08:58:56 -05:00
|
|
|
|
assert_node(
|
|
|
|
|
doctree,
|
|
|
|
|
(
|
|
|
|
|
addnodes.index,
|
|
|
|
|
[
|
|
|
|
|
desc,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
desc_signature,
|
|
|
|
|
(
|
|
|
|
|
[desc_annotation, ('class', desc_sig_space)],
|
|
|
|
|
[desc_name, 'Class'],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter_list,
|
|
|
|
|
(
|
|
|
|
|
[desc_type_parameter, ([desc_sig_name, 'T'])],
|
|
|
|
|
[desc_type_parameter, ([desc_sig_name, 'KT'])],
|
|
|
|
|
[desc_type_parameter, ([desc_sig_name, 'VT'])],
|
|
|
|
|
# J: int
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'J'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_name, ([pending_xref, 'int'])],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
# K = list
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'K'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_operator, '='],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[nodes.inline, 'list'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
# S: str = str
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'S'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_name, ([pending_xref, 'str'])],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_operator, '='],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[nodes.inline, 'str'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'L'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_punctuation, '('],
|
|
|
|
|
[
|
|
|
|
|
desc_sig_name,
|
|
|
|
|
(
|
|
|
|
|
# T
|
|
|
|
|
[pending_xref, 'T'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
# tuple[T, ...]
|
|
|
|
|
[pending_xref, 'tuple'],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'T'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_punctuation, '...'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
# collections.abc.Iterable[T]
|
|
|
|
|
[
|
|
|
|
|
pending_xref,
|
|
|
|
|
'collections.abc.Iterable',
|
|
|
|
|
],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'T'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_sig_punctuation, ')'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_operator, '='],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[nodes.inline, 'set[T]'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_name, 'Q'],
|
|
|
|
|
[desc_sig_punctuation, ':'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[
|
|
|
|
|
desc_sig_name,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
pending_xref,
|
|
|
|
|
'collections.abc.Mapping',
|
|
|
|
|
],
|
|
|
|
|
[desc_sig_punctuation, '['],
|
|
|
|
|
[pending_xref, 'KT'],
|
|
|
|
|
[desc_sig_punctuation, ','],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[pending_xref, 'VT'],
|
|
|
|
|
[desc_sig_punctuation, ']'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_operator, '='],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[nodes.inline, 'dict[KT, VT]'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_operator, '*'],
|
|
|
|
|
[desc_sig_name, 'V'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_operator, '='],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[nodes.inline, '*tuple[*Ts, bool]'],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
desc_type_parameter,
|
|
|
|
|
(
|
|
|
|
|
[desc_sig_operator, '**'],
|
|
|
|
|
[desc_sig_name, 'P'],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[desc_sig_operator, '='],
|
|
|
|
|
desc_sig_space,
|
|
|
|
|
[
|
|
|
|
|
nodes.inline,
|
|
|
|
|
'[int, Annotated[int, ValueRange(3, 10), ctype("char")]]',
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
desc_parameterlist,
|
|
|
|
|
(
|
|
|
|
|
[
|
|
|
|
|
desc_parameter,
|
|
|
|
|
'Other[T, KT, VT, J, S, L, Q, *V, **P]',
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[desc_content, ()],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
('tp_list', 'tptext'),
|
|
|
|
|
[
|
|
|
|
|
('[T:int]', '[T: int]'),
|
|
|
|
|
('[T:*Ts]', '[T: *Ts]'),
|
|
|
|
|
('[T:int|(*Ts)]', '[T: int | (*Ts)]'),
|
|
|
|
|
('[T:(*Ts)|int]', '[T: (*Ts) | int]'),
|
|
|
|
|
('[T:(int|(*Ts))]', '[T: (int | (*Ts))]'),
|
|
|
|
|
('[T:((*Ts)|int)]', '[T: ((*Ts) | int)]'),
|
|
|
|
|
("[T:Annotated[int,ctype('char')]]", "[T: Annotated[int, ctype('char')]]"),
|
|
|
|
|
],
|
|
|
|
|
)
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-07-23 17:22:40 -05:00
|
|
|
|
def test_pep_695_and_pep_696_whitespaces_in_bound(app, tp_list, tptext):
|
|
|
|
|
text = f'.. py:function:: f{tp_list}()'
|
|
|
|
|
doctree = restructuredtext.parse(app, text)
|
|
|
|
|
assert doctree.astext() == f'\n\nf{tptext}()\n\n'
|
|
|
|
|
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
('tp_list', 'tptext'),
|
|
|
|
|
[
|
|
|
|
|
('[T:(int,str)]', '[T: (int, str)]'),
|
|
|
|
|
('[T:(int|str,*Ts)]', '[T: (int | str, *Ts)]'),
|
|
|
|
|
],
|
|
|
|
|
)
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-07-23 17:22:40 -05:00
|
|
|
|
def test_pep_695_and_pep_696_whitespaces_in_constraints(app, tp_list, tptext):
|
|
|
|
|
text = f'.. py:function:: f{tp_list}()'
|
|
|
|
|
doctree = restructuredtext.parse(app, text)
|
|
|
|
|
assert doctree.astext() == f'\n\nf{tptext}()\n\n'
|
|
|
|
|
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
('tp_list', 'tptext'),
|
|
|
|
|
[
|
|
|
|
|
('[T=int]', '[T = int]'),
|
|
|
|
|
('[T:int=int]', '[T: int = int]'),
|
|
|
|
|
('[*V=*Ts]', '[*V = *Ts]'),
|
|
|
|
|
('[*V=(*Ts)]', '[*V = (*Ts)]'),
|
|
|
|
|
('[*V=*tuple[str,...]]', '[*V = *tuple[str, ...]]'),
|
|
|
|
|
('[*V=*tuple[*Ts,...]]', '[*V = *tuple[*Ts, ...]]'),
|
|
|
|
|
('[*V=*tuple[int,*Ts]]', '[*V = *tuple[int, *Ts]]'),
|
|
|
|
|
('[*V=*tuple[*Ts,int]]', '[*V = *tuple[*Ts, int]]'),
|
|
|
|
|
('[**P=[int,*Ts]]', '[**P = [int, *Ts]]'),
|
|
|
|
|
('[**P=[int, int*3]]', '[**P = [int, int * 3]]'),
|
|
|
|
|
('[**P=[int, *Ts*3]]', '[**P = [int, *Ts * 3]]'),
|
|
|
|
|
('[**P=[int,A[int,ctype("char")]]]', '[**P = [int, A[int, ctype("char")]]]'),
|
|
|
|
|
],
|
|
|
|
|
)
|
2024-08-12 16:34:03 -05:00
|
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-07-23 17:22:40 -05:00
|
|
|
|
def test_pep_695_and_pep_696_whitespaces_in_default(app, tp_list, tptext):
|
|
|
|
|
text = f'.. py:function:: f{tp_list}()'
|
|
|
|
|
doctree = restructuredtext.parse(app, text)
|
|
|
|
|
assert doctree.astext() == f'\n\nf{tptext}()\n\n'
|