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.

Title: Path objects cannot be constructed from str subclasses
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Antony.Lee, pitrou, python-dev
Priority: normal Keywords: patch

Created on 2014-04-01 22:08 by Antony.Lee, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
pathlib.patch Antony.Lee, 2014-04-08 06:21 review
Messages (6)
msg215342 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-04-01 22:08
Trying to construct a Path object from a str subclass, e.g.

class S(str): pass

fails because the subclass cannot be interned.  I think that the interning should simply be removed for non-exactly-str arguments (it is only here for performance reasons, right?), or at least the error should be more explicit (note, in particular, that there is no error if one tries 'Path(S("foo/bar"))' instead, which only confuses the matter more).

In practice, I found out this via numpy, which provides its own str subclass, numpy.str_.
msg215741 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-04-08 06:21
The attached patch should fix the issue.
msg215907 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-04-10 20:08
I'm curious: what is the use case for str subclasses in Path objects?
If this is to be supported, I think I'd rather force-cast to str rather than accept arbitrary subclasses.
msg215936 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-04-11 17:41
I am loading some structure from a MATLAB binary file using  This structure contains (in particular) paths (written as bytestrings) to other files which end up being loaded as numpy.str_ objects.

In fact, just trying to store and retrieve strings from numpy arrays wraps them in numpy.str_:

>>> import numpy
>>> type(numpy.array(["foo"])[0])
<class 'numpy.str_'>

... and now trying to construct a Path from that will crash.

I agree, though, that force-casting str subclasses in the constructor may avoid other issues.
msg217038 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-04-22 22:34
New changeset c24cbd9bd63b by Antoine Pitrou in branch '3.4':
Issue #21127: Path objects can now be instantiated from str subclass instances (such as numpy.str_).

New changeset aad6d6b819ed by Antoine Pitrou in branch 'default':
Issue #21127: Path objects can now be instantiated from str subclass instances (such as numpy.str_).
msg217039 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-04-22 22:36
Ok, I've committed a patch (to 3.4 and 3.5) that force-casts to str.
Thank you for reporting this bug!
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65326
2014-04-22 22:36:11pitrousetstatus: open -> closed
versions: + Python 3.4
type: behavior
messages: + msg217039

resolution: fixed
stage: resolved
2014-04-22 22:34:57python-devsetnosy: + python-dev
messages: + msg217038
2014-04-11 17:41:53Antony.Leesetmessages: + msg215936
2014-04-10 20:08:11pitrousetmessages: + msg215907
2014-04-08 06:21:17Antony.Leesetfiles: + pathlib.patch
keywords: + patch
messages: + msg215741
2014-04-02 01:26:41ned.deilysetnosy: + pitrou
2014-04-01 22:08:28Antony.Leecreate