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.unpack_archive stub by jpy-git · Pull Request #6869 · python/typeshed

Conversation

@jpy-git

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

Copy link
Copy Markdown
Contributor

filename argument of shutil.unpack_archive can take bytes and PathLike[bytes] (python3.6 can only take bytes).

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.unpack_archive(
    filename="my_archive.tar.gz",
    extract_dir="extract_folder",
    format="gztar",
)
shutil.unpack_archive(
    filename=b"my_archive.tar.gz",
    extract_dir="extract_folder",
    format="gztar",
)
shutil.unpack_archive(
    filename=Path("my_archive.tar.gz"),
    extract_dir="extract_folder",
    format="gztar",
)
shutil.unpack_archive(
    filename=DummyPathLike(b"my_archive.tar.gz"),
    extract_dir="extract_folder",
    format="gztar",
)


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

I also checked the extract_dir argument but those still need to be strings so no change there:

# N.B. these versions below fail with TypeError: `Can't mix strings and bytes in path components`
shutil.unpack_archive(
    filename="my_archive.tar.gz",
    extract_dir=b"extract_folder",
    format="gztar",
)
shutil.unpack_archive(
    filename="my_archive.tar.gz",
    extract_dir=DummyPathLike(b"extract_folder"),
    format="gztar",
)

Sorry, something went wrong.

Uh oh!

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

All reactions
@jpy-git
Fix shutil.unpack_archive stub
bfb0431
@github-actions

github-actions Bot commented Jan 9, 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.

jpy-git
jpy-git commented Jan 9, 2022
View reviewed changes
Comment thread stdlib/shutil.pyi Resolved

Uh oh!

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

@srittau

srittau commented Feb 2, 2022

Copy link
Copy Markdown
Collaborator

That doesn't look right to me in the general case. For example:

>>> import shutil
>>> shutil.unpack_archive(b"foo.zip")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/shutil.py", line 1239, in unpack_archive
    format = _find_unpack_format(filename)
  File "/usr/lib/python3.9/shutil.py", line 1202, in _find_unpack_format
    if filename.endswith(extension):
TypeError: endswith first arg must be bytes or a tuple of bytes, not str
All reactions

Sorry, something went wrong.

Uh oh!

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

@srittau srittau mentioned this pull request Feb 2, 2022
Fix shutil.make_archive stub #6868
Closed
@srittau

srittau commented Feb 22, 2022

Copy link
Copy Markdown
Collaborator

Closing per my last comment.

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

@AlexWaygood AlexWaygood AlexWaygood left review comments

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.

3 participants

@jpy-git @srittau @AlexWaygood
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.