Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix shutil.make_archive stub #6868

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

Closed
wants to merge 1 commit into from

Conversation

jpy-git
Copy link
Contributor

@jpy-git jpy-git commented Jan 8, 2022

root_dir is only used to change into the source directory and therefore can accept bytes and PathLike[bytes] arguments.
(The base_dir arg of shutil.make_archive must still be a StrPath since it gets joined with base_name and format which must be strings)

Examples:

from pathlib import Path
import shutil
from typing import AnyStr, Generic


class DummyPathLike(Generic[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("some_folder").mkdir()
Path("some_folder/test.txt").touch()

shutil.make_archive(base_name="my_archive", format="gztar", root_dir="some_folder", base_dir=".")
shutil.make_archive(base_name="my_archive", format="gztar", root_dir=Path("some_folder"), base_dir=".")
shutil.make_archive(base_name="my_archive", format="gztar", root_dir=b"some_folder", base_dir=".")
shutil.make_archive(base_name="my_archive", format="gztar", root_dir=DummyPathLike(b"some_folder"), base_dir=".")

Path("some_folder/test.txt").unlink()
Path("some_folder").rmdir()
Path("my_archive.tar.gz").unlink()

@github-actions
Copy link
Contributor

github-actions bot commented Jan 8, 2022

According to mypy_primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

@JelleZijlstra
Copy link
Member

I'm a bit uneasy about this series of PRs figuring out exactly where in shutil bytes paths can go through; it feels like we're codifying implementation details that could easily change without warning. It may be worth asking the CPython developers who own shutil whether they think bytes paths should generally be accepted.

@AlexWaygood
Copy link
Member

Serhiy Storchaka has recently been doing some work on shutil (e.g. https://bugs.python.org/issue46245); he might be interested. Sending an email to python-dev is also an option.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jan 15, 2022

Oh oops, merged one of these PRs before seeing this discussion.

I'm still in favour of merging if this was discovered while actually type checking real world code. In general, typeshed is happy to reflect people's reliance on implementation details, since it reduces false positives. Either way, would be good to hear from CPython devs.

@JelleZijlstra
Copy link
Member

That makes sense in general, but the discussion on #6832 especially gives me pause. In that case, bytes paths only work in some specific circumstances where we happen to avoid the codepaths that require str paths. If the rest of the module is anything like that, allowing bytes paths is going to be a magnet for subtle bugs, and we'd be doing users a service by flagging it in the type checker.

@srittau
Copy link
Collaborator

srittau commented Feb 2, 2022

See also #6869, where unpack_archive() only accepts bytes paths in some circumstances.

@JelleZijlstra
Copy link
Member

I filed https://bugs.python.org/issue46727 to discuss this over at BPO.

@srittau
Copy link
Collaborator

srittau commented Feb 22, 2022

I am going to close this for now. While we usually prefer to follow the implementation, bytes paths working seems more of an accident and could stop working when the code is refactored or changed. We can revisit this if the bpo discussion turns out differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants