Repository: getsentry/responses

Issue:
Setting registry in activate sets the default mock's registry for other users of activate
If the registry parameter is ever used in `activate`, it will "poison" the default registry for other tests. This is illustrated by the below reproducer:

```py
import responses, responses.registries

@responses.activate(registry=responses.registries.OrderedRegistry)
def test_something_else():
    pass

@responses.activate
def test_registry_type():
    assert type(responses.mock.get_registry()) == responses.registries.FirstMatchRegistry
```

Removing the first `activate` line will cause the test to pass. The order of uses does not matter. This occurs with and without pytest. This affects responses 0.20 and 0.21 (at least) on python 3.10.2.

Comments:

Patch:
diff --git a/CHANGES b/CHANGES
index bcd26b5..d088093 100644
--- a/CHANGES
+++ b/CHANGES
@@ -2,6 +2,7 @@
 ------
 
 * Add `passthrough` argument to `BaseResponse` object. See #557
+* Fix `registries` leak. See #563
 
 0.21.0
 ------
diff --git a/responses/__init__.py b/responses/__init__.py
index 30b9597..1a6bd2e 100644
--- a/responses/__init__.py
+++ b/responses/__init__.py
@@ -181,9 +181,6 @@ def get_wrapped(
         Wrapped function
 
     """
-    if registry is not None:
-        responses._set_registry(registry)
-
     assert_mock = std_mock.patch.object(
         target=responses,
         attribute="assert_all_requests_are_fired",
@@ -195,6 +192,9 @@ def get_wrapped(
         @wraps(func)
         async def wrapper(*args: Any, **kwargs: Any) -> Any:  # type: ignore[misc]
 
+            if registry is not None:
+                responses._set_registry(registry)
+
             with assert_mock, responses:
                 return await func(*args, **kwargs)
 
@@ -203,6 +203,9 @@ def get_wrapped(
         @wraps(func)
         def wrapper(*args: Any, **kwargs: Any) -> Any:  # type: ignore[misc]
 
+            if registry is not None:
+                responses._set_registry(registry)
+
             with assert_mock, responses:
                 # set 'assert_all_requests_are_fired' temporarily for a single run.
                 # Mock automatically unsets to avoid leakage to another decorated


Test patch:
diff --git a/responses/tests/test_registries.py b/responses/tests/test_registries.py
index 4dd5765..2ab035b 100644
--- a/responses/tests/test_registries.py
+++ b/responses/tests/test_registries.py
@@ -43,6 +43,44 @@ def test_set_registry():
     assert_reset()
 
 
+def test_set_registry_reversed():
+    """See https://github.com/getsentry/responses/issues/563"""
+
+    class CustomRegistry(registries.FirstMatchRegistry):
+        pass
+
+    @responses.activate
+    def run():
+        # test that registry does not leak to another test
+        assert type(responses.mock.get_registry()) == registries.FirstMatchRegistry
+
+    @responses.activate(registry=CustomRegistry)
+    def run_with_registry():
+        assert type(responses.mock.get_registry()) == CustomRegistry
+
+    run()
+    run_with_registry()
+    assert_reset()
+
+
+async def test_registry_async():
+    class CustomRegistry(registries.FirstMatchRegistry):
+        pass
+
+    @responses.activate
+    async def run():
+        # test that registry does not leak to another test
+        assert type(responses.mock.get_registry()) == registries.FirstMatchRegistry
+
+    @responses.activate(registry=CustomRegistry)
+    async def run_with_registry():
+        assert type(responses.mock.get_registry()) == CustomRegistry
+
+    await run()
+    await run_with_registry()
+    assert_reset()
+
+
 def test_set_registry_context_manager():
     def run():
         class CustomRegistry(registries.FirstMatchRegistry):