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: mailbox does not support new Path object
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: LimaAlphaHotel, ethan.furman, remi.lapeyre, thatiparthy
Priority: normal Keywords: patch

Created on 2020-06-18 16:45 by LimaAlphaHotel, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 20976 open LimaAlphaHotel, 2020-06-19 11:38
PR 20990 closed thatiparthy, 2020-06-19 16:57
Messages (6)
msg371823 - (view) Author: Laurence (LimaAlphaHotel) * Date: 2020-06-18 16:45
The mailbox library, in particular the Mailbox class I'm using, does not support the new Path object requiring a clumsy `mbx = Maildir(str(some_path_obj))` to use with a Path instance.

It currently blows up if passed a Path directly (does not support startswith) - perhaps a simple solution is to coerce whatever is passed into a string inside `__init__`?

Could this support be added?

I feel that strings representing paths should be discouraged as a general principal now we have a truly portable object to represent paths, and supporting Path in all places it makes logical sense (without breaking backwards compatibility, if implemented as I suggest with coercion by `str(...)` inside the module) in the core library seems like a good thing to me.
msg371825 - (view) Author: Laurence (LimaAlphaHotel) * Date: 2020-06-18 16:46
Sorry, should read "in particular the Maildir class I'm using"...

Same applies to mbox, however.
msg371829 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-06-18 17:19
Hi Laurence, Maildir predates pathlib so it's not surprising it hasn't been updated yet. Could you open a PR to add this?

BTW, you should use os.fspath() instead of str() here.
msg371853 - (view) Author: Laurence (LimaAlphaHotel) * Date: 2020-06-19 08:42
Hi Rémi,

I understand why it is the case, I just thought it would be a nice enhancement and quick win to add the support.

RE "you should use os.fspath() instead of str()": I'm following in the pathlib docuementation (https://docs.python.org/3/library/pathlib.html):

> The string representation of a path is the raw filesystem path itself (in native form, e.g. with backslashes under Windows), which you can pass to any function taking a file path as a string:
> >>>
> >>> p = PurePath('/etc')
> >>> str(p)
> '/etc'
> >>> p = PureWindowsPath('c:/Program Files')
> >>> str(p)
> 'c:\\Program Files'

Is the pathlib documentation wrong/out-of-date?  I Googled your suggestion of `os.fspath` and found <https://www.python.org/dev/peps/pep-0519/> which reads like the pathlib docs need correcting?

I'm trying to setup a build environment to create a PR for this issue as I type...

Thanks,

Laurence
msg371879 - (view) Author: Laurence (LimaAlphaHotel) * Date: 2020-06-19 11:48
I have patched the module to accept a path-like object, however it still follows the pre-pathlib ways of doing things elsewhere.  There's quite a bit of work to do to modernise the whole module, but this patch at least adds support to pass a Path to the classes it provides so external code can use pathlib and interface with it.
msg401350 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-09-08 00:54
The problem with using `str()` on a path argument is that the argument may not be a str, nor a pathlib object, but `str()` will still create a string out of it, leading to difficult bugs.

The documentation for pathlib also predates the addition of `os.fspath()`.
History
Date User Action Args
2022-04-11 14:59:32adminsetgithub: 85198
2021-09-08 00:54:18ethan.furmansetnosy: + ethan.furman
messages: + msg401350
2020-06-19 16:57:32thatiparthysetnosy: + thatiparthy
pull_requests: + pull_request20164
2020-06-19 11:48:37LimaAlphaHotelsetmessages: + msg371879
2020-06-19 11:38:07LimaAlphaHotelsetkeywords: + patch
stage: patch review
pull_requests: + pull_request20153
2020-06-19 08:42:43LimaAlphaHotelsetmessages: + msg371853
2020-06-18 17:19:51remi.lapeyresetnosy: + remi.lapeyre

messages: + msg371829
versions: + Python 3.10
2020-06-18 16:46:51LimaAlphaHotelsetmessages: + msg371825
2020-06-18 16:45:48LimaAlphaHotelcreate