From d8af4afe5a448dd3d71c0259f286b8dc3cdf797f Mon Sep 17 00:00:00 2001 From: Piotr Maslanka <piotr.maslanka@ericsson.com> Date: Mon, 4 Mar 2024 13:07:01 +0100 Subject: [PATCH] Contexts are experimental Change-Id: I450ace29716a81eacb1b1c5b9326cada9ff516c4 --- CHANGELOG.md | 1 + satella/__init__.py | 2 +- satella/coding/environment.py | 4 ++++ .../test_coding}/test_environment.py | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) rename {satella/debug => tests/test_coding}/test_environment.py (73%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93d89b0f..81002101 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # v2.24.2 * added DeferredValue +* satella.coding.Context are considered experimental diff --git a/satella/__init__.py b/satella/__init__.py index 6a8e7b4d..6aaf86b1 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.24.2a2' +__version__ = '2.24.2a3' diff --git a/satella/coding/environment.py b/satella/coding/environment.py index a871ac4a..17e2dd9d 100644 --- a/satella/coding/environment.py +++ b/satella/coding/environment.py @@ -2,6 +2,7 @@ from __future__ import annotations import threading import typing as tp +import warnings from satella.coding.typing import V @@ -13,9 +14,12 @@ THEY_HATIN = object() class Context: """ New layer of environment. Can have it's own variables, or can hoist them onto the parent. + + .. warning:: This is considered experimental. I just haven't found out a good use case for it yet. """ def __init__(self, parent: tp.Optional[Context] = None, **variables): + warnings.warn('This is experimental', RuntimeWarning) self.parent = parent self.variables = variables self.bool = None diff --git a/satella/debug/test_environment.py b/tests/test_coding/test_environment.py similarity index 73% rename from satella/debug/test_environment.py rename to tests/test_coding/test_environment.py index 2b1e959a..9ecfaacd 100644 --- a/satella/debug/test_environment.py +++ b/tests/test_coding/test_environment.py @@ -1,3 +1,4 @@ +import threading import unittest from satella.coding.environment import Context @@ -12,6 +13,19 @@ class TestEnvs(unittest.TestCase): with Context() as new_ctxt: self.assertEqual(new_ctxt.value, 5) + def test_threads(self): + ctxt = Context.get() + ctxt.value = 2 + with ctxt: + def thread(): + with Context() as new_ctxt: + self.assertEqual(new_ctxt.value, 2) + new_ctxt.value2 = 2 + thr = threading.Thread(target=thread) + thr.start() + thr.join() + self.assertRaises(AttributeError, lambda: ctxt.value2) + def test_push(self): ctxt = Context.get() ctxt.value = 5 -- GitLab