-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Adjust shutil.move stubs to allow bytes/Pathlike[bytes] args #6832
Conversation
stdlib/shutil.pyi
Outdated
|
||
else: | ||
# See https://bugs.python.org/issue32689 | ||
def move(src: str, dst: StrPath, copy_function: _CopyFn = ...) -> _PathReturn: ... | ||
def move(src: str | bytes, dst: StrOrBytesPath, copy_function: _CopyFn = ...) -> _PathReturn: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI the linked bug applies to Pathlike[bytes]
as well, but I've verified it works fine with bytes
for Python < 3.9.
>>> import shutil
>>> shutil.move(b"test1.txt", b"test2.txt")
'test2.txt'
This comment has been minimized.
This comment has been minimized.
I don't think mixing str and bytes paths works. The source code has this:
which fails if it tries to join strings and bytes. |
This def works for me: >>> import shutil
>>> shutil.move("test.txt", b"test2.txt")
b'test2.txt'
>>> shutil.move(b"test2.txt", "test.txt")
'test.txt'
>>> shutil.move("some_folder", "some_folder2")
'some_folder2'
>>> shutil.move(b"some_folder2", b"some_folder")
b'some_folder'
>>> shutil.move(b"some_folder", "some_folder2")
'some_folder2'
>>> shutil.move("some_folder2", b"some_folder")
b'some_folder' EDIT: I noticed that bit in the source code only impacts directories, so I've added them above. They still seem to work though. |
The code I mentioned runs when |
Ahh it's only when In that case yes I get errors:
Feels like a bug given that it's only in this section of the code... |
The second of those issues seems like a bigger issue since this would also fail:
|
With those test cases, I think we should close this PR since bytes will always break the existing folder case. Do you happen to know where the best place flag this issue is for cpython? |
Edit: disregard my comment, I just pattern matched to a thing I'd seen before (shutil has historically not supported path objects well, in general worth double checking these kinds of changes against older Pythons) |
No worries, it looks like the types I'm working through the other In the meantime, maybe it's worth me removing my changes and just adding a comment above the |
That seems right to me. |
I've reverted the typing changes and added a comment linking to this PR to highlight for future contributors 👍 |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
|
These paths can flow into `shutil.move`, which does not accept byte paths or (int) file descriptors. See python/typeshed#6832
* Fix type hint for load_dotenv Fixes #431 * Quote type hints to avoid runtime errors in earlier Python versions * Revise type of dotenv_path parameter Based on PR feedback and typeshed's type hint for the built-in open() function: https://github.com/python/typeshed/blob/e2d67bf7034f68c07bd35150247e58e0817725d9/stdlib/builtins.pyi#L1421 * Allow only string paths, not byte paths These paths can flow into `shutil.move`, which does not accept byte paths or (int) file descriptors. See python/typeshed#6832 * Create a type alias for the paths this library accepts And use it consistently in main.py.
Closes #6831. Thorough example included in the issue.
TLDR:
shutil.move
can take bytes arguments