Skip to content

gh-106137: Add SoftDeprecationWarning category #106142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,7 @@ the variables:
single: PyExc_PendingDeprecationWarning
single: PyExc_ResourceWarning
single: PyExc_RuntimeWarning
single: PyExc_SoftDeprecationWarning
single: PyExc_SyntaxWarning
single: PyExc_UnicodeWarning
single: PyExc_UserWarning
Expand All @@ -1189,6 +1190,8 @@ the variables:
+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_RuntimeWarning` | :exc:`RuntimeWarning` | |
+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_SoftDeprecationWarning` | :exc:`SoftDeprecationWarning` | |
+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_SyntaxWarning` | :exc:`SyntaxWarning` | |
+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_UnicodeWarning` | :exc:`UnicodeWarning` | |
Expand All @@ -1199,6 +1202,9 @@ the variables:
.. versionadded:: 3.2
:c:data:`PyExc_ResourceWarning`.

.. versionadded:: 3.13
:c:data:`PyExc_SoftDeprecationWarning`.

Notes:

.. [3]
Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Doc/library/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,19 @@ The following exceptions are used as warning categories; see the
The deprecation policy is described in :pep:`387`.


.. exception:: SoftDeprecationWarning

Base class for warnings about soft deprecated features when those warnings
are intended for other Python developers.

Ignored by the default warning filters, except in the ``__main__`` module.
Enabling the :ref:`Python Development Mode <devmode>` shows this warning.

The soft deprecation policy is described in :pep:`387`.

.. versionadded:: 3.13


.. exception:: PendingDeprecationWarning

Base class for warnings about features which are obsolete and
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ New Features
Other Language Changes
======================

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


New Modules
Expand Down Expand Up @@ -447,6 +450,10 @@ New Features
If the assertion fails, make sure that the size is set before.
(Contributed by Victor Stinner in :gh:`106168`.)

* Add ``PyExc_SoftDeprecationWarning`` warning category to implement soft
deprecation. The soft deprecation policy is described in :pep:`387`.
(Contributed by Victor Stinner in :gh:`106137`.)

Porting to Python 3.13
----------------------

Expand Down
1 change: 1 addition & 0 deletions Include/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ PyAPI_DATA(PyObject *) PyExc_Warning;
PyAPI_DATA(PyObject *) PyExc_UserWarning;
PyAPI_DATA(PyObject *) PyExc_DeprecationWarning;
PyAPI_DATA(PyObject *) PyExc_PendingDeprecationWarning;
PyAPI_DATA(PyObject *) PyExc_SoftDeprecationWarning;
PyAPI_DATA(PyObject *) PyExc_SyntaxWarning;
PyAPI_DATA(PyObject *) PyExc_RuntimeWarning;
PyAPI_DATA(PyObject *) PyExc_FutureWarning;
Expand Down
1 change: 1 addition & 0 deletions Lib/test/exception_hierarchy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ BaseException
├── PendingDeprecationWarning
├── ResourceWarning
├── RuntimeWarning
├── SoftDeprecationWarning
├── SyntaxWarning
├── UnicodeWarning
└── UserWarning
7 changes: 7 additions & 0 deletions Lib/test/support/warnings_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def import_deprecated(name):
return importlib.import_module(name)


def import_soft_deprecated(name):
"""Import *name* while suppressing SoftDeprecationWarning."""
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=SoftDeprecationWarning)
return importlib.import_module(name)


def check_syntax_warning(testcase, statement, errtext='',
*, lineno=1, offset=None):
# Test also that a warning is emitted only once.
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,10 @@ def test_xdev(self):
else:
expected_filters = ("default::Warning "
"default::DeprecationWarning "
"default::SoftDeprecationWarning "
"ignore::DeprecationWarning "
"ignore::PendingDeprecationWarning "
"ignore::SoftDeprecationWarning "
"ignore::ImportWarning "
"ignore::ResourceWarning")

Expand Down Expand Up @@ -768,8 +770,10 @@ def test_warnings_filter_precedence(self):
if not support.Py_DEBUG:
expected_filters += (" "
"default::DeprecationWarning "
"default::SoftDeprecationWarning "
"ignore::DeprecationWarning "
"ignore::PendingDeprecationWarning "
"ignore::SoftDeprecationWarning "
"ignore::ImportWarning "
"ignore::ResourceWarning")

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ def test_exceptions(self):
RecursionError,
EncodingWarning,
BaseExceptionGroup,
ExceptionGroup):
ExceptionGroup,
SoftDeprecationWarning):
continue
if exc is not OSError and issubclass(exc, OSError):
self.assertEqual(reverse_mapping('builtins', name),
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,8 +1242,10 @@ def test_default_filter_configuration(self):
main_module_filter = "__main__"
expected_default_filters = [
('default', None, DeprecationWarning, main_module_filter, 0),
('default', None, SoftDeprecationWarning, main_module_filter, 0),
('ignore', None, DeprecationWarning, None, 0),
('ignore', None, PendingDeprecationWarning, None, 0),
('ignore', None, SoftDeprecationWarning, None, 0),
('ignore', None, ImportWarning, None, 0),
('ignore', None, ResourceWarning, None, 0),
]
Expand Down
10 changes: 10 additions & 0 deletions Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,13 @@ def _deprecated(name, message=_DEPRECATED_MSG, *, remove, _version=sys.version_i
warn(msg, DeprecationWarning, stacklevel=3)


def _soft_deprecated(msg):
# only emit SoftDeprecationWarning in the Python Development Mode
# and if Python is built in debug mode
if sys.flags.dev_mode or hasattr(sys, 'gettotalrefcount'):
warn(msg, SoftDeprecationWarning, stacklevel=3)


# Private utility function called by _PyErr_WarnUnawaitedCoroutine
def _warn_unawaited_coroutine(coro):
msg_lines = [
Expand Down Expand Up @@ -587,8 +594,11 @@ def _filters_mutated():
if not hasattr(sys, 'gettotalrefcount'):
filterwarnings("default", category=DeprecationWarning,
module="__main__", append=1)
filterwarnings("default", category=SoftDeprecationWarning,
module="__main__", append=1)
simplefilter("ignore", category=DeprecationWarning, append=1)
simplefilter("ignore", category=PendingDeprecationWarning, append=1)
simplefilter("ignore", category=SoftDeprecationWarning, append=1)
simplefilter("ignore", category=ImportWarning, append=1)
simplefilter("ignore", category=ResourceWarning, append=1)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add ``PyExc_SoftDeprecationWarning`` warning category to implement soft
deprecation. The soft deprecation policy is described in :pep:`387`. Patch
by Victor Stinner.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add :exc:`SoftDeprecationWarning` warning category to implement soft
deprecation. The soft deprecation policy is described in :pep:`387`. Patch
by Victor Stinner.
2 changes: 2 additions & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2432,3 +2432,5 @@
added = '3.13'
[function.PyWeakref_GetRef]
added = '3.13'
[data.PyExc_SoftDeprecationWarning]
added = '3.13'
8 changes: 8 additions & 0 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3461,6 +3461,13 @@ SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
"in the future.");


/*
* SoftDeprecationWarning extends Warning
*/
SimpleExtendsException(PyExc_Warning, SoftDeprecationWarning,
"Base class for warnings about soft deprecated features.");


/*
* SyntaxWarning extends Warning
*/
Expand Down Expand Up @@ -3630,6 +3637,7 @@ static struct static_exception static_exceptions[] = {
ITEM(PendingDeprecationWarning),
ITEM(ResourceWarning),
ITEM(RuntimeWarning),
ITEM(SoftDeprecationWarning),
ITEM(SyntaxWarning),
ITEM(UnicodeWarning),
ITEM(UserWarning),
Expand Down
1 change: 1 addition & 0 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ init_filters(PyInterpreterState *interp)
return PyList_New(0);
#else
/* Other builds ignore a number of warning categories by default */
PyObject *filters = PyList_New(5);
PyObject *filters = PyList_New(7);
if (filters == NULL) {
return NULL;
}
Expand All @@ -106,8 +106,10 @@ init_filters(PyInterpreterState *interp)
PyList_SET_ITEM(filters, pos++, \
create_filter(TYPE, &_Py_ID(ACTION), MODNAME));
ADD(PyExc_DeprecationWarning, default, "__main__");
ADD(PyExc_SoftDeprecationWarning, default, "__main__");
ADD(PyExc_DeprecationWarning, ignore, NULL);
ADD(PyExc_PendingDeprecationWarning, ignore, NULL);
ADD(PyExc_SoftDeprecationWarning, ignore, NULL);
ADD(PyExc_ImportWarning, ignore, NULL);
ADD(PyExc_ResourceWarning, ignore, NULL);
#undef ADD
Expand Down
3 changes: 3 additions & 0 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ Objects/exceptions.c - PyExc_UnicodeWarning -
Objects/exceptions.c - PyExc_BytesWarning -
Objects/exceptions.c - PyExc_ResourceWarning -
Objects/exceptions.c - PyExc_EncodingWarning -
Objects/exceptions.c - _PyExc_SoftDeprecationWarning -
Objects/exceptions.c - PyExc_SoftDeprecationWarning -


##-----------------------
## singletons
Expand Down