Skip to content

SFTPClient.realpath returns bytes in SFTPName.filename even when input is str #806

@vivodi

Description

@vivodi

There is a consistency discrepancy in asyncssh.SFTPClient.realpath when it returns an SFTPName object.

In most asyncssh SFTP APIs, the return type matches the input type: if a str or PurePath is provided, a str is returned; if bytes are provided, bytes are returned. However, when realpath() is called with check=FXRP_STAT_IF_EXISTS (or other flags that trigger an SFTPName return), the filename attribute of the resulting object is always bytes, regardless of the input type.

Reproduction Code

import asyncio
import asyncssh
from asyncssh import FXRP_STAT_IF_EXISTS

async def main():
    async with (
        asyncssh.connect('host', username='user', password='pw', known_hosts=None) as conn,
        conn.start_sftp_client() as sftp,
    ):
        path = await sftp.realpath('a')
        print(type(path)) # <class 'str'>

        sftp_name = await sftp.realpath('a', check=FXRP_STAT_IF_EXISTS)
        print(type(sftp_name.filename)) # <class 'bytes'>

asyncio.run(main())

Expected Behavior

If the input to realpath is a str, sftp_name.filename should be a str to maintain consistency with the rest of the library's SFTP file-handling logic.

Actual Behavior

sftp_name.filename is returned as bytes, requiring manual decoding even when the rest of the session uses string paths.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions