Skip to content

Commit 15876f6

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 the type of the instance instead.
1 parent e9040a2 commit 15876f6

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

bioblend/_tests/TestGalaxyRateLimit.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ def test_context_manager_enables_and_closes_the_session(self):
237237
with gi as entered:
238238
assert entered is gi
239239
used_inside = gi.use_session
240-
assert gi.make_get_request(f"{gi.url}/libraries").status_code == 200
240+
# Accessing a GalaxyInstance-only attribute also checks that
241+
# entering the context manager preserves the subclass.
242+
assert entered.libraries.gi is entered
243+
assert entered.make_get_request(f"{gi.url}/libraries").status_code == 200
241244
assert used_inside is True
242245
assert gi.use_session is False
243246

bioblend/galaxyclient.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from types import TracebackType
1616
from typing import (
1717
Any,
18+
TypeVar,
1819
)
1920

2021
import requests
@@ -50,6 +51,9 @@
5051
# Number of connections kept in the pool when ``use_session`` is enabled.
5152
SESSION_POOL_MAXSIZE = 20
5253

54+
# Used to preserve the subclass when a client is used as a context manager.
55+
GalaxyClientT = TypeVar("GalaxyClientT", bound="GalaxyClient")
56+
5357

5458
class _RateLimitRetry(Retry):
5559
"""
@@ -295,7 +299,7 @@ def close(self) -> None:
295299
self._session.close()
296300
self._session = None
297301

298-
def __enter__(self) -> "GalaxyClient":
302+
def __enter__(self: GalaxyClientT) -> GalaxyClientT:
299303
self.use_session = True
300304
return self
301305

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)