Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return type for SSHClientConnection.create_process() #608

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions asyncssh/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
from pathlib import Path
from types import TracebackType
from typing import TYPE_CHECKING, Any, AnyStr, Awaitable, Callable, Dict
from typing import List, Mapping, Optional, Sequence, Set, Tuple, Type
from typing import TypeVar, Union, cast
from typing import List, Mapping, Optional, overload, Sequence, Set
from typing import Tuple, Type, TypeVar, Union, cast
from typing_extensions import Protocol

from .agent import SSHAgentClient, SSHAgentListener
Expand Down Expand Up @@ -4052,6 +4052,54 @@ async def open_session(self, *args: object, **kwargs: object) -> \
return (SSHWriter(session, chan), SSHReader(session, chan),
SSHReader(session, chan, EXTENDED_DATA_STDERR))

@overload
@async_context_manager
async def create_process(self, *args: object,
input: None = None,
stdin: ProcessSource = PIPE,
stdout: ProcessTarget = PIPE,
stderr: ProcessTarget = PIPE,
bufsize: int = io.DEFAULT_BUFFER_SIZE,
send_eof: bool = True, recv_eof: bool = True,
encoding: str,
**kwargs: object) -> SSHClientProcess[str]: ...

@overload
@async_context_manager
async def create_process(self, *args: object,
input: str,
stdin: ProcessSource = PIPE,
stdout: ProcessTarget = PIPE,
stderr: ProcessTarget = PIPE,
bufsize: int = io.DEFAULT_BUFFER_SIZE,
send_eof: bool = True, recv_eof: bool = True,
encoding: str,
**kwargs: object) -> SSHClientProcess[str]: ...

@overload
@async_context_manager
async def create_process(self, *args: object,
input: None = None,
stdin: ProcessSource = PIPE,
stdout: ProcessTarget = PIPE,
stderr: ProcessTarget = PIPE,
bufsize: int = io.DEFAULT_BUFFER_SIZE,
send_eof: bool = True, recv_eof: bool = True,
encoding: None = None,
**kwargs: object) -> SSHClientProcess[bytes]: ...

@overload
@async_context_manager
async def create_process(self, *args: object,
input: bytes,
stdin: ProcessSource = PIPE,
stdout: ProcessTarget = PIPE,
stderr: ProcessTarget = PIPE,
bufsize: int = io.DEFAULT_BUFFER_SIZE,
send_eof: bool = True, recv_eof: bool = True,
encoding: None = None,
**kwargs: object) -> SSHClientProcess[bytes]: ...

# pylint: disable=redefined-builtin
@async_context_manager
async def create_process(self, *args: object,
Expand All @@ -4061,6 +4109,7 @@ async def create_process(self, *args: object,
stderr: ProcessTarget = PIPE,
bufsize: int = io.DEFAULT_BUFFER_SIZE,
send_eof: bool = True, recv_eof: bool = True,
encoding: Optional[str] = None,
**kwargs: object) -> SSHClientProcess[AnyStr]:
"""Create a process on the remote system

Expand Down Expand Up @@ -4127,7 +4176,7 @@ async def create_process(self, *args: object,
"""

chan, process = await self.create_session(
SSHClientProcess, *args, **kwargs) # type: ignore
SSHClientProcess, *args, encoding=encoding, **kwargs) # type: ignore

new_stdin: Optional[ProcessSource] = stdin
process: SSHClientProcess
Expand Down
12 changes: 5 additions & 7 deletions asyncssh/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
from random import SystemRandom
from types import TracebackType
from typing import Any, AsyncContextManager, Awaitable, Callable, Dict
from typing import Generator, Generic, IO, Mapping, Optional, Sequence
from typing import Tuple, Type, TypeVar, Union, cast, overload
from typing import Generator, Generic, IO, Mapping, Optional, ParamSpec
from typing import Sequence, Tuple, Type, TypeVar, Union, cast, overload
from typing_extensions import Literal, Protocol

from .constants import DEFAULT_LANG
Expand Down Expand Up @@ -287,11 +287,9 @@ async def __aexit__(self, exc_type: Optional[Type[BaseException]],

return exit_result

_ACMParams = ParamSpec('_ACMParams')

_ACMCoro = Callable[..., Awaitable[_ACM]]
_ACMWrapperFunc = Callable[..., _ACMWrapper[_ACM]]

def async_context_manager(coro: _ACMCoro[_ACM]) -> _ACMWrapperFunc[_ACM]:
def async_context_manager(coro: Callable[_ACMParams, Awaitable[_ACM]]) -> Callable[_ACMParams, _ACMWrapper[_ACM]]:
"""Decorator for functions returning asynchronous context managers

This decorator can be used on functions which return objects
Expand All @@ -305,7 +303,7 @@ def async_context_manager(coro: _ACMCoro[_ACM]) -> _ACMWrapperFunc[_ACM]:
"""

@functools.wraps(coro)
def context_wrapper(*args, **kwargs) -> _ACMWrapper[_ACM]:
def context_wrapper(*args: _ACMParams.args, **kwargs: _ACMParams.kwargs) -> _ACMWrapper[_ACM]:
"""Return an async context manager wrapper for this coroutine"""

return _ACMWrapper(coro(*args, **kwargs))
Expand Down