diff --git a/newsfragments/167.fixed.rst b/newsfragments/167.fixed.rst new file mode 100644 index 0000000..738fc40 --- /dev/null +++ b/newsfragments/167.fixed.rst @@ -0,0 +1 @@ +Fixed a :class:`ReferenceError` when chaining a request off a temporary client, e.g. ``SpaceTrackClient(...).basicspacedata.gp(...)``. diff --git a/src/spacetrack/base.py b/src/spacetrack/base.py index 0106ec9..ab1589b 100644 --- a/src/spacetrack/base.py +++ b/src/spacetrack/base.py @@ -1090,9 +1090,7 @@ class _ControllerProxy: """Proxies request class methods with a preset request controller.""" def __init__(self, client, controller): - # The client will cache _ControllerProxy instances, so only store - # a weak reference to it. - self.client = weakref.proxy(client) + self.client = client self.controller = controller def __getattr__(self, attr): diff --git a/tests/test_spacetrack.py b/tests/test_spacetrack.py index a0c9bfa..fffb8c8 100644 --- a/tests/test_spacetrack.py +++ b/tests/test_spacetrack.py @@ -368,6 +368,16 @@ def test_controller_spacetrack_methods(client): assert mock_generic_request.call_args == expected +def test_controller_proxy_keeps_client_alive(httpx2_mock): + # A _ControllerProxy used to hold only a weak reference, so using a + # proxy after the client's last strong reference was dropped raised + # ReferenceError. + with patch.object(SpaceTrackClient, "generic_request") as mock_generic_request: + SpaceTrackClient("identity", "password").basicspacedata.gp() + + mock_generic_request.assert_called_once() + + def test_authenticate(httpx2_mock): def request_callback(request): if b"wrongpassword" in request.content: