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 6270d3eeaf)

Co-authored-by: Kyungmin Lee <rekyungmin@gmail.com>
This commit is contained in:
Miss Islington (bot) 2021-10-20 14:27:30 -07:00 committed by GitHub
parent b2a989995e
commit 64e83c711e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 0 deletions

View file

@ -62,6 +62,25 @@ def test_infer_return_type_multiples_and_none(self):
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.