Skip to content

Commit

Permalink
initial generic version typing
Browse files Browse the repository at this point in the history
  • Loading branch information
spyoungtech committed Sep 29, 2023
1 parent 1fcce26 commit f63d062
Show file tree
Hide file tree
Showing 9 changed files with 394 additions and 241 deletions.
2 changes: 1 addition & 1 deletion ahk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'MsgBoxModality',
]

_global_instance: Optional[AHK] = None
_global_instance: Optional[AHK[None]] = None


def __getattr__(name: str) -> Any:
Expand Down
144 changes: 108 additions & 36 deletions ahk/_async/engine.py

Large diffs are not rendered by default.

148 changes: 74 additions & 74 deletions ahk/_async/transport.py

Large diffs are not rendered by default.

19 changes: 12 additions & 7 deletions ahk/_async/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Sequence
from typing import Tuple
from typing import TYPE_CHECKING
from typing import TypeVar
from typing import Union

from ahk.message import Position
Expand Down Expand Up @@ -45,10 +46,12 @@ class WindowNotFoundException(Exception):
'Use of the {0} property setter is not supported in the async API. Use the set_{0} instead.'
)

T_EngineVersion = TypeVar('T_EngineVersion', bound=Optional[Literal['v1', 'v2']])


class AsyncWindow:
def __init__(self, engine: AsyncAHK, ahk_id: str):
self._engine: AsyncAHK = engine
def __init__(self, engine: AsyncAHK[T_EngineVersion], ahk_id: str):
self._engine: AsyncAHK[T_EngineVersion] = engine
if not ahk_id:
raise ValueError(f'Invalid ahk_id: {ahk_id!r}')
self._ahk_id: str = ahk_id
Expand Down Expand Up @@ -381,10 +384,12 @@ async def get_position(self, *, blocking: Literal[False]) -> AsyncFutureResult[O
@overload
async def get_position(self, *, blocking: Literal[True]) -> Position: ...
@overload
async def get_position(self, *, blocking: bool = True) -> Union[Position, AsyncFutureResult[Optional[Position]]]: ...
async def get_position(self, *, blocking: bool = True) -> Union[Position, AsyncFutureResult[Optional[Position]], AsyncFutureResult[Position]]: ...
# fmt: on
async def get_position(self, *, blocking: bool = True) -> Union[Position, AsyncFutureResult[Optional[Position]]]:
resp = await self._engine.win_get_position(
async def get_position(
self, *, blocking: bool = True
) -> Union[Position, AsyncFutureResult[Optional[Position]], AsyncFutureResult[Position]]:
resp = await self._engine.win_get_position( # type: ignore[misc] # this appears to be a mypy bug
title=f'ahk_id {self._ahk_id}',
blocking=blocking,
detect_hidden_windows=True,
Expand Down Expand Up @@ -652,11 +657,11 @@ async def move(
)

@classmethod
async def from_pid(cls, engine: AsyncAHK, pid: int) -> Optional[AsyncWindow]:
async def from_pid(cls, engine: AsyncAHK[Any], pid: int) -> Optional[AsyncWindow]:
return await engine.win_get(title=f'ahk_pid {pid}')

@classmethod
async def from_mouse_position(cls, engine: AsyncAHK) -> Optional[AsyncWindow]:
async def from_mouse_position(cls, engine: AsyncAHK[Any]) -> Optional[AsyncWindow]:
return await engine.win_get_from_mouse_position()


Expand Down
144 changes: 108 additions & 36 deletions ahk/_sync/engine.py

Large diffs are not rendered by default.

147 changes: 72 additions & 75 deletions ahk/_sync/transport.py

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions ahk/_sync/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Sequence
from typing import Tuple
from typing import TYPE_CHECKING
from typing import TypeVar
from typing import Union

from ahk.message import Position
Expand Down Expand Up @@ -41,10 +42,12 @@ class WindowNotFoundException(Exception):
'Use of the {0} property setter is not supported in the async API. Use the set_{0} instead.'
)

T_EngineVersion = TypeVar('T_EngineVersion', bound=Optional[Literal['v1', 'v2']])


class Window:
def __init__(self, engine: AHK, ahk_id: str):
self._engine: AHK = engine
def __init__(self, engine: AHK[T_EngineVersion], ahk_id: str):
self._engine: AHK[T_EngineVersion] = engine
if not ahk_id:
raise ValueError(f'Invalid ahk_id: {ahk_id!r}')
self._ahk_id: str = ahk_id
Expand Down Expand Up @@ -289,7 +292,9 @@ def send(self, keys: str, control: str = '', *, blocking: Literal[True]) -> None
@overload
def send(self, keys: str, control: str = '', *, blocking: bool = True) -> Union[None, FutureResult[None]]: ...
# fmt: on
def send(self, keys: str, control: str = '', *, blocking: bool = True) -> Union[None, FutureResult[None]]:
def send(
self, keys: str, control: str = '', *, blocking: bool = True
) -> Union[None, FutureResult[None]]:
return self._engine.control_send(
keys=keys,
control=control,
Expand Down Expand Up @@ -358,10 +363,12 @@ def get_position(self, *, blocking: Literal[False]) -> FutureResult[Optional[Pos
@overload
def get_position(self, *, blocking: Literal[True]) -> Position: ...
@overload
def get_position(self, *, blocking: bool = True) -> Union[Position, FutureResult[Optional[Position]]]: ...
def get_position(self, *, blocking: bool = True) -> Union[Position, FutureResult[Optional[Position]], FutureResult[Position]]: ...
# fmt: on
def get_position(self, *, blocking: bool = True) -> Union[Position, FutureResult[Optional[Position]]]:
resp = self._engine.win_get_position(
def get_position(
self, *, blocking: bool = True
) -> Union[Position, FutureResult[Optional[Position]], FutureResult[Position]]:
resp = self._engine.win_get_position( # type: ignore[misc] # this appears to be a mypy bug
title=f'ahk_id {self._ahk_id}',
blocking=blocking,
detect_hidden_windows=True,
Expand Down Expand Up @@ -629,11 +636,11 @@ def move(
)

@classmethod
def from_pid(cls, engine: AHK, pid: int) -> Optional[Window]:
def from_pid(cls, engine: AHK[Any], pid: int) -> Optional[Window]:
return engine.win_get(title=f'ahk_pid {pid}')

@classmethod
def from_mouse_position(cls, engine: AHK) -> Optional[Window]:
def from_mouse_position(cls, engine: AHK[Any]) -> Optional[Window]:
return engine.win_get_from_mouse_position()


Expand Down
2 changes: 1 addition & 1 deletion ahk/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class _ExtensionEntry:
if typing.TYPE_CHECKING:
from ahk import AHK, AsyncAHK

TAHK = TypeVar('TAHK', bound=typing.Union[AHK, AsyncAHK])
TAHK = TypeVar('TAHK', bound=typing.Union[AHK[Any], AsyncAHK[Any]])


@dataclass
Expand Down
6 changes: 3 additions & 3 deletions ahk/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def __init_subclass__(cls: Type[T_ResponseMessageType], **kwargs: Any) -> None:
_message_registry[tom] = cls
super().__init_subclass__(**kwargs)

def __init__(self, raw_content: bytes, engine: Optional[Union[AsyncAHK, AHK]] = None):
def __init__(self, raw_content: bytes, engine: Optional[Union[AsyncAHK[Any], AHK[Any]]] = None):
self._raw_content: bytes = raw_content
self._engine: Optional[Union[AsyncAHK, AHK]] = engine
self._engine: Optional[Union[AsyncAHK[Any], AHK[Any]]] = engine

def __repr__(self) -> str:
return f'ResponseMessage<fqn={self.fqn()!r}>'
Expand All @@ -130,7 +130,7 @@ def _tom_lookup(tom: bytes) -> 'ResponseMessageClassTypes':

@classmethod
def from_bytes(
cls: Type[T_ResponseMessageType], b: bytes, engine: Optional[Union[AsyncAHK, AHK]] = None
cls: Type[T_ResponseMessageType], b: bytes, engine: Optional[Union[AsyncAHK[Any], AHK[Any]]] = None
) -> 'ResponseMessageTypes':
tom, _, message_bytes = b.split(b'\n', 2)
klass = cls._tom_lookup(tom)
Expand Down

0 comments on commit f63d062

Please sign in to comment.