asyncssh: Return type of create_process is partially unknown

When type checking a program using create_process a warning will be shown that its return type is partially unknown.

This is because create_process (https://github.com/ronf/asyncssh/blob/develop/asyncssh/connection.py#L4057) has been annotated with the return type of SSHClientProcess. This error arises as SSHClientProcess is generic and create_process does not specify the string type.

There are likely three options to fix this:

  1. Set the return type to be AnyStr, which means that SSHClientProcess will have the same string type as the input parameter.
    -> SSHClientProcess[AnyStr]
    
  2. Set the return type to be str or bytes, but do not tie the type to the same type as input.
    -> SSHClientProcess[Union[str, bytes]]
    
  3. Use overloads to specify the different type depending on the encoding type. This is used by subprocess, so is probably the way to go.
        @overload
        @async_context_manager
        async def create_process(self, *args: object,
                                 input: Optional[str] = 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: Optional[bytes] = 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]: ...
    
        # pylint: disable=redefined-builtin
        @async_context_manager
        async def create_process(self, *args,
                                 input = None,
                                 stdin = PIPE,
                                 stdout = PIPE,
                                 stderr = PIPE,
                                 bufsize = io.DEFAULT_BUFFER_SIZE,
                                 send_eof = True, recv_eof = True,
                                 **kwargs):
    

Version information, but this is also currently an issue in develop.

$ python
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncssh
>>> asyncssh.__version__
'2.14.0'

Using VSCode and PyLance for type checking.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (11 by maintainers)

Most upvoted comments

I’m definitely not running with “–strict” here. In fact, to keep the error count manageable, I set “–allow-redefinition”. There are a number of places where AsyncSSH allows intentionally flexible typing, but then narrows the type as a function proceeds, and mypy doesn’t like redefinitions of that sort.