diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-10-20 14:27:30 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-20 23:27:30 +0200 |
| commit | 64e83c711eb371d60fce64cae074c4d3311f6ece (patch) | |
| tree | e93024392d5a1a2376a407d8c5b03604d6e9f8b6 /Lib/test | |
| parent | b2a989995e6b725c5c957927127832fd3fcecfa2 (diff) | |
| download | cpython-git-64e83c711eb371d60fce64cae074c4d3311f6ece.tar.gz | |
bpo-45192: Fix a bug that infers the type of an os.PathLike[bytes] object 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 6270d3eeaf17b50abc4f8f4d97790d66179638e4)
Co-authored-by: Kyungmin Lee <rekyungmin@gmail.com>
Diffstat (limited to 'Lib/test')
| -rw-r--r-- | Lib/test/test_tempfile.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 96946a281a..2b0ec46a10 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -62,6 +62,25 @@ class TestLowLevelInternals(unittest.TestCase): def test_infer_return_type_pathlib(self): self.assertIs(str, tempfile._infer_return_type(pathlib.Path('/'))) + def test_infer_return_type_pathlike(self): + class Path: + def __init__(self, path): + self.path = path + + def __fspath__(self): + return self.path + + self.assertIs(str, tempfile._infer_return_type(Path('/'))) + self.assertIs(bytes, tempfile._infer_return_type(Path(b'/'))) + self.assertIs(str, tempfile._infer_return_type('', Path(''))) + self.assertIs(bytes, tempfile._infer_return_type(b'', Path(b''))) + self.assertIs(bytes, tempfile._infer_return_type(None, Path(b''))) + self.assertIs(str, tempfile._infer_return_type(None, Path(''))) + + with self.assertRaises(TypeError): + tempfile._infer_return_type('', Path(b'')) + with self.assertRaises(TypeError): + tempfile._infer_return_type(b'', Path('')) # Common functionality. |
