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

File tree

17 files changed

+71
-2
lines changed

17 files changed

+71
-2
lines changed

Doc/c-api/exceptions.rst

Lines changed: 6 additions & 0 deletions
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

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 :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

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

Lines changed: 2 additions & 1 deletion
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

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
]

0 commit comments

Comments
 (0)