This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add suffix property to zipfile.Path
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: MrQubo, jaraco, miguendes
Priority: normal Keywords: patch

Created on 2021-05-10 05:29 by MrQubo, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26129 merged miguendes, 2021-05-14 15:52
Messages (6)
msg393352 - (view) Author: Jakub Nowak (MrQubo) Date: 2021-05-10 05:29
suffix property is present on pathlib.Path (specifically pathlib.PurePath) and it could also be added to zipfile.Path for consistency.

My use case is filtering files by suffix:
    patch_files = list(filter(lambda file: file.suffix == '.patch', files))

Besides suffix also most of the other pathlib.PurePath properties and methods could be added.
msg393637 - (view) Author: Miguel Brito (miguendes) * Date: 2021-05-14 10:12
+1. 

The docstring says:
```
class Path:
    """
    A pathlib-compatible interface for zip files.
```
but only a few methods are supported. It'd be nice to have at least `stem`, `parents`, `suffixes`, `parts`, which IMHO would make sense here.

I'd love to work on this if the Core Devs think it makes sense and if OP is not doing so also.
msg393650 - (view) Author: Jakub Nowak (MrQubo) Date: 2021-05-14 12:31
Thank for your interest Miguel. I have no experience with CPython codebase so I'll be glad if you work on this.

I think treating ZipFile's like a separate drive would be the best. If we agree on this then probably all of the properties and methods from pathlib could be implemented. But without this at least `suffix`, `suffixes`, `stem`, `with_name()`, `with_stem()`, and `with_suffix()` makes perfect sense.

If some of the properties/methods won't be implemented on zipfile.Path then it would be useful to raise `NotImplementedError`. Currently, trying to use suffix property, raises the error `AttributeError: 'Path' object has no attribute 'suffix'`. This is confusing as the name of the class is the same as in `pathlib.Path` so it's not immediately obvious that it meant `zipfile.Path`.

The use-case for mimicking those two interfaces closely is to use both interchangeably (thanks to duck-typing).
msg393671 - (view) Author: Miguel Brito (miguendes) * Date: 2021-05-14 15:53
I prototyped something by adding `suffix`, `suffixes`, and `stem`. To me these are the most obvious ones and would be useful right away.

Regarding the others I'm not so sure. It seems that zipfile.Path is just a convenient class to provide basic navigational operations. It's not meant to be 100% consistent with pathlib.Path. 

In any case, it'd nice to discuss it.


Here it is: https://github.com/python/cpython/pull/26129
msg393682 - (view) Author: Jason R. Coombs (jaraco) * (Python committer) Date: 2021-05-14 17:55
Sounds good to me. In general, it's easier to contribute first to https://github.com/jaraco/zipp and those changes can be readily merged into CPython. No worries, though. If these changes can be accepted here, I should be able to graft them onto jaraco/zipp as well, and thus provide the functionality for older Pythons.

At this point, Python 3.10 is frozen for features, which this change would be, so I'm updating the target version to 3.11.

> It's not meant to be 100% consistent with pathlib.Path.

That's right. It's meant to provide whatever consistency makes sense, but a subset of what's available on pathlib.Path. Happy to accept any contributions that would be useful.

>> A pathlib-compatible interface
> only a few methods are supported

Indeed, perhaps the documentation should be more clear about what is and isn't supported.
msg393683 - (view) Author: Jason R. Coombs (jaraco) * (Python committer) Date: 2021-05-14 18:00
Thanks for the excellent patch. I've created https://github.com/jaraco/zipp/issues/74 to track the backport of these changes.
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88261
2021-05-14 18:00:17jaracosetstatus: open -> closed
resolution: fixed
messages: + msg393683

stage: patch review -> resolved
2021-05-14 17:55:48jaracosetmessages: + msg393682
versions: + Python 3.11, - Python 3.9
2021-05-14 15:53:06miguendessetmessages: + msg393671
2021-05-14 15:52:13miguendessetkeywords: + patch
stage: patch review
pull_requests: + pull_request24768
2021-05-14 12:31:37MrQubosetmessages: + msg393650
2021-05-14 10:12:07miguendessetnosy: + miguendes
messages: + msg393637
2021-05-10 07:35:47xtreaksetnosy: + jaraco
2021-05-10 05:29:23MrQubocreate