From aa874326d86734606a1930e7f48e2ee204cae0b6 Mon Sep 17 00:00:00 2001
From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Date: Sat, 12 Nov 2022 12:47:53 +0530
Subject: [PATCH] GH-94597: add deprecation warnings for subclassing
 `AbstractChildWatcher` (#99386)

---
 Doc/library/asyncio-policy.rst                           | 3 +++
 Doc/whatsnew/3.12.rst                                    | 4 ++--
 Lib/asyncio/unix_events.py                               | 7 +++++++
 Lib/test/test_asyncio/test_unix_events.py                | 9 ++++++++-
 .../2022-11-11-18-23-41.gh-issue-94597.m6vMDK.rst        | 1 +
 5 files changed, 21 insertions(+), 3 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Library/2022-11-11-18-23-41.gh-issue-94597.m6vMDK.rst

diff --git a/Doc/library/asyncio-policy.rst b/Doc/library/asyncio-policy.rst
index 052378ef327..98c85015874 100644
--- a/Doc/library/asyncio-policy.rst
+++ b/Doc/library/asyncio-policy.rst
@@ -222,6 +222,9 @@ implementation used by the asyncio event loop:
       This method has to be called to ensure that underlying
       resources are cleaned-up.
 
+   .. deprecated:: 3.12
+
+
 .. class:: ThreadedChildWatcher
 
    This implementation starts a new waiting thread for every subprocess spawn.
diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index 37b9fb1ea4e..ead2a9e718a 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -203,8 +203,8 @@ asyncio
   (Contributed by Kumar Aditya in :gh:`98024`.)
 
 * The child watcher classes :class:`~asyncio.MultiLoopChildWatcher`,
-  :class:`~asyncio.FastChildWatcher` and
-  :class:`~asyncio.SafeChildWatcher` are deprecated and
+  :class:`~asyncio.FastChildWatcher`, :class:`~asyncio.AbstractChildWatcher`
+  and :class:`~asyncio.SafeChildWatcher` are deprecated and
   will be removed in Python 3.14. It is recommended to not manually
   configure a child watcher as the event loop now uses the best available
   child watcher for each platform (:class:`~asyncio.PidfdChildWatcher`
diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py
index 2de7a1bfd68..b21e0394141 100644
--- a/Lib/asyncio/unix_events.py
+++ b/Lib/asyncio/unix_events.py
@@ -846,6 +846,13 @@ class AbstractChildWatcher:
         waitpid(-1), there should be only one active object per process.
     """
 
+    def __init_subclass__(cls) -> None:
+        if cls.__module__ != __name__:
+            warnings._deprecated("AbstractChildWatcher",
+                             "{name!r} is deprecated as of Python 3.12 and will be "
+                             "removed in Python {remove}.",
+                              remove=(3, 14))
+
     def add_child_handler(self, pid, callback, *args):
         """Register a new child handler.
 
diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py
index d806ed497aa..93e8611f184 100644
--- a/Lib/test/test_asyncio/test_unix_events.py
+++ b/Lib/test/test_asyncio/test_unix_events.py
@@ -1108,6 +1108,11 @@ def test_write_eof_pending(self):
 
 class AbstractChildWatcherTests(unittest.TestCase):
 
+    def test_warns_on_subclassing(self):
+        with self.assertWarns(DeprecationWarning):
+            class MyWatcher(asyncio.AbstractChildWatcher):
+                pass
+
     def test_not_implemented(self):
         f = mock.Mock()
         watcher = asyncio.AbstractChildWatcher()
@@ -1747,7 +1752,9 @@ def f():
 
             self.assertIsInstance(policy.get_event_loop(),
                                   asyncio.AbstractEventLoop)
-            watcher = policy.get_child_watcher()
+            with warnings.catch_warnings():
+                warnings.simplefilter("ignore", DeprecationWarning)
+                watcher = policy.get_child_watcher()
 
             self.assertIsInstance(watcher, asyncio.SafeChildWatcher)
             self.assertIsNone(watcher._loop)
diff --git a/Misc/NEWS.d/next/Library/2022-11-11-18-23-41.gh-issue-94597.m6vMDK.rst b/Misc/NEWS.d/next/Library/2022-11-11-18-23-41.gh-issue-94597.m6vMDK.rst
new file mode 100644
index 00000000000..5e649a88fdd
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-11-11-18-23-41.gh-issue-94597.m6vMDK.rst
@@ -0,0 +1 @@
+Deprecate :class:`asyncio.AbstractChildWatcher` to be removed in Python 3.14. Patch by Kumar Aditya.
-- 
GitLab