From 6f074242462e04ea4f42557e95eaf3a429413e4a Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Wed, 31 May 2017 10:43:26 -0700 Subject: [PATCH] fix types in os.path (#1363) In Python 2, doing os.path.join(u"foo", "bar") is actually legal, and returns a unicode string. Also os.path.relpath always returns the type of its first argument. (The solution is not perfect -- e.g. os.path.join("a", "b", "c", "d", u"e") will still result in a type error. ) --- stdlib/2/os/path.pyi | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/stdlib/2/os/path.pyi b/stdlib/2/os/path.pyi index 56973e423..8f82bf246 100644 --- a/stdlib/2/os/path.pyi +++ b/stdlib/2/os/path.pyi @@ -55,7 +55,20 @@ def isdir(path: _PathType) -> bool: ... def islink(path: _PathType) -> bool: ... def ismount(path: _PathType) -> bool: ... -def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ... +# Make sure signatures are disjunct, and allow combinations of bytes and unicode. +# (Since Python 2 allows that, too) +# Note that e.g. os.path.join("a", "b", "c", "d", u"e") will still result in +# a type error. +@overload +def join(__p1: bytes, *p: bytes) -> bytes: ... +@overload +def join(__p1: Text, *p: _PathType) -> Text: ... +@overload +def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ... +@overload +def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ... +@overload +def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ... def normcase(path: AnyStr) -> AnyStr: ... def normpath(path: AnyStr) -> AnyStr: ... @@ -63,7 +76,7 @@ if sys.platform == 'win32': def realpath(path: AnyStr) -> AnyStr: ... else: def realpath(filename: AnyStr) -> AnyStr: ... -def relpath(path: AnyStr, start: AnyStr = ...) -> AnyStr: ... +def relpath(path: AnyStr, start: _PathType = ...) -> AnyStr: ... def samefile(path1: _PathType, path2: _PathType) -> bool: ... def sameopenfile(fp1: int, fp2: int) -> bool: ...