-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mypy type-checking, recode to comply (#193)
- Loading branch information
Showing
8 changed files
with
247 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,64 @@ | ||
import asyncio | ||
from concurrent.futures import Future | ||
from threading import Thread | ||
from typing import Coroutine, Dict, List, Optional | ||
from types import TracebackType | ||
from typing import Any, Awaitable, Dict, Optional, Type, Union | ||
|
||
from .duckduckgo_search_async import AsyncDDGS | ||
|
||
# Create an event loop and run it in a separate thread. | ||
_SHARED_LOOP = asyncio.new_event_loop() | ||
_SHARED_THREAD = Thread(target=_SHARED_LOOP.run_forever, daemon=True) | ||
_SHARED_LOOP: asyncio.AbstractEventLoop = asyncio.new_event_loop() | ||
_SHARED_THREAD: Thread = Thread(target=_SHARED_LOOP.run_forever, daemon=True) | ||
_SHARED_THREAD.start() | ||
|
||
|
||
class DDGS(AsyncDDGS): | ||
def __init__(self, headers=None, proxies=None, timeout=10) -> None: | ||
super().__init__(headers, proxies, timeout) | ||
def __init__( | ||
self, | ||
headers: Optional[Dict[str, str]] = None, | ||
proxies: Union[Dict[str, str], str, None] = None, | ||
timeout: Optional[int] = 10, | ||
) -> None: | ||
super().__init__(headers=headers, proxies=proxies, timeout=timeout) | ||
self._loop = _SHARED_LOOP | ||
|
||
def __enter__(self): | ||
def __enter__(self) -> "DDGS": | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
pass | ||
def __exit__( | ||
self, | ||
exc_type: Optional[Type[BaseException]], | ||
exc_val: Optional[BaseException], | ||
exc_tb: Optional[TracebackType], | ||
) -> bool: | ||
return True | ||
|
||
def _run_async_in_thread(self, coro: Coroutine) -> Optional[List[Dict[str, Optional[str]]]]: | ||
def _run_async_in_thread(self, coro: Awaitable[Any]) -> Any: | ||
"""Runs an async coroutine in a separate thread.""" | ||
future = asyncio.run_coroutine_threadsafe(coro, self._loop) | ||
return future.result() | ||
future: Future[Any] = asyncio.run_coroutine_threadsafe(coro, self._loop) | ||
result = future.result() | ||
return result | ||
|
||
def text(self, *args, **kwargs) -> Optional[List[Dict[str, Optional[str]]]]: | ||
def text(self, *args: Any, **kwargs: Any) -> Any: | ||
return self._run_async_in_thread(super().text(*args, **kwargs)) | ||
|
||
def images(self, *args, **kwargs) -> Optional[List[Dict[str, Optional[str]]]]: | ||
def images(self, *args: Any, **kwargs: Any) -> Any: | ||
return self._run_async_in_thread(super().images(*args, **kwargs)) | ||
|
||
def videos(self, *args, **kwargs) -> Optional[List[Dict[str, Optional[str]]]]: | ||
def videos(self, *args: Any, **kwargs: Any) -> Any: | ||
return self._run_async_in_thread(super().videos(*args, **kwargs)) | ||
|
||
def news(self, *args, **kwargs) -> Optional[List[Dict[str, Optional[str]]]]: | ||
def news(self, *args: Any, **kwargs: Any) -> Any: | ||
return self._run_async_in_thread(super().news(*args, **kwargs)) | ||
|
||
def answers(self, *args, **kwargs) -> Optional[List[Dict[str, Optional[str]]]]: | ||
def answers(self, *args: Any, **kwargs: Any) -> Any: | ||
return self._run_async_in_thread(super().answers(*args, **kwargs)) | ||
|
||
def suggestions(self, *args, **kwargs) -> Optional[List[Dict[str, Optional[str]]]]: | ||
def suggestions(self, *args: Any, **kwargs: Any) -> Any: | ||
return self._run_async_in_thread(super().suggestions(*args, **kwargs)) | ||
|
||
def maps(self, *args, **kwargs) -> Optional[List[Dict[str, Optional[str]]]]: | ||
def maps(self, *args: Any, **kwargs: Any) -> Any: | ||
return self._run_async_in_thread(super().maps(*args, **kwargs)) | ||
|
||
def translate(self, *args, **kwargs) -> Optional[List[Dict[str, Optional[str]]]]: | ||
def translate(self, *args: Any, **kwargs: Any) -> Any: | ||
return self._run_async_in_thread(super().translate(*args, **kwargs)) |
Oops, something went wrong.