You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add/check os.PathLike support for the tempfile module's 'dir' arguments #73633
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2017-02-05.05:39:42.905>labels= ['type-feature', 'library', '3.9']
title="Add/check os.PathLike support for the tempfile module's 'dir' arguments"updated_at=<Date2020-07-18.03:39:52.122>user='https://github.com/brettcannon'
Without looking at the PR there are two ways to interpret my unfortunately vague comment. One is to say that the objects returned by tempfile code should support os.PathLike; that's what Serhiy is objecting to as those are objects representing concrete things on the file system instead of a general concept of a path (i.e. open() returns a concrete file while pathlib returns a path). You could potentially argue that TemporaryDirectory can implement os.PathLike since it does represent a path on the file system, but I see where Serhiy is coming from about how this can be viewed as inappropriate.
The other way to interpret what I said was to make sure things like the 'dir' argument accepted path-like objects.
What I probably meant back in February was the first interpretation, but after hearing Serhiy's argument, I agree that the second interpretation is best. (My apologies to svelankar if they implemented the first idea.)
changed the title [-]Add os.PathLike support to the tempfile module[/-][+]Add os.PathLike support to the tempfile module's 'dir' arguments[/+]on May 3, 2017
changed the title [-]Add os.PathLike support to the tempfile module's 'dir' arguments[/-][+]Add/check os.PathLike support for the tempfile module's 'dir' arguments[/+]on May 3, 2017
mlouielu commented on May 7, 2017
mlouielumannequin
Mannequin
More actions
Since tempfile is relay on os,
e.g. file = _os.join.path(dir, pre+name+suf),
it can directly accept dir as PathLike type, this should need to add test case for it.
mlouielu commented on May 7, 2017
mlouielumannequin
Mannequin
More actions
Regards my words, some place need to changed to support PathLike, I'll test it tomorrow.
mlouielu commented on May 8, 2017
mlouielumannequin
Mannequin
More actions
@brett, do you think if given a path-like dir, it should only be treated as str, or it could be str and bytes?
My PR is now treated path-like dir as str, not bytes.
I don't see any changes in tempfile.py. If the path-like protocol already is supported for the dir argument, no change in the documentation is needed.
tempfile.mkdtemp(dir=pathlike.Path(''), pre=b'', suf=b'') should raise a TypeError, but add also tests for path-like objects returning bytes path.
mlouielu commented on May 8, 2017
mlouielumannequin
Mannequin
More actions
Serhiy, though this no need to add versionchanged, should this need to explicit note in doc, that tempfile support os.PathLike?
also, I didn't get "add also tests for path-like objects returning bytes path", if tempfile.mkdtemp(dir=pathlike.Path(''), pre=b'', suf=b'') will raise TypeError, then we don't need to deal with path-like objects returning bytes. because tempfile._infer_return_type will treat path-like objects as str.
The key thing with the docs is that it doesn't say anywhere "takes a string path" or a "path as a string" or something else that suggests path-like objects don't work. If you want to clearly state that path-like objects are acceptable that is fine as well.
As for the bytes/str parts, path-like objects that return bytes should work, but only if everything is str or bytes as passed into the function (e.g. mixing the types should not be expected to work).
If tempfile doesn't have special code for supporting path-like objects, and nothing in the documentation points that path-like objects don't work, then the documentation doesn't need changes. This is just a consequence of implementing PEP-519 in low-level functions.
If tempfile._infer_return_type will treat *all* path-like objects as str, this is a bug.
It's actually implicitly implemented via os.path functions all supporting path-like objects and the assumption shas everything does. Regardless, I think the issue can be closed. Thanks for letting us know!
Activity
brettcannon commentedon Feb 5, 2017
The various classes in the tempfile module could implement os.PathLike.
serhiy-storchaka commentedon May 3, 2017
TemporaryDirectory and _TemporaryFileWrapper are *not* paths, as well as ordinal files are not paths. Adding __fspath__() to them looks wrong to me.
I think this isn't what Brett meant.
brettcannon commentedon May 3, 2017
Without looking at the PR there are two ways to interpret my unfortunately vague comment. One is to say that the objects returned by tempfile code should support os.PathLike; that's what Serhiy is objecting to as those are objects representing concrete things on the file system instead of a general concept of a path (i.e. open() returns a concrete file while pathlib returns a path). You could potentially argue that TemporaryDirectory can implement os.PathLike since it does represent a path on the file system, but I see where Serhiy is coming from about how this can be viewed as inappropriate.
The other way to interpret what I said was to make sure things like the 'dir' argument accepted path-like objects.
What I probably meant back in February was the first interpretation, but after hearing Serhiy's argument, I agree that the second interpretation is best. (My apologies to svelankar if they implemented the first idea.)
[-]Add os.PathLike support to the tempfile module[/-][+]Add os.PathLike support to the tempfile module's 'dir' arguments[/+][-]Add os.PathLike support to the tempfile module's 'dir' arguments[/-][+]Add/check os.PathLike support for the tempfile module's 'dir' arguments[/+]mlouielu commentedon May 7, 2017
Since tempfile is relay on
os,e.g.
file = _os.join.path(dir, pre+name+suf),it can directly accept dir as PathLike type, this should need to add test case for it.
mlouielu commentedon May 7, 2017
Regards my words, some place need to changed to support PathLike, I'll test it tomorrow.
mlouielu commentedon May 8, 2017
@brett, do you think if given a path-like dir, it should only be treated as
str, or it could bestrandbytes?My PR is now treated path-like dir as
str, notbytes.This will affect at this places:
Should it raise a TypeError (since we can't mix str and bytes), or it will convert path-like to bytes.
serhiy-storchaka commentedon May 8, 2017
I don't see any changes in tempfile.py. If the path-like protocol already is supported for the dir argument, no change in the documentation is needed.
tempfile.mkdtemp(dir=pathlike.Path(''), pre=b'', suf=b'') should raise a TypeError, but add also tests for path-like objects returning bytes path.
mlouielu commentedon May 8, 2017
Serhiy, though this no need to add versionchanged, should this need to explicit note in doc, that tempfile support os.PathLike?
also, I didn't get "add also tests for path-like objects returning bytes path", if
tempfile.mkdtemp(dir=pathlike.Path(''), pre=b'', suf=b'')will raise TypeError, then we don't need to deal with path-like objects returning bytes. because tempfile._infer_return_type will treat path-like objects as str.brettcannon commentedon May 8, 2017
The key thing with the docs is that it doesn't say anywhere "takes a string path" or a "path as a string" or something else that suggests path-like objects don't work. If you want to clearly state that path-like objects are acceptable that is fine as well.
As for the bytes/str parts, path-like objects that return bytes should work, but only if everything is str or bytes as passed into the function (e.g. mixing the types should not be expected to work).
serhiy-storchaka commentedon May 8, 2017
If tempfile doesn't have special code for supporting path-like objects, and nothing in the documentation points that path-like objects don't work, then the documentation doesn't need changes. This is just a consequence of implementing PEP-519 in low-level functions.
If tempfile._infer_return_type will treat *all* path-like objects as str, this is a bug.
csabella commentedon May 31, 2019
The pull request attached to this issue has been closed as the repository was marked as unknown.
This issue is now available for a new pull request.
mbyrnepr2 commentedon Jan 16, 2026
I think this issue is fixed via https://github.com/python/cpython/pull/28323/files if I'm not mistaken?
brettcannon commentedon Jan 16, 2026
It's actually implicitly implemented via
os.pathfunctions all supporting path-like objects and the assumption shas everything does. Regardless, I think the issue can be closed. Thanks for letting us know!