Skip to content

Commit d630c6f

Browse files
committed
overlapping/nested context managers on one instance are not supported
1 parent a526998 commit d630c6f

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎Changes.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
unreleased
2+
Add warning and safeguard that overlapping/nested context managers on one instance aren't supported
3+
14
v3.4.0 Mon Jun 09 2026
25
CLI tool extracted to separate `opencage-cli` package and repository (https://github.com/OpenCageData/opencage-cli)
36

‎opencage/geocoder.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ def __init__(
203203
self.user_agent_comment = user_agent_comment
204204

205205
def __enter__(self):
206+
"""Open a pooled requests session for sync geocoding.
207+
208+
Overlapping or nested ``with`` blocks on the same instance are
209+
not supported and will raise ``RuntimeError`` to prevent silently
210+
leaking the previous session's connection pool.
211+
"""
212+
if self.session is not None:
213+
raise RuntimeError(
214+
"OpenCageGeocode context already entered; "
215+
"overlapping `with` blocks on the same instance are not supported."
216+
)
206217
self.session = requests.Session()
207218
return self
208219

@@ -212,9 +223,20 @@ def __exit__(self, *args):
212223
return False
213224

214225
async def __aenter__(self):
226+
"""Open a pooled aiohttp session for async geocoding.
227+
228+
Overlapping or nested ``async with`` blocks on the same instance
229+
are not supported and will raise ``RuntimeError`` to prevent
230+
silently leaking the previous session's connection pool.
231+
"""
215232
if not AIOHTTP_AVAILABLE:
216233
raise AioHttpError("You must install `aiohttp` to use async methods")
217234

235+
if self.session is not None:
236+
raise RuntimeError(
237+
"OpenCageGeocode context already entered; "
238+
"overlapping `async with` blocks on the same instance are not supported."
239+
)
218240
self.session = aiohttp.ClientSession()
219241
return self
220242

0 commit comments

Comments
 (0)