Skip to content

Commit ca9ff20

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 161012f commit ca9ff20

17 files changed

+71
-2
lines changed

Doc/c-api/exceptions.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,7 @@ the variables:
11661166
single: PyExc_PendingDeprecationWarning
11671167
single: PyExc_ResourceWarning
11681168
single: PyExc_RuntimeWarning
1169+
single: PyExc_SoftDeprecationWarning
11691170
single: PyExc_SyntaxWarning
11701171
single: PyExc_UnicodeWarning
11711172
single: PyExc_UserWarning
@@ -1189,6 +1190,8 @@ the variables:
11891190
+------------------------------------------+---------------------------------+----------+
11901191
| :c:data:`PyExc_RuntimeWarning` | :exc:`RuntimeWarning` | |
11911192
+------------------------------------------+---------------------------------+----------+
1193+
| :c:data:`PyExc_SoftDeprecationWarning` | :exc:`SoftDeprecationWarning` | |
1194+
+------------------------------------------+---------------------------------+----------+
11921195
| :c:data:`PyExc_SyntaxWarning` | :exc:`SyntaxWarning` | |
11931196
+------------------------------------------+---------------------------------+----------+
11941197
| :c:data:`PyExc_UnicodeWarning` | :exc:`UnicodeWarning` | |
@@ -1199,6 +1202,9 @@ the variables:
11991202
.. versionadded:: 3.2
12001203
:c:data:`PyExc_ResourceWarning`.
12011204
1205+
.. versionadded:: 3.13
1206+
:c:data:`PyExc_SoftDeprecationWarning`.
1207+
12021208
Notes:
12031209
12041210
.. [3]

Doc/data/stable_abi.dat

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/library/exceptions.rst

+13
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

+7
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 :c:data:`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

+1
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

+1
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

+7
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_pickle.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ def test_exceptions(self):
542542
RecursionError,
543543
EncodingWarning,
544544
BaseExceptionGroup,
545-
ExceptionGroup):
545+
ExceptionGroup,
546+
SoftDeprecationWarning):
546547
continue
547548
if exc is not OSError and issubclass(exc, OSError):
548549
self.assertEqual(reverse_mapping('builtins', name),

Lib/test/test_stable_abi_ctypes.py

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_warnings/__init__.py

+2
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

+10
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

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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :exc:`SoftDeprecationWarning` warning category to implement soft
2+
deprecation. The soft deprecation policy is described in :pep:`387`. Patch
3+
by Victor Stinner.

Misc/stable_abi.toml

+2
Original file line numberDiff line numberDiff line change
@@ -2432,3 +2432,5 @@
24322432
added = '3.13'
24332433
[function.PyWeakref_GetRef]
24342434
added = '3.13'
2435+
[data.PyExc_SoftDeprecationWarning]
2436+
added = '3.13'

Objects/exceptions.c

+8
Original file line numberDiff line numberDiff line change
@@ -3461,6 +3461,13 @@ SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
34613461
"in the future.");
34623462

34633463

3464+
/*
3465+
* SoftDeprecationWarning extends Warning
3466+
*/
3467+
SimpleExtendsException(PyExc_Warning, SoftDeprecationWarning,
3468+
"Base class for warnings about soft deprecated features.");
3469+
3470+
34643471
/*
34653472
* SyntaxWarning extends Warning
34663473
*/
@@ -3630,6 +3637,7 @@ static struct static_exception static_exceptions[] = {
36303637
ITEM(PendingDeprecationWarning),
36313638
ITEM(ResourceWarning),
36323639
ITEM(RuntimeWarning),
3640+
ITEM(SoftDeprecationWarning),
36333641
ITEM(SyntaxWarning),
36343642
ITEM(UnicodeWarning),
36353643
ITEM(UserWarning),

PC/python3dll.c

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/_warnings.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ init_filters(PyInterpreterState *interp)
9696
return PyList_New(0);
9797
#else
9898
/* Other builds ignore a number of warning categories by default */
99-
PyObject *filters = PyList_New(5);
99+
PyObject *filters = PyList_New(7);
100100
if (filters == NULL) {
101101
return NULL;
102102
}
@@ -106,8 +106,10 @@ init_filters(PyInterpreterState *interp)
106106
PyList_SET_ITEM(filters, pos++, \
107107
create_filter(TYPE, &_Py_ID(ACTION), MODNAME));
108108
ADD(PyExc_DeprecationWarning, default, "__main__");
109+
ADD(PyExc_SoftDeprecationWarning, default, "__main__");
109110
ADD(PyExc_DeprecationWarning, ignore, NULL);
110111
ADD(PyExc_PendingDeprecationWarning, ignore, NULL);
112+
ADD(PyExc_SoftDeprecationWarning, ignore, NULL);
111113
ADD(PyExc_ImportWarning, ignore, NULL);
112114
ADD(PyExc_ResourceWarning, ignore, NULL);
113115
#undef ADD

0 commit comments

Comments
 (0)