Skip to content

Commit 706f5f9

Browse files
authored
Warn on deprecated Python-specific index types (#11412)
1 parent db54618 commit 706f5f9

File tree

14 files changed

+91
-71
lines changed

14 files changed

+91
-71
lines changed

CHANGES

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Incompatible changes
1414
Deprecated
1515
----------
1616

17+
* #11412: Emit warnings on using a deprecated Python-specific index entry type
18+
(namely, ``module``, ``keyword``, ``operator``, ``object``, ``exception``,
19+
``statement``, and ``builtin``) in the :rst:dir:`index` directive, and
20+
set the removal version to Sphinx 9. Patch by Adam Turner.
21+
1722
Features added
1823
--------------
1924

doc/usage/configuration.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ General configuration
337337
* ``epub.unknown_project_files``
338338
* ``epub.duplicated_toc_entry``
339339
* ``i18n.inconsistent_references``
340+
* ``index``
340341
* ``image.not_readable``
341342
* ``ref.term``
342343
* ``ref.ref``
@@ -388,6 +389,10 @@ General configuration
388389

389390
Added ``i18n.inconsistent_references``
390391

392+
.. versionadded:: 7.1
393+
394+
Added ``index`` warning type.
395+
391396
.. confval:: needs_sphinx
392397

393398
If set to a ``major.minor`` version string like ``'1.1'``, Sphinx will

doc/usage/restructuredtext/directives.rst

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,10 @@ mainly contained in information units, such as the language reference.
896896

897897
.. index::
898898
single: execution; context
899-
module: __main__
900-
module: sys
899+
pair: module; __main__
900+
pair: module; sys
901901
triple: module; search; path
902+
seealso: scope
902903

903904
The execution context
904905
---------------------
@@ -916,25 +917,63 @@ mainly contained in information units, such as the language reference.
916917
The possible entry types are:
917918

918919
single
919-
Creates a single index entry. Can be made a subentry by separating the
920-
subentry text with a semicolon (this notation is also used below to
921-
describe what entries are created).
920+
Creates a single index entry.
921+
Can be made a sub-entry by separating the sub-entry text with a semicolon
922+
(this notation is also used below to describe what entries are created).
923+
Examples:
924+
925+
.. code:: reStructuredText
926+
927+
.. index:: single: execution
928+
single: execution; context
929+
930+
- ``single: execution`` creates an index entry labelled ``execution``.
931+
- ``single: execution; context`` creates an sub-entry of ``execution``
932+
labelled ``context``.
922933
pair
923-
``pair: loop; statement`` is a shortcut that creates two index entries,
924-
namely ``loop; statement`` and ``statement; loop``.
934+
A shortcut to create two index entries.
935+
The pair of values must be separated by a semicolon.
936+
Example:
937+
938+
.. code:: reStructuredText
939+
940+
.. index:: pair: loop; statement
941+
942+
This would create two index entries; ``loop; statement`` and ``statement; loop``.
925943
triple
926-
Likewise, ``triple: module; search; path`` is a shortcut that creates
927-
three index entries, which are ``module; search path``, ``search; path,
928-
module`` and ``path; module search``.
944+
A shortcut to create three index entries.
945+
All three values must be separated by a semicolon.
946+
Example:
947+
948+
.. code:: reStructuredText
949+
950+
.. index:: triple: module; search; path
951+
952+
This would create three index entries; ``module; search path``,
953+
``search; path, module``, and ``path; module search``.
929954
see
930-
``see: entry; other`` creates an index entry that refers from ``entry`` to
931-
``other``.
955+
A shortcut to create an index entry that refers to another entry.
956+
Example:
957+
958+
.. code:: reStructuredText
959+
960+
.. index:: see: entry; other
961+
962+
This would create an index entry referring from ``entry`` to ``other``
963+
(i.e. 'entry': See 'other').
932964
seealso
933-
Like ``see``, but inserts "see also" instead of "see".
965+
Like ``see``, but inserts 'see also' instead of 'see'.
934966
module, keyword, operator, object, exception, statement, builtin
935-
These all create two index entries. For example, ``module: hashlib``
936-
creates the entries ``module; hashlib`` and ``hashlib; module``. (These
937-
are Python-specific and therefore deprecated.)
967+
These **deprecated** shortcuts all create two index entries.
968+
For example, ``module: hashlib`` creates the entries ``module; hashlib``
969+
and ``hashlib; module``.
970+
971+
.. deprecated:: 1.0
972+
These Python-specific entry types are deprecated.
973+
974+
.. versionchanged:: 7.1
975+
Removal version set to Sphinx 9.0.
976+
Using these entry types will now emit warnings with the ``index`` category.
938977

939978
You can mark up "main" index entries by prefixing them with an exclamation
940979
mark. The references to "main" entries are emphasized in the generated

sphinx/builders/gettext.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from sphinx import addnodes, package_dir
1717
from sphinx.application import Sphinx
1818
from sphinx.builders import Builder
19-
from sphinx.domains.python import pairindextypes
2019
from sphinx.errors import ThemeError
2120
from sphinx.locale import __
2221
from sphinx.util import logging, split_index_msg
@@ -159,10 +158,6 @@ def write_doc(self, docname: str, doctree: nodes.document) -> None:
159158
for node, entries in traverse_translatable_index(doctree):
160159
for typ, msg, _tid, _main, _key in entries:
161160
for m in split_index_msg(typ, msg):
162-
if typ == 'pair' and m in pairindextypes.values():
163-
# avoid built-in translated message was incorporated
164-
# in 'sphinx.util.nodes.process_index_entry'
165-
continue
166161
catalog.add(m, node)
167162

168163

sphinx/domains/python.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050

5151

5252
pairindextypes = {
53-
'module': _('module'),
54-
'keyword': _('keyword'),
55-
'operator': _('operator'),
56-
'object': _('object'),
57-
'exception': _('exception'),
58-
'statement': _('statement'),
59-
'builtin': _('built-in function'),
53+
'module': 'module',
54+
'keyword': 'keyword',
55+
'operator': 'operator',
56+
'object': 'object',
57+
'exception': 'exception',
58+
'statement': 'statement',
59+
'builtin': 'built-in function',
6060
}
6161

6262

@@ -729,7 +729,7 @@ def add_target_and_index(self, name_cls: tuple[str, str], sig: str,
729729
text = _('%s() (in module %s)') % (name, modname)
730730
self.indexnode['entries'].append(('single', text, node_id, '', None))
731731
else:
732-
text = f'{pairindextypes["builtin"]}; {name}()'
732+
text = f'built-in function; {name}()'
733733
self.indexnode['entries'].append(('pair', text, node_id, '', None))
734734

735735
def get_index_text(self, modname: str, name_cls: tuple[str, str]) -> str | None:
@@ -1058,7 +1058,7 @@ def run(self) -> list[Node]:
10581058
# the platform and synopsis aren't printed; in fact, they are only
10591059
# used in the modindex currently
10601060
ret.append(target)
1061-
indextext = f'{pairindextypes["module"]}; {modname}'
1061+
indextext = f'module; {modname}'
10621062
inode = addnodes.index(entries=[('pair', indextext, node_id, '', None)])
10631063
ret.append(inode)
10641064
ret.extend(content_node.children)

sphinx/locale/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,3 @@ def gettext(message: str) -> str:
223223
'tip': _('Tip'),
224224
'warning': _('Warning'),
225225
}
226-
227-
# Moved to sphinx.directives.other (will be overridden later)
228-
versionlabels: dict[str, str] = {}
229-
230-
# Moved to sphinx.domains.python (will be overridden later)
231-
pairindextypes: dict[str, str] = {}

sphinx/util/nodes.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,19 +365,23 @@ def process_index_entry(entry: str, targetid: str,
365365
if entry.startswith('!'):
366366
main = 'main'
367367
entry = entry[1:].lstrip()
368-
for type in pairindextypes:
369-
if entry.startswith(type + ':'):
370-
value = entry[len(type) + 1:].strip()
371-
value = pairindextypes[type] + '; ' + value
368+
for index_type in pairindextypes:
369+
if entry.startswith(f'{index_type}:'):
370+
value = entry[len(index_type) + 1:].strip()
371+
value = f'{pairindextypes[index_type]}; {value}'
372+
# xref RemovedInSphinx90Warning
373+
logger.warning(__('%r is deprecated for index entries (from entry %r). '
374+
"Use 'pair: %s' instead."),
375+
index_type, entry, value, type='index')
372376
indexentries.append(('pair', value, targetid, main, None))
373377
break
374378
else:
375-
for type in indextypes:
376-
if entry.startswith(type + ':'):
377-
value = entry[len(type) + 1:].strip()
378-
if type == 'double':
379-
type = 'pair'
380-
indexentries.append((type, value, targetid, main, None))
379+
for index_type in indextypes:
380+
if entry.startswith(f'{index_type}:'):
381+
value = entry[len(index_type) + 1:].strip()
382+
if index_type == 'double':
383+
index_type = 'pair'
384+
indexentries.append((index_type, value, targetid, main, None))
381385
break
382386
# shorthand notation for single entries
383387
else:

tests/roots/test-intl/index_entries.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,5 @@ various index entries
2020
triple: First; Second; Third
2121
see: Entry; Mailing List
2222
seealso: See; Newsletter
23-
module: Module
24-
keyword: Keyword
25-
operator: Operator
26-
object: Object
27-
exception: Exception
28-
statement: Statement
29-
builtin: Builtin
3023

3124
That's all.

tests/roots/test-root/markup.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ Index markup
373373
pair: entry; pair
374374
double: entry; double
375375
triple: index; entry; triple
376-
keyword: with
377376
see: from; to
378377
seealso: fromalso; toalso
379378

tests/roots/test-warnings/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Don't download :download:`this <nonexisting.png>`.
2727
.. index::
2828
single:
2929
pair:
30-
keyword:
30+
seealso:
3131

3232
.. Invalid code-block
3333
.. code-block:: c

0 commit comments

Comments
 (0)