Skip to content

Commit

Permalink
Fixed type hints for sync_to_async()
Browse files Browse the repository at this point in the history
Use `@overload` to specify the two different sets of types for `sync_to_async()`. This prevents any errors in the example shown in #254.
  • Loading branch information
adamchainz authored May 22, 2021
1 parent 83d4823 commit d1ee83f
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions asgiref/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import warnings
import weakref
from concurrent.futures import Future, ThreadPoolExecutor
from typing import Any, Callable, Dict, Optional, Union
from typing import Any, Callable, Dict, Optional, overload

from .compatibility import get_running_loop
from .current_thread_executor import CurrentThreadExecutor
Expand Down Expand Up @@ -493,11 +493,29 @@ def get_current_task():
async_to_sync = AsyncToSync


@overload
def sync_to_async(
func: Optional[Callable[..., Any]] = None,
func: None = None,
thread_sensitive: bool = True,
executor: Optional["ThreadPoolExecutor"] = None,
) -> Union[SyncToAsync, Callable[[Callable[..., Any]], SyncToAsync]]:
) -> Callable[[Callable[..., Any]], SyncToAsync]:
...


@overload
def sync_to_async(
func: Callable[..., Any],
thread_sensitive: bool = True,
executor: Optional["ThreadPoolExecutor"] = None,
) -> SyncToAsync:
...


def sync_to_async(
func=None,
thread_sensitive=True,
executor=None,
):
if func is None:
return lambda f: SyncToAsync(
f,
Expand Down

0 comments on commit d1ee83f

Please sign in to comment.