From 55408f86d78259f18c56c5e1ea51e0f8dcdbeb67 Mon Sep 17 00:00:00 2001 From: Grigoriev Semyon <33061489+grigoriev-semyon@users.noreply.github.com> Date: Sun, 16 Jul 2023 18:30:39 +0300 Subject: gh-105726: Add `__slots__` to `AbstractContextManager` and `AbstractAsyncContextManager` (#106771) Co-authored-by: Kumar Aditya --- Lib/contextlib.py | 4 ++++ Lib/test/test_contextlib.py | 10 ++++++++++ Lib/test/test_contextlib_async.py | 12 ++++++++++++ .../Library/2023-07-15-12-52-50.gh-issue-105726.NGthO8.rst | 3 +++ 4 files changed, 29 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-07-15-12-52-50.gh-issue-105726.NGthO8.rst diff --git a/Lib/contextlib.py b/Lib/contextlib.py index b5acbcb..95947ac 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -20,6 +20,8 @@ class AbstractContextManager(abc.ABC): __class_getitem__ = classmethod(GenericAlias) + __slots__ = () + def __enter__(self): """Return `self` upon entering the runtime context.""" return self @@ -42,6 +44,8 @@ class AbstractAsyncContextManager(abc.ABC): __class_getitem__ = classmethod(GenericAlias) + __slots__ = () + async def __aenter__(self): """Return `self` upon entering the runtime context.""" return self diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 0f8351a..ecc5a43 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -24,6 +24,16 @@ class TestAbstractContextManager(unittest.TestCase): manager = DefaultEnter() self.assertIs(manager.__enter__(), manager) + def test_slots(self): + class DefaultContextManager(AbstractContextManager): + __slots__ = () + + def __exit__(self, *args): + super().__exit__(*args) + + with self.assertRaises(AttributeError): + DefaultContextManager().var = 42 + def test_exit_is_abstract(self): class MissingExit(AbstractContextManager): pass diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index 3d43ed0..bb72ae7 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -38,6 +38,18 @@ class TestAbstractAsyncContextManager(unittest.TestCase): self.assertIs(manager, context) @_async_test + async def test_slots(self): + class DefaultAsyncContextManager(AbstractAsyncContextManager): + __slots__ = () + + async def __aexit__(self, *args): + await super().__aexit__(*args) + + with self.assertRaises(AttributeError): + manager = DefaultAsyncContextManager() + manager.var = 42 + + @_async_test async def test_async_gen_propagates_generator_exit(self): # A regression test for https://bugs.python.org/issue33786. diff --git a/Misc/NEWS.d/next/Library/2023-07-15-12-52-50.gh-issue-105726.NGthO8.rst b/Misc/NEWS.d/next/Library/2023-07-15-12-52-50.gh-issue-105726.NGthO8.rst new file mode 100644 index 0000000..434f932 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-15-12-52-50.gh-issue-105726.NGthO8.rst @@ -0,0 +1,3 @@ +Added ``__slots__`` to :class:`contextlib.AbstractContextManager` and :class:`contextlib.AbstractAsyncContextManager` +so that child classes can use ``__slots__``. + -- cgit v0.12