[New-bugs-announce] [issue46727] Should shutil functions support bytes paths?

Jelle Zijlstra report at bugs.python.org
Fri Feb 11 20:43:24 EST 2022

New submission from Jelle Zijlstra <jelle.zijlstra at gmail.com>:

The shutil documentation doesn't say anything about bytes paths, and the CPython unit tests don't test them. But some functions do work with bytes paths in practice, and on typeshed we've received some requests to add support for them in the type stubs.

- https://github.com/python/typeshed/pull/7165/files (shutil.unpack_archive works with bytes paths, but only sometimes)
- https://github.com/python/typeshed/pull/6868 (shutil.make_archive)
- https://github.com/python/typeshed/pull/6832 (shutil.move accepts bytes paths, except when moving into an existing directory)

My overall impression is that bytes paths sometimes work by accident because they happen to not hit any code paths where we do os.path.join or string concatenation, but relying on them is risky because minor changes in the call site or in the file system can cause the call to break.

Here's three possible proposals:

(1) We document in the shutil docs that only str paths are officially supported. Bytes paths may sometimes work, but do it at your own risk.

(2) We add this documentation, but also make code changes to deprecate or even remove any support for bytes paths.

(3) We decide that bytes paths are officially supported, and we add tests for them and fix any cases where they don't work.

My preference is for (1). (2) feels like gratuitously breaking backward compatibility, and (3) is more work and there is little indication that bytes path support is a desired feature.

components: Library (Lib)
messages: 413113
nosy: AlexWaygood, Jelle Zijlstra, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Should shutil functions support bytes paths?
type: behavior
versions: Python 3.11

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list