Skip to content

shutil.move stub does not account for bytes paths #6831

Closed
@jpy-git

Description

@jpy-git

Just encountered this.

Consider this valid script:

import os
from pathlib import Path
import shutil
from typing import AnyStr


class DummyPathLike(os.PathLike[AnyStr]):
    """Since pathlib.Path can't accept bytes itself."""

    def __init__(self, path: AnyStr) -> None:
        self._path: AnyStr = path

    def __fspath__(self) -> AnyStr:
        """Return the file system path representation of the object."""
        return self._path


Path("test1.txt").touch()

# Both str
shutil.move("test1.txt", "test2.txt")

# Both bytes
shutil.move(b"test2.txt", b"test1.txt")

# Both Pathlike[str]
shutil.move(Path("test1.txt"), Path("test2.txt"))

# Both Pathlike[bytes]
# (N.B. pathlib.Path doesn't accept bytes so made dummy bytes pathlike for demo)
shutil.move(DummyPathLike(b"test2.txt"), DummyPathLike(b"test1.txt"))

# There's no constraints between the src and dst path types so no need for type vars.
shutil.move("test1.txt", DummyPathLike(b"test2.txt"))

Path("test2.txt").unlink()

mypy gives the following false positives:

type "bytes"; expected "Union[str, PathLike[str]]"
test.py:24: error: Argument 2 to "move" has incompatible type "bytes"; expected "Union[str, PathLike[str]]"
test.py:31: error: Argument 1 to "DummyPathLike" has incompatible type "bytes"; expected "str"
test.py:34: error: Argument 1 to "DummyPathLike" has incompatible type "bytes"; expected "str"
Found 4 errors in 1 file (checked 1 source file)

Should be a quick fix to switch StrPath with StrOrBytesPath in the stub so will pick this up now 👍

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