Skip to content

bpo-45192: Fix a bug that infers the type of an os.PathLike[bytes] object as str - #28323

Merged
ambv merged 4 commits into
python:mainfrom
rekyungmin:bpo-45192
Oct 20, 2021
Merged

ambv merged 4 commits into
python:mainfrom
rekyungmin:bpo-45192

Conversation

@rekyungmin

@rekyungmin rekyungmin commented Sep 14, 2021 •

Copy link
Copy Markdown
Contributor

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:

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

This bug occurs because the tempfile._infer_return_type function considers all non-bytes objects as str.

_infer_return_type function should infer os.PathLike[str] object as str type and os.PathLike[bytes] object as bytes type.

https://bugs.python.org/issue45192

…ject as str

An object implementing the os.PathLike protocol can represent a file
system path as a str or bytes object.
Therefore, _infer_return_type function should infer os.PathLike[str]
object as str type and os.PathLike[bytes] object as bytes type.
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 18, 2021
@rekyungmin

Copy link
Copy Markdown
Contributor Author

I’m waiting for review.

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Oct 19, 2021
@ambv

ambv commented Oct 20, 2021

Copy link
Copy Markdown
Contributor

Closing and re-opening to re-trigger CI.

@ambv ambv closed this Oct 20, 2021
@ambv ambv reopened this Oct 20, 2021
@ambv
ambv merged commit 6270d3e into python:main Oct 20, 2021
@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @rekyungmin for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10.
🐍🍒⛏🤖

@bedevere-bot

Copy link
Copy Markdown

GH-29111 is a backport of this pull request to the 3.10 branch.

@bedevere-bot

Copy link
Copy Markdown

GH-29112 is a backport of this pull request to the 3.9 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 20, 2021
…ject as str (pythonGH-28323)

An object implementing the os.PathLike protocol can represent a file
system path as a str or bytes object.
Therefore, _infer_return_type function should infer os.PathLike[str]
object as str type and os.PathLike[bytes] object as bytes type.
(cherry picked from commit 6270d3e)

Co-authored-by: Kyungmin Lee <rekyungmin@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 20, 2021
…ject as str (pythonGH-28323)

An object implementing the os.PathLike protocol can represent a file
system path as a str or bytes object.
Therefore, _infer_return_type function should infer os.PathLike[str]
object as str type and os.PathLike[bytes] object as bytes type.
(cherry picked from commit 6270d3e)

Co-authored-by: Kyungmin Lee <rekyungmin@gmail.com>
ambv pushed a commit that referenced this pull request Oct 20, 2021
…ject as str (GH-28323) (GH-29112)

An object implementing the os.PathLike protocol can represent a file
system path as a str or bytes object.
Therefore, _infer_return_type function should infer os.PathLike[str]
object as str type and os.PathLike[bytes] object as bytes type.
(cherry picked from commit 6270d3e)

Co-authored-by: Kyungmin Lee <rekyungmin@gmail.com>
ambv pushed a commit that referenced this pull request Oct 20, 2021
…ject as str (GH-28323) (GH-29111)

An object implementing the os.PathLike protocol can represent a file
system path as a str or bytes object.
Therefore, _infer_return_type function should infer os.PathLike[str]
object as str type and os.PathLike[bytes] object as bytes type.
(cherry picked from commit 6270d3e)

Co-authored-by: Kyungmin Lee <rekyungmin@gmail.com>
shihai1991 added a commit to shihai1991/cpython that referenced this pull request Oct 21, 2021
* main: (263 commits)
  bpo-45521: Fix a bug in the obmalloc radix tree code. (pythonGH-29051)
  bpo-45522: Allow to disable freelists on build time (pythonGH-29056)
  bpo-34451: Document prompt and output toggle feature in html tutorial (pythonGH-27105)
  bpo-44019: Add operator.call() to __all__ for the operator module (pythonGH-29110)
  bpo-45315: PyType_FromSpec: Copy spec->name and have the type own the memory for its name (pythonGH-29103)
  bpo-44220: Export PyStructSequence_UnnamedField in the limited API (pythonGH-26331)
  bpo-44174: [Enum] add reference to name mangling (pythonGH-29116)
  bpo-45548: add some missing entries to `Modules/Setup` (pythonGH-29115)
  bpo-35673: Add a public alias for namespace package __loader__ attribute (python#29049)
  bpo-45192: Fix a bug that infers the type of an os.PathLike[bytes] object as str (pythonGH-28323)
  bpo-45527: Don't count cache hits, just misses. (pythonGH-29092)
  bpo-45320: Remove long-deprecated inspect methods (pythonGH-28618)
  bpo-41374: Remove obsolete exclusion of netinet/tcp.h on Cygwin (pythonGH-21649)
  bpo-45532: Replace 'default' with 'main' as default in sys.version (pythonGH-29100)
  bpo-45464: [doc] Explain that subclassing multiple exceptions is fragile (pythonGH-29094)
  Cleanup a couple of comments left on PR 28775 post-merge. (pythonGH-29079)
  bpo-45536: Check OpenSSL APIs in configure (pythonGH-29088)
  Add PEPs 593 & 647 to list of PEPs at top of typing docs (pythonGH-29097)
  Add a comment about how to fix bogus test_host_resolution_bad_address failures (python#29085)
  bpo-44525: Specialize simple Python calls. (pythonGH-29033)
  ...
@rekyungmin
rekyungmin deleted the bpo-45192 branch October 22, 2021 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

6 participants