mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-105726: Add __slots__
to AbstractContextManager
and AbstractAsyncContextManager
(#106771)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
parent
cc25ca16ee
commit
55408f86d7
4 changed files with 29 additions and 0 deletions
|
@ -20,6 +20,8 @@ class AbstractContextManager(abc.ABC):
|
||||||
|
|
||||||
__class_getitem__ = classmethod(GenericAlias)
|
__class_getitem__ = classmethod(GenericAlias)
|
||||||
|
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
"""Return `self` upon entering the runtime context."""
|
"""Return `self` upon entering the runtime context."""
|
||||||
return self
|
return self
|
||||||
|
@ -42,6 +44,8 @@ class AbstractAsyncContextManager(abc.ABC):
|
||||||
|
|
||||||
__class_getitem__ = classmethod(GenericAlias)
|
__class_getitem__ = classmethod(GenericAlias)
|
||||||
|
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
async def __aenter__(self):
|
async def __aenter__(self):
|
||||||
"""Return `self` upon entering the runtime context."""
|
"""Return `self` upon entering the runtime context."""
|
||||||
return self
|
return self
|
||||||
|
|
|
@ -24,6 +24,16 @@ class TestAbstractContextManager(unittest.TestCase):
|
||||||
manager = DefaultEnter()
|
manager = DefaultEnter()
|
||||||
self.assertIs(manager.__enter__(), manager)
|
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):
|
def test_exit_is_abstract(self):
|
||||||
class MissingExit(AbstractContextManager):
|
class MissingExit(AbstractContextManager):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -37,6 +37,18 @@ class TestAbstractAsyncContextManager(unittest.TestCase):
|
||||||
async with manager as context:
|
async with manager as context:
|
||||||
self.assertIs(manager, context)
|
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_test
|
||||||
async def test_async_gen_propagates_generator_exit(self):
|
async def test_async_gen_propagates_generator_exit(self):
|
||||||
# A regression test for https://bugs.python.org/issue33786.
|
# A regression test for https://bugs.python.org/issue33786.
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Added ``__slots__`` to :class:`contextlib.AbstractContextManager` and :class:`contextlib.AbstractAsyncContextManager`
|
||||||
|
so that child classes can use ``__slots__``.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue