Skip to content

Commit d323997

Browse files
vstinnergpsheadhugovk
authored
gh-105376: Restore deprecated logging warn() method (#122775)
This reverts commit dcc028d and commit 6c54e5d. Keep the deprecated logging warn() method in Python 3.13. Co-authored-by: Gregory P. Smith <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent b4a3160 commit d323997

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

Doc/deprecations/pending-removal-in-future.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ although there is currently no date scheduled for their removal.
6767
* ``EntryPoints`` tuple interface.
6868
* Implicit ``None`` on return values.
6969

70+
* :mod:`logging`: the ``warn()`` method has been deprecated
71+
since Python 3.3, use :meth:`~logging.warning()` instead.
72+
7073
* :mod:`mailbox`: Use of StringIO input and text mode is deprecated, use
7174
BytesIO and binary mode instead.
7275

Doc/library/logging.rst

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,6 @@ in a module, ``__name__`` is the module's name in the Python package namespace.
352352
.. versionchanged:: 3.8
353353
The *stacklevel* parameter was added.
354354

355-
.. versionchanged:: 3.13
356-
Remove the undocumented ``warn()`` method which was an alias to the
357-
:meth:`warning` method.
358-
359355

360356
.. method:: Logger.info(msg, *args, **kwargs)
361357

@@ -368,6 +364,10 @@ in a module, ``__name__`` is the module's name in the Python package namespace.
368364
Logs a message with level :const:`WARNING` on this logger. The arguments are
369365
interpreted as for :meth:`debug`.
370366

367+
.. note:: There is an obsolete method ``warn`` which is functionally
368+
identical to ``warning``. As ``warn`` is deprecated, please do not use
369+
it - use ``warning`` instead.
370+
371371
.. method:: Logger.error(msg, *args, **kwargs)
372372

373373
Logs a message with level :const:`ERROR` on this logger. The arguments are
@@ -1124,11 +1124,6 @@ information into logging calls. For a usage example, see the section on
11241124
Attribute :attr:`!manager` and method :meth:`!_log` were added, which
11251125
delegate to the underlying logger and allow adapters to be nested.
11261126

1127-
.. versionchanged:: 3.13
1128-
1129-
Remove the undocumented :meth:`!warn`` method which was an alias to the
1130-
:meth:`!warning` method.
1131-
11321127
.. versionchanged:: 3.13
11331128

11341129
The *merge_extra* argument was added.
@@ -1224,10 +1219,6 @@ functions.
12241219
identical to ``warning``. As ``warn`` is deprecated, please do not use
12251220
it - use ``warning`` instead.
12261221

1227-
.. versionchanged:: 3.13
1228-
Remove the undocumented ``warn()`` function which was an alias to the
1229-
:func:`warning` function.
1230-
12311222

12321223
.. function:: error(msg, *args, **kwargs)
12331224

Doc/whatsnew/3.13.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,16 +1397,6 @@ locale
13971397
use ``locale.setlocale(locale.LC_ALL, "")`` instead.
13981398
(Contributed by Victor Stinner in :gh:`104783`.)
13991399

1400-
logging
1401-
-------
1402-
1403-
* :mod:`logging`: Remove undocumented and untested ``Logger.warn()`` and
1404-
``LoggerAdapter.warn()`` methods and ``logging.warn()`` function. Deprecated
1405-
since Python 3.3, they were aliases to the :meth:`logging.Logger.warning`
1406-
method, :meth:`!logging.LoggerAdapter.warning` method and
1407-
:func:`logging.warning` function.
1408-
(Contributed by Victor Stinner in :gh:`105376`.)
1409-
14101400
pathlib
14111401
-------
14121402

Lib/logging/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
'captureWarnings', 'critical', 'debug', 'disable', 'error',
3838
'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass',
3939
'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown',
40-
'warning', 'getLogRecordFactory', 'setLogRecordFactory',
40+
'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory',
4141
'lastResort', 'raiseExceptions', 'getLevelNamesMapping',
4242
'getHandlerByName', 'getHandlerNames']
4343

@@ -1530,6 +1530,11 @@ def warning(self, msg, *args, **kwargs):
15301530
if self.isEnabledFor(WARNING):
15311531
self._log(WARNING, msg, args, **kwargs)
15321532

1533+
def warn(self, msg, *args, **kwargs):
1534+
warnings.warn("The 'warn' method is deprecated, "
1535+
"use 'warning' instead", DeprecationWarning, 2)
1536+
self.warning(msg, *args, **kwargs)
1537+
15331538
def error(self, msg, *args, **kwargs):
15341539
"""
15351540
Log 'msg % args' with severity 'ERROR'.
@@ -1906,6 +1911,11 @@ def warning(self, msg, *args, **kwargs):
19061911
"""
19071912
self.log(WARNING, msg, *args, **kwargs)
19081913

1914+
def warn(self, msg, *args, **kwargs):
1915+
warnings.warn("The 'warn' method is deprecated, "
1916+
"use 'warning' instead", DeprecationWarning, 2)
1917+
self.warning(msg, *args, **kwargs)
1918+
19091919
def error(self, msg, *args, **kwargs):
19101920
"""
19111921
Delegate an error call to the underlying logger.
@@ -2169,6 +2179,11 @@ def warning(msg, *args, **kwargs):
21692179
basicConfig()
21702180
root.warning(msg, *args, **kwargs)
21712181

2182+
def warn(msg, *args, **kwargs):
2183+
warnings.warn("The 'warn' function is deprecated, "
2184+
"use 'warning' instead", DeprecationWarning, 2)
2185+
warning(msg, *args, **kwargs)
2186+
21722187
def info(msg, *args, **kwargs):
21732188
"""
21742189
Log a message with severity 'INFO' on the root logger. If the logger has
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Restore the deprecated :mod:`logging` ``warn()`` method. It was removed in
2+
Python 3.13 alpha 1. Keep the deprecated ``warn()`` method in Python 3.13.
3+
Patch by Victor Stinner.

0 commit comments

Comments
 (0)