Skip to content

✨ Add show_warning_types configuration variable #12131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Deprecated
Features added
--------------

* #12131: Added :confval:`show_warning_types` configuration option.
Patch by Chris Sewell.

* #11701: HTML Search: Adopt the new `<search>`_ element.
Patch by Bénédikt Tran.

Expand Down
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
release = version
show_authors = True
nitpicky = True
show_warning_types = True

html_theme = 'sphinx13'
html_theme_path = ['_themes']
Expand Down
13 changes: 10 additions & 3 deletions doc/usage/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,18 @@ General configuration

.. versionadded:: 0.5

.. confval:: show_warning_types

If ``True``, the type of each warning is added as a suffix to the warning message.
The default is ``False``.

.. versionadded:: 7.3.0

.. confval:: suppress_warnings

A list of warning types to suppress arbitrary warning messages.

Sphinx supports following warning types:
Sphinx core supports following warning types:

* ``app.add_node``
* ``app.add_directive``
Expand Down Expand Up @@ -359,11 +366,11 @@ General configuration
* ``toc.not_readable``
* ``toc.secnum``

Then extensions can also define their own warning types.

You can choose from these types. You can also give only the first
component to exclude all warnings attached to it.

Now, this option should be considered *experimental*.

.. versionadded:: 1.4

.. versionchanged:: 1.5
Expand Down
1 change: 1 addition & 0 deletions sphinx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class Config:
'template_bridge': _Opt(None, 'html', frozenset((str,))),
'keep_warnings': _Opt(False, 'env', ()),
'suppress_warnings': _Opt([], 'env', ()),
'show_warning_types': _Opt(False, 'env', ()),
'modindex_common_prefix': _Opt([], 'html', ()),
'rst_epilog': _Opt(None, 'env', frozenset((str,))),
'rst_prolog': _Opt(None, 'env', frozenset((str,))),
Expand Down
18 changes: 18 additions & 0 deletions sphinx/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ class SphinxLogRecordTranslator(logging.Filter):

* Make a instance of SphinxLogRecord
* docname to path if location given
* append warning type/subtype to message if show_warning_types is True
"""

LogRecordClass: type[logging.LogRecord]
Expand Down Expand Up @@ -522,6 +523,23 @@ class WarningLogRecordTranslator(SphinxLogRecordTranslator):

LogRecordClass = SphinxWarningLogRecord

def filter(self, record: SphinxWarningLogRecord) -> bool: # type: ignore[override]
super().filter(record)

try:
show_warning_types = self.app.config.show_warning_types
except AttributeError:
# config is not initialized yet (ex. in conf.py)
show_warning_types = False
if show_warning_types:
if log_type := getattr(record, 'type', ''):
if log_subtype := getattr(record, 'subtype', ''):
record.msg += f' [{log_type}.{log_subtype}]'
else:
record.msg += f' [{log_type}]'

return True


def get_node_location(node: Node) -> str | None:
source, line = get_source_line(node)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_util/test_util_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,16 @@ def test_get_node_location_abspath():
location = logging.get_node_location(n)

assert location == absolute_filename + ':'


@pytest.mark.sphinx(confoverrides={'show_warning_types': True})
def test_show_warning_types(app, status, warning):
logging.setup(app, status, warning)
logger = logging.getLogger(__name__)
logger.warning('message2')
logger.warning('message3', type='test')
logger.warning('message4', type='test', subtype='logging')

assert 'WARNING: message2' in warning.getvalue()
assert 'WARNING: message3 [test]' in warning.getvalue()
assert 'WARNING: message4 [test.logging]' in warning.getvalue()