Skip to content

Commit b6dc14a

Browse files
committed
gh-106137: Add SoftDeprecationWarning category
* Add SoftDeprecationWarning warning category. * Add default warnings filters for SoftDeprecationWarning: ignore SoftDeprecationWarning by default, except in the __main__ module (similar to PEP 565). * Add warnings._soft_deprecated(): only emit SoftDeprecationWarning in Python Development Mode and if Python is built in debug mode. * Add PyExc_SoftDeprecationWarning to the limited C API.
1 parent bb578a0 commit b6dc14a

File tree

15 files changed

+63
-1
lines changed

15 files changed

+63
-1
lines changed

Doc/data/stable_abi.dat

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/library/exceptions.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,19 @@ The following exceptions are used as warning categories; see the
802802
The deprecation policy is described in :pep:`387`.
803803

804804

805+
.. exception:: SoftDeprecationWarning
806+
807+
Base class for warnings about soft deprecated features when those warnings
808+
are intended for other Python developers.
809+
810+
Ignored by the default warning filters, except in the ``__main__`` module.
811+
Enabling the :ref:`Python Development Mode <devmode>` shows this warning.
812+
813+
The soft deprecation policy is described in :pep:`387`.
814+
815+
.. versionadded:: 3.13
816+
817+
805818
.. exception:: PendingDeprecationWarning
806819

807820
Base class for warnings about features which are obsolete and

Doc/whatsnew/3.13.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ New Features
7676
Other Language Changes
7777
======================
7878

79+
* Add :exc:`SoftDeprecationWarning` warning category to implement soft
80+
deprecation. The soft deprecation policy is described in :pep:`387`.
81+
(Contributed by Victor Stinner in :gh:`106137`.)
7982

8083

8184
New Modules
@@ -441,6 +444,10 @@ New Features
441444
``NULL`` if the referent is no longer live.
442445
(Contributed by Victor Stinner in :gh:`105927`.)
443446

447+
* Add :exc:`PyExc_SoftDeprecationWarning` warning category to implement soft
448+
deprecation. The soft deprecation policy is described in :pep:`387`.
449+
(Contributed by Victor Stinner in :gh:`106137`.)
450+
444451
Porting to Python 3.13
445452
----------------------
446453

Include/pyerrors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ PyAPI_DATA(PyObject *) PyExc_Warning;
153153
PyAPI_DATA(PyObject *) PyExc_UserWarning;
154154
PyAPI_DATA(PyObject *) PyExc_DeprecationWarning;
155155
PyAPI_DATA(PyObject *) PyExc_PendingDeprecationWarning;
156+
PyAPI_DATA(PyObject *) PyExc_SoftDeprecationWarning;
156157
PyAPI_DATA(PyObject *) PyExc_SyntaxWarning;
157158
PyAPI_DATA(PyObject *) PyExc_RuntimeWarning;
158159
PyAPI_DATA(PyObject *) PyExc_FutureWarning;

Lib/test/exception_hierarchy.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ BaseException
6262
├── PendingDeprecationWarning
6363
├── ResourceWarning
6464
├── RuntimeWarning
65+
├── SoftDeprecationWarning
6566
├── SyntaxWarning
6667
├── UnicodeWarning
6768
└── UserWarning

Lib/test/support/warnings_helper.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ def import_deprecated(name):
1313
return importlib.import_module(name)
1414

1515

16+
def import_soft_deprecated(name):
17+
"""Import *name* while suppressing SoftDeprecationWarning."""
18+
with warnings.catch_warnings():
19+
warnings.simplefilter('ignore', category=SoftDeprecationWarning)
20+
return importlib.import_module(name)
21+
22+
1623
def check_syntax_warning(testcase, statement, errtext='',
1724
*, lineno=1, offset=None):
1825
# Test also that a warning is emitted only once.

Lib/test/test_stable_abi_ctypes.py

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_warnings/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,8 +1242,10 @@ def test_default_filter_configuration(self):
12421242
main_module_filter = "__main__"
12431243
expected_default_filters = [
12441244
('default', None, DeprecationWarning, main_module_filter, 0),
1245+
('default', None, SoftDeprecationWarning, main_module_filter, 0),
12451246
('ignore', None, DeprecationWarning, None, 0),
12461247
('ignore', None, PendingDeprecationWarning, None, 0),
1248+
('ignore', None, SoftDeprecationWarning, None, 0),
12471249
('ignore', None, ImportWarning, None, 0),
12481250
('ignore', None, ResourceWarning, None, 0),
12491251
]

Lib/warnings.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,13 @@ def _deprecated(name, message=_DEPRECATED_MSG, *, remove, _version=sys.version_i
529529
warn(msg, DeprecationWarning, stacklevel=3)
530530

531531

532+
def _soft_deprecated(msg):
533+
# only emit SoftDeprecationWarning in the Python Development Mode
534+
# and if Python is built in debug mode
535+
if sys.flags.dev_mode or hasattr(sys, 'gettotalrefcount'):
536+
warn(msg, SoftDeprecationWarning, stacklevel=3)
537+
538+
532539
# Private utility function called by _PyErr_WarnUnawaitedCoroutine
533540
def _warn_unawaited_coroutine(coro):
534541
msg_lines = [
@@ -587,8 +594,11 @@ def _filters_mutated():
587594
if not hasattr(sys, 'gettotalrefcount'):
588595
filterwarnings("default", category=DeprecationWarning,
589596
module="__main__", append=1)
597+
filterwarnings("default", category=SoftDeprecationWarning,
598+
module="__main__", append=1)
590599
simplefilter("ignore", category=DeprecationWarning, append=1)
591600
simplefilter("ignore", category=PendingDeprecationWarning, append=1)
601+
simplefilter("ignore", category=SoftDeprecationWarning, append=1)
592602
simplefilter("ignore", category=ImportWarning, append=1)
593603
simplefilter("ignore", category=ResourceWarning, append=1)
594604

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :exc:`PyExc_SoftDeprecationWarning` warning category to implement soft
2+
deprecation. The soft deprecation policy is described in :pep:`387`. Patch
3+
by Victor Stinner.

0 commit comments

Comments
 (0)