__class_getitem__ = classmethod(GenericAlias)
+ __slots__ = ()
+
def __enter__(self):
"""Return `self` upon entering the runtime context."""
return self
__class_getitem__ = classmethod(GenericAlias)
+ __slots__ = ()
+
async def __aenter__(self):
"""Return `self` upon entering the runtime context."""
return self
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
async with manager as 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 def test_async_gen_propagates_generator_exit(self):
# A regression test for https://bugs.python.org/issue33786.
--- /dev/null
+Added ``__slots__`` to :class:`contextlib.AbstractContextManager` and :class:`contextlib.AbstractAsyncContextManager`
+so that child classes can use ``__slots__``.
+