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: The tempfile._infer_return_type function cannot infer the type of os.PathLike objects.
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk, lukasz.langa, miss-islington, rekyungmin
Priority: normal Keywords: patch

Created on 2021-09-14 05:44 by rekyungmin, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 28323 merged rekyungmin, 2021-09-14 06:05
PR 29111 merged miss-islington, 2021-10-20 19:54
PR 29112 merged miss-islington, 2021-10-20 19:54
Messages (6)
msg401754 - (view) Author: Kyungmin Lee (rekyungmin) * Date: 2021-09-14 05:44
The tempfile module has been updated to accept an object implementing os.PathLike protocol for path-related parameters as of Python 3.6 (e.g. dir parameter). An os.PathLike object represents a filesystem path as a str or bytes object (i.e. def __fspath__(self) -> Union[str, bytes]:). However, if an object implementing os.PathLike[bytes] is passed as a dir argument, a TypeError is raised. This bug occurs because the tempfile._infer_return_type function considers all objects other than bytes as str type.
msg401755 - (view) Author: Kyungmin Lee (rekyungmin) * Date: 2021-09-14 05:50
for example:

```
from typing import Union


class FakePath:
    def __init__(self, path):
        self.path = path

    def __fspath__(self) -> Union[str, bytes]:
        return self.path


if __name__ == "__main__":
    from tempfile import TemporaryDirectory

    # You need to create `existing_path` directory

    with TemporaryDirectory(dir=FakePath("existing_path")) as str_path:
        print(str_path)  # 'existing_path/...'

    with TemporaryDirectory(dir=FakePath(b"existing_path")) as byte_path:
        print(byte_path)  # expected b'existing_path/...' but raised TypeError

```
msg404139 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-10-17 14:02
This is a duplicate of #29447, so if this is merged, the other issue should also be closed as fixed.
msg404523 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-20 19:54
New changeset 6270d3eeaf17b50abc4f8f4d97790d66179638e4 by Kyungmin Lee in branch 'main':
bpo-45192: Fix a bug that infers the type of an os.PathLike[bytes] object as str (GH-28323)
https://github.com/python/cpython/commit/6270d3eeaf17b50abc4f8f4d97790d66179638e4
msg404528 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-20 21:25
New changeset d33fae7105aaea7c376b5455fd1f8de802ca2542 by Miss Islington (bot) in branch '3.9':
bpo-45192: Fix a bug that infers the type of an os.PathLike[bytes] object as str (GH-28323) (GH-29112)
https://github.com/python/cpython/commit/d33fae7105aaea7c376b5455fd1f8de802ca2542
msg404529 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-20 21:27
New changeset 64e83c711eb371d60fce64cae074c4d3311f6ece by Miss Islington (bot) in branch '3.10':
bpo-45192: Fix a bug that infers the type of an os.PathLike[bytes] object as str (GH-28323) (GH-29111)
https://github.com/python/cpython/commit/64e83c711eb371d60fce64cae074c4d3311f6ece
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89355
2021-10-20 21:27:38lukasz.langasetmessages: + msg404529
2021-10-20 21:25:19lukasz.langasetmessages: + msg404528
2021-10-20 19:54:56miss-islingtonsetpull_requests: + pull_request27379
2021-10-20 19:54:51miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request27378
2021-10-20 19:54:50lukasz.langasetnosy: + lukasz.langa
messages: + msg404523
2021-10-17 14:02:54andrei.avksetnosy: + andrei.avk
messages: + msg404139
2021-09-14 06:05:55rekyungminsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26732
2021-09-14 05:50:24rekyungminsetmessages: + msg401755
2021-09-14 05:44:04rekyungmincreate