Message416617
> Can the argument to `joinpath` contain path separators?
Good question. Currently, I'm aware of three concrete implementations of Traversable.joinpath:
- pathlib.Path.joinpath
- zipfile.Path.joinpath
- importlib.resources.simple.ResourceContainer.joinpath
``Traversable.joinpath`` was modeled after ``pathlib.Path.joinpath``, which itself is itself not rigorous in what path separators are allowed. The [docstring](https://github.com/python/cpython/blob/3faa9f78d4b9a8c0fd4657b434bdb08ae1f28800/Lib/pathlib.py#L754-L758) does indicate that each of the parameters will be combined with the existing path and acknowledges that absolute paths are possible. The [curated docs](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.joinpath) only give examples, and curiously, those examples only use posixpath.sep, suggesting that it's recommended when passing compound paths to use posixpath separators as they're more widely supported.
For zipfile.Path, I observe that [joinpath does accept path separators](https://github.com/python/cpython/blob/3faa9f78d4b9a8c0fd4657b434bdb08ae1f28800/Lib/zipfile.py#L2438-L2440) but looking at the [docs for zipfile](https://gist.github.com/jaraco/5b266870c62680d6d6b3a876be321fa4), it's not even obvious to me what path separators are used in zipfiles, but I do observe there is [coercion to posixpath.sep](https://gist.github.com/jaraco/5b266870c62680d6d6b3a876be321fa4).
``ResourceContainer``, on the other hand, [will not accept path separators](https://github.com/python/cpython/blob/3faa9f78d4b9a8c0fd4657b434bdb08ae1f28800/Lib/importlib/resources/simple.py#L102-L105). This class, however, may not be used anywhere and probably could be extended to support path separators as well.
Here's what I propose:
- First, document that the interface is under-specified and that for maximum compatibility, a consumer should pass only relative paths using posixpath.sep or no separator at all. Additionally, explicitly declare that behavior with absolute paths is undefined and unsupported.
- Second, expand the interface on ``Traversable.joinpath`` to stipulate that the interface should accept multiple relative paths.
- Third, ``simple.ResourceContainer`` should be updated to meet that spec. |
|
Date |
User |
Action |
Args |
2022-04-03 12:55:22 | jaraco | set | recipients:
+ jaraco, petr.viktorin, docs@python |
2022-04-03 12:55:22 | jaraco | set | messageid: <1648990522.53.0.0274909917678.issue47142@roundup.psfhosted.org> |
2022-04-03 12:55:22 | jaraco | link | issue47142 messages |
2022-04-03 12:55:22 | jaraco | create | |
|