Skip to content

Commit 586d596

Browse files
committed
update context doc string and add type hint
1 parent 7214e47 commit 586d596

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/fundus/scraping/session.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
from contextlib import contextmanager
44
from dataclasses import dataclass
55
from queue import Empty, Queue
6-
from typing import Optional, Union
6+
from typing import Iterator, Optional, Union
77

88
import requests.adapters
9+
from typing_extensions import Self
910

1011
from fundus.logging import create_logger
1112
from fundus.utils.events import __EVENTS__
@@ -170,11 +171,26 @@ def close_current_session(self) -> None:
170171
self._session = None
171172

172173
@contextmanager
173-
def context(self, **kwargs):
174-
"""Context manager to temporarily overwrite session parameters.
174+
def context(self, **kwargs) -> Iterator[Self]:
175+
"""Thread-safe context manager for temporarily overriding session configuration.
176+
177+
This context manager temporarily replaces the current `CONFIG` instance with a new one
178+
constructed from the provided keyword arguments and clears the current session.
179+
Subsequent calls to `get_session()` will use the new configuration, while existing
180+
sessions remain unchanged. Internally we use a reentrant lock (RLock) to allow nested
181+
contexts within the same thread.
182+
183+
To prevent deadlocks, attempting to open a new context in another thread while already in
184+
a context raises an `AssertionError`.`
185+
186+
Args:
187+
**kwargs: Keyword arguments corresponding to `CONFIG` parameters.
175188
176189
Returns:
177-
SessionHandler: The session handler instance.
190+
SessionHandler: The current session handler instance with the temporary configuration.
191+
192+
Raises:
193+
AssertionError: If a context is already active in another thread.
178194
"""
179195

180196
if self._context_lock.acquire(blocking=False):

0 commit comments

Comments
 (0)