New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add/check os.PathLike support for the tempfile module's 'dir' arguments #73633
Comments
The various classes in the tempfile module could implement os.PathLike. |
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. |
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.) |
Since tempfile is relay on e.g. it can directly accept dir as PathLike type, this should need to add test case for it. |
Regards my words, some place need to changed to support PathLike, I'll test it tomorrow. |
@brett, do you think if given a path-like dir, it should only be treated as My PR is now treated path-like dir as 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. |
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. |
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 |
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. |
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. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: