Skip to content

Commit

Permalink
blacked
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnnsrs committed Aug 23, 2024
1 parent bf407c4 commit 85561bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 47 deletions.
18 changes: 6 additions & 12 deletions koil/composition/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,17 @@ class Config:
class KoiledModel(BaseModel):
koil: PedanticKoil = Field(default_factory=PedanticKoil, exclude=True)

def __enter__(self: T) -> T:
...
def __enter__(self: T) -> T: ...

def enter(self: T) -> T:
...
def enter(self: T) -> T: ...

async def aenter(self: T) -> T:
...
async def aenter(self: T) -> T: ...

def exit(self: T):
...
def exit(self: T): ...

async def aexit(self: T):
...
async def aexit(self: T): ...

def __exit__(self, exc_type, exc_val, exc_tb) -> None:
...
def __exit__(self, exc_type, exc_val, exc_tb) -> None: ...

async def __aenter__(self: T) -> T:
return self
Expand Down
55 changes: 26 additions & 29 deletions koil/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from .utils import check_is_asyncfunc, check_is_asyncgen, check_is_syncgen



logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -349,19 +348,19 @@ class UnkoiledQt(Protocol):
returned: QtCore.Signal

def run(self, *args, **kwargs) -> KoilFuture:
""" Runs the function in the governing loop and returns a KoilFuture
"""Runs the function in the governing loop and returns a KoilFuture
This is useful if you want to cancel the function from the outside.
The function will be run in the governing loop and the result will be
send to the main thread via a QtSignal.
Args:
*args: The arguments for the function
**kwargs: The keyword arguments for the function
Returns:
KoilFuture: The future that can be cancelled
"""
...

Expand All @@ -374,73 +373,72 @@ class KoilQt(Protocol):
returned: QtCore.Signal

def run(self, *args, **kwargs) -> KoilFuture:
""" Runs the function in the governing loop and returns a KoilFuture
"""Runs the function in the governing loop and returns a KoilFuture
This is useful if you want to cancel the function from the outside.
The function will be run in the governing loop and the result will be
send to the main thread via a QtSignal.
Args:
*args: The arguments for the function
**kwargs: The keyword arguments for the function
Returns:
KoilFuture: The future that can be cancelled
"""
...



def unkoilqt(func, *args, **kwargs) -> UnkoiledQt:
""" Unkoils a function so that it can be run in the main thread
"""Unkoils a function so that it can be run in the main thread
Args:
func (Callable): The function to run in the main thread
"""

if not (check_is_asyncgen(func) or check_is_asyncfunc(func)):
raise TypeError(f"{func} is not an async function")

if check_is_asyncgen(func):
return async_generator_to_qt(func, *args, **kwargs)
return async_generator_to_qt(func, *args, **kwargs)

else:
return async_to_qt(func, *args, **kwargs)


def koilqt(func, *args, autoresolve=None, **kwargs) -> Callable[..., Awaitable[Any]]:
""" Converts a qt mainthread function to be run in the asyncio loop
"""Converts a qt mainthread function to be run in the asyncio loop
Args:
func (Callable): The function to run in the main thread (can also
be a generator)
Returns:
Callable[..., Awaitable[Any]]: The function that can be run in the
asyncio loop
"""

if check_is_asyncgen(func) or check_is_asyncfunc(func):
raise TypeError(f"{func} should NOT be a coroutine function. This is a decorator to convert a function to be callable form the asyncio loop")

raise TypeError(
f"{func} should NOT be a coroutine function. This is a decorator to convert a function to be callable form the asyncio loop"
)

if check_is_syncgen(func):
if autoresolve is not None or autoresolve is True:
raise TypeError("Cannot autoresolve a generator")
return qtgenerator_to_async(func, *args, **kwargs)

else:
if autoresolve is None:
autoresolve = True
return qt_to_async(func, *args, autoresolve=autoresolve, **kwargs)




class WrappedObject(QtCore.QObject):
def __init__(self, *args, koil: Koil = None, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -473,9 +471,8 @@ class Config:
arbitrary_types_allowed = True



def create_qt_koil(parent, auto_enter: bool=True) -> QtKoil:
koil = QtKoil(parent=parent)
def create_qt_koil(parent, auto_enter: bool = True) -> QtKoil:
koil = QtKoil(parent=parent)
if auto_enter:
koil.enter()
return koil
return koil
12 changes: 6 additions & 6 deletions koil/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@


# Will only be set in the worker process
output_queue_context: contextvars.ContextVar[
multiprocessing.Queue
] = contextvars.ContextVar("out_queue_context")
input_queue_context: contextvars.ContextVar[
multiprocessing.Queue
] = contextvars.ContextVar("input_queue_context")
output_queue_context: contextvars.ContextVar[multiprocessing.Queue] = (
contextvars.ContextVar("out_queue_context")
)
input_queue_context: contextvars.ContextVar[multiprocessing.Queue] = (
contextvars.ContextVar("input_queue_context")
)
in_process_context: contextvars.ContextVar[bool] = contextvars.ContextVar(
"in_process_context", default=False
)
Expand Down

0 comments on commit 85561bf

Please sign in to comment.