Skip to content

Commit b3d9fcb

Browse files
committed
deepcopy to isolate changes
1 parent aabb480 commit b3d9fcb

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

taskbadger/mug.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import dataclasses
22
from contextlib import ContextDecorator
33
from contextvars import ContextVar
4+
from copy import deepcopy
45
from typing import Union
56

67
from taskbadger.internal import AuthenticatedClient
@@ -87,8 +88,8 @@ def __init__(self):
8788

8889
def __enter__(self):
8990
self.stack.append((self.context, self.tags))
90-
self.context = self.context.copy()
91-
self.tags = self.tags.copy()
91+
self.context = deepcopy(self.context)
92+
self.tags = deepcopy(self.tags)
9293
return self
9394

9495
def __exit__(self, *args):
@@ -116,17 +117,19 @@ def current(cls):
116117

117118
class Badger(metaclass=MugMeta):
118119
def __init__(self, settings_or_mug=None):
120+
self._session = ReentrantSession()
121+
self._scope = Scope()
122+
119123
if isinstance(settings_or_mug, Badger):
120124
self.settings = settings_or_mug.settings
125+
self._scope.context = deepcopy(settings_or_mug._scope.context)
126+
self._scope.tags = deepcopy(settings_or_mug._scope.tags)
121127
else:
122128
self.settings = settings_or_mug
123129

124-
self._session = ReentrantSession()
125-
self._scope = Scope()
126-
127130
def bind(self, settings, tags=None):
128131
self.settings = settings
129-
self.scope().tags = tags or {}
132+
self._scope.tags = tags or {}
130133

131134
def session(self) -> ReentrantSession:
132135
return self._session

tests/test_session.py renamed to tests/test_mug.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@pytest.fixture(autouse=True)
1111
def _bind_settings():
12-
Badger.current.bind(Settings("https://taskbadger.net", "token", "org", "proj"))
12+
Badger.current.bind(Settings("https://taskbadger.net", "token", "org", "proj"), tags={"env": "test"})
1313

1414

1515
def test_session_singleton():
@@ -19,6 +19,8 @@ def test_session_singleton():
1919
assert session.stack == []
2020
assert session == Badger.current.session()
2121

22+
assert Badger.current.scope().tags == {"env": "test"}
23+
2224

2325
def test_session_global():
2426
session = Session()
@@ -90,3 +92,10 @@ def run(self):
9092
with session as client:
9193
assert client is not None
9294
self.clients.append(client)
95+
96+
assert Badger.current.scope().tags == {"env": "test"}
97+
with Badger.current.scope() as scope:
98+
scope.tag({"thread": self.name})
99+
assert scope.tags == {"env": "test", "thread": self.name}
100+
101+
assert Badger.current.scope().tags == {"env": "test"}

tests/test_scope.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ def test_scope_context():
2222
with scope:
2323
assert scope.stack == [({}, {})]
2424
scope.context["foo"] = "bar"
25+
scope.context["nit"] = [1]
2526
scope.tags["name"] = "value"
2627
with scope:
27-
assert scope.stack == [({}, {}), ({"foo": "bar"}, {"name": "value"})]
28-
assert scope.context == {"foo": "bar"}
28+
assert scope.stack == [({}, {}), ({"foo": "bar", "nit": [1]}, {"name": "value"})]
29+
assert scope.context == {"foo": "bar", "nit": [1]}
2930
assert scope.tags == {"name": "value"}
3031
scope.context["bar"] = "bazz"
32+
scope.context["nit"].append(2)
3133
scope.tags["bar"] = "bazz"
3234
with scope:
33-
assert scope.context == {"foo": "bar", "bar": "bazz"}
35+
assert scope.context == {"foo": "bar", "bar": "bazz", "nit": [1, 2]}
3436
assert scope.tags == {"name": "value", "bar": "bazz"}
3537
scope.context.clear()
3638
scope.tags.clear()
37-
assert scope.context == {"foo": "bar"}
39+
assert scope.context == {"foo": "bar", "nit": [1]}
3840
assert scope.tags == {"name": "value"}
3941
assert scope.stack == [({}, {})]
4042
assert scope.context == {}

0 commit comments

Comments
 (0)