A simple utility for synchronously executing async functions while preserving type information. The IDE correctly determines the return type thanks to TypeVar usage.
The Problem
With regular asyncio.run(), the IDE loses type information:
asyncdeffetch_book(id: int) -> BookModel:
...
# IDE doesn't know this is BookModel
book = asyncio.run(fetch_book(123))
The Solution
Using sync_await with proper typing via TypeVar:
book = sync_await(fetch_book(4325)) # ✅ IDE correctly infers type as BookModel
print(book.title) # Autocomplete works!
Usage
example.py
from sync_await import sync_await
classBookModel(BaseModel):
id: int
title: str
content: str
asyncdeffetch_book(id: int) -> BookModel:
...
if__name__=="__main__":
book = sync_await(fetch_book(4325)) # The IDE will correctly determine the type of the book variable