From a5a22d3cea5838a947227d609e680248fa88fc96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <pmaslanka@smok.co> Date: Thu, 30 Sep 2021 20:27:42 +0200 Subject: [PATCH] added empty context manager --- CHANGELOG.md | 2 ++ docs/coding/ctxt_managers.rst | 5 +++++ docs/index.rst | 1 + satella/__init__.py | 2 +- satella/coding/__init__.py | 2 ++ satella/coding/ctxt_managers.py | 19 +++++++++++++++++++ tests/test_coding/test_debug.py | 6 +++++- 7 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 docs/coding/ctxt_managers.rst create mode 100644 satella/coding/ctxt_managers.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fcc2636..00a244bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,3 @@ # v2.17.22 + +* added empty context manager diff --git a/docs/coding/ctxt_managers.rst b/docs/coding/ctxt_managers.rst new file mode 100644 index 00000000..2bcd52fa --- /dev/null +++ b/docs/coding/ctxt_managers.rst @@ -0,0 +1,5 @@ +Context managers +================ + +.. autoclass:: satella.coding.EmptyContextManager + :members: diff --git a/docs/index.rst b/docs/index.rst index fe5dbf0c..601fa559 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -12,6 +12,7 @@ Visit the project's page at GitHub_! configuration/schema configuration/sources + coding/ctxt_managers coding/functions coding/futures coding/structures diff --git a/satella/__init__.py b/satella/__init__.py index 0583b476..b9ea9dfc 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.17.22a1' +__version__ = '2.17.22a2' diff --git a/satella/coding/__init__.py b/satella/coding/__init__.py index 1d58a54b..b8963fa4 100644 --- a/satella/coding/__init__.py +++ b/satella/coding/__init__.py @@ -2,6 +2,7 @@ Just useful objects to make your coding nicer every day """ +from .ctxt_managers import EmptyContextManager from .algos import merge_dicts from .concurrent import Monitor, RMonitor from .decorators import precondition, short_none, has_keys, \ @@ -23,6 +24,7 @@ from .expect_exception import expect_exception from .deep_compare import assert_equal, InequalityReason, Inequal __all__ = [ + 'EmptyContextManager', 'assert_equal', 'InequalityReason', 'Inequal', 'Closeable', 'contains', 'enum_value', 'reraise_as', 'expect_exception', diff --git a/satella/coding/ctxt_managers.py b/satella/coding/ctxt_managers.py new file mode 100644 index 00000000..cf2d20e3 --- /dev/null +++ b/satella/coding/ctxt_managers.py @@ -0,0 +1,19 @@ +class EmptyContextManager: + """ + A context manager that does nothing. Only to support conditional change of context managers, + eg in such a way: + + >>> if tx_enabled: + >>> ctxt = transaction.atomic + >>> else: + >>> ctxt = EmptyContextManager() + >>> with ctxt: + >>> ... + """ + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + return False + + diff --git a/tests/test_coding/test_debug.py b/tests/test_coding/test_debug.py index 23d40509..d9ce597e 100644 --- a/tests/test_coding/test_debug.py +++ b/tests/test_coding/test_debug.py @@ -1,13 +1,17 @@ import unittest from satella.coding import precondition, short_none, has_keys, update_if_not_none, postcondition, \ - get_arguments, expect_exception + get_arguments, expect_exception, EmptyContextManager from satella.coding.decorators import for_argument from satella.exceptions import PreconditionError class TestTypecheck(unittest.TestCase): + def test_empty_ctxt_manager(self): + with EmptyContextManager() as p: + pass + def test_except_exception(self): def expect_exception_1(): with expect_exception(KeyError, ValueError, 'Hello world!'): -- GitLab