Skip to content

Commit ace9d97

Browse files
chrisjsewellpicnixzdanieleades
authored
👌 Improve signature and type annotations for SphinxLoggerAdapter.warning (#12154)
This commit overrides the `SphinxLoggerAdapter.warning` method, to provide documentation and type annotations specific to logging sphinx warnings. This should aide sphinx core/extension developers in creating good warnings. Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: danieleades <[email protected]>
1 parent d7c94d1 commit ace9d97

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

‎sphinx/util/logging.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,52 @@ def process(self, msg: str, kwargs: dict) -> tuple[str, dict]: # type: ignore[o
147147
def handle(self, record: logging.LogRecord) -> None:
148148
self.logger.handle(record)
149149

150+
def warning( # type: ignore[override]
151+
self,
152+
msg: object,
153+
*args: object,
154+
type: None | str = None,
155+
subtype: None | str = None,
156+
location: None | str | tuple[str | None, int | None] | Node = None,
157+
nonl: bool = True,
158+
color: str | None = None,
159+
once: bool = False,
160+
**kwargs: Any,
161+
) -> None:
162+
"""Log a sphinx warning.
163+
164+
It is recommended to include a ``type`` and ``subtype`` for warnings as
165+
these can be displayed to the user using :confval:`show_warning_types`
166+
and used in :confval:`suppress_warnings` to suppress specific warnings.
167+
168+
It is also recommended to specify a ``location`` whenever possible
169+
to help users in correcting the warning.
170+
171+
:param msg: The message, which may contain placeholders for ``args``.
172+
:param args: The arguments to substitute into ``msg``.
173+
:param type: The type of the warning.
174+
:param subtype: The subtype of the warning.
175+
:param location: The source location of the warning's origin,
176+
which can be a string (the ``docname`` or ``docname:lineno``),
177+
a tuple of ``(docname, lineno)``,
178+
or the docutils node object.
179+
:param nonl: Whether to append a new line terminator to the message.
180+
:param color: A color code for the message.
181+
:param once: Do not log this warning,
182+
if a previous warning already has same ``msg``, ``args`` and ``once=True``.
183+
"""
184+
return super().warning(
185+
msg,
186+
*args,
187+
type=type,
188+
subtype=subtype,
189+
location=location,
190+
nonl=nonl,
191+
color=color,
192+
once=once,
193+
**kwargs,
194+
)
195+
150196

151197
class WarningStreamHandler(logging.StreamHandler):
152198
"""StreamHandler for warnings."""

0 commit comments

Comments
 (0)