Skip to content

Commit accddd2

Browse files
committed
Improve the connection reuse example in the documentation
The example made a single request, which is exactly the case where reusing a connection makes no difference. Make one request per history returned, and quote strings with `"` as in the rest of the documentation. Writing the example also showed that `__enter__()` was annotated as returning `GalaxyClient`, so the subclass was lost and accessing e.g. `gi.histories` on the result did not type check. Return `Self` instead, as done elsewhere in the package.
1 parent 0979d83 commit accddd2

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

bioblend/_tests/TestGalaxyRateLimit.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from typing import Any
1818

1919
import pytest
20+
from typing_extensions import Self
2021

2122
from bioblend import ConnectionError
2223
from bioblend.galaxy import GalaxyInstance
@@ -72,7 +73,7 @@ def log_message(self, format: str, *args: Any) -> None:
7273
def request_count(self) -> int:
7374
return len(self.requests)
7475

75-
def __enter__(self) -> "MockServer":
76+
def __enter__(self) -> Self:
7677
self._thread = threading.Thread(target=self._httpd.serve_forever, daemon=True)
7778
self._thread.start()
7879
return self
@@ -237,7 +238,10 @@ def test_context_manager_enables_and_closes_the_session(self):
237238
with gi as entered:
238239
assert entered is gi
239240
used_inside = gi.use_session
240-
assert gi.make_get_request(f"{gi.url}/libraries").status_code == 200
241+
# Accessing a GalaxyInstance-only attribute also checks that
242+
# entering the context manager preserves the subclass.
243+
assert entered.libraries.gi is entered
244+
assert entered.make_get_request(f"{gi.url}/libraries").status_code == 200
241245
assert used_inside is True
242246
assert gi.use_session is False
243247

bioblend/galaxyclient.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from requests_toolbelt import MultipartEncoder
2525
from tusclient.storage.filestorage import FileStorage
2626
from tusclient.uploader.uploader import Uploader
27+
from typing_extensions import Self
2728
from urllib3 import BaseHTTPResponse
2829
from urllib3.connectionpool import ConnectionPool
2930
from urllib3.util.retry import Retry
@@ -295,11 +296,11 @@ def close(self) -> None:
295296
self._session.close()
296297
self._session = None
297298

298-
def __enter__(self) -> "GalaxyClient":
299+
def __enter__(self) -> Self:
299300
self.use_session = True
300301
return self
301302

302-
def __exit__(self, *args: Any) -> None:
303+
def __exit__(self, *args: object) -> None:
303304
self.close()
304305

305306
@property

docs/index.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ToolShed) instance object::
6161

6262
from bioblend.galaxy import GalaxyInstance
6363

64-
gi = GalaxyInstance(url='https://usegalaxy.org', key='your_api_key')
64+
gi = GalaxyInstance(url="https://usegalaxy.org", key="your_api_key")
6565
# Give up after a total of 5 minutes of waiting.
6666
gi.max_total_retry_delay = 300.0
6767
# Never wait more than 1 minute before a single retry.
@@ -79,11 +79,13 @@ Reusing connections
7979

8080
By default, each request opens a new connection. Scripts making many requests
8181
can reuse a single session instead, which avoids re-establishing a connection
82-
every time::
82+
every time. In the following example, one connection is used for the initial
83+
request and for the one made for each history returned by it::
8384

84-
with GalaxyInstance(url='https://usegalaxy.org', key='your_api_key') as gi:
85+
with GalaxyInstance(url="https://usegalaxy.org", key="your_api_key") as gi:
8586
for history in gi.histories.get_histories():
86-
print(history['name'])
87+
datasets = gi.histories.show_history(history["id"], contents=True)
88+
print(history["name"], len(datasets))
8789

8890
Equivalently, ``gi.use_session = True`` enables it and ``gi.close()`` releases
8991
the connections. An instance with a session enabled should not be shared between

0 commit comments

Comments
 (0)