GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix shutil.make_archive stub by jpy-git · Pull Request #6868 · python/typeshed

Conversation

@jpy-git

@jpy-git jpy-git commented Jan 8, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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()

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jpy-git
Fix shutil.make_archive stub
17c6ed8
@github-actions

github-actions Bot commented Jan 8, 2022

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Jan 9, 2022

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Jan 9, 2022

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hauntsaninja

hauntsaninja commented Jan 15, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Jan 15, 2022

Copy link
Copy Markdown
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.

hauntsaninja reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@srittau

srittau commented Feb 2, 2022

Copy link
Copy Markdown
Collaborator

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Feb 12, 2022

Copy link
Copy Markdown
Member

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

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@srittau

srittau commented Feb 22, 2022

Copy link
Copy Markdown
Collaborator

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@srittau srittau closed this Feb 22, 2022
@JelleZijlstra JelleZijlstra mentioned this pull request Oct 5, 2023
Should shutil functions support bytes paths? python/cpython#90883
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@jpy-git @JelleZijlstra @AlexWaygood @hauntsaninja @srittau
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.