Skip to content

Commit 6f07424

Browse files
matthiaskrammgvanrossum
authored andcommitted
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. )
1 parent 2b79108 commit 6f07424

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

stdlib/2/os/path.pyi

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,28 @@ def isdir(path: _PathType) -> bool: ...
5555
def islink(path: _PathType) -> bool: ...
5656
def ismount(path: _PathType) -> bool: ...
5757

58-
def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ...
58+
# Make sure signatures are disjunct, and allow combinations of bytes and unicode.
59+
# (Since Python 2 allows that, too)
60+
# Note that e.g. os.path.join("a", "b", "c", "d", u"e") will still result in
61+
# a type error.
62+
@overload
63+
def join(__p1: bytes, *p: bytes) -> bytes: ...
64+
@overload
65+
def join(__p1: Text, *p: _PathType) -> Text: ...
66+
@overload
67+
def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ...
68+
@overload
69+
def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ...
70+
@overload
71+
def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ...
5972

6073
def normcase(path: AnyStr) -> AnyStr: ...
6174
def normpath(path: AnyStr) -> AnyStr: ...
6275
if sys.platform == 'win32':
6376
def realpath(path: AnyStr) -> AnyStr: ...
6477
else:
6578
def realpath(filename: AnyStr) -> AnyStr: ...
66-
def relpath(path: AnyStr, start: AnyStr = ...) -> AnyStr: ...
79+
def relpath(path: AnyStr, start: _PathType = ...) -> AnyStr: ...
6780

6881
def samefile(path1: _PathType, path2: _PathType) -> bool: ...
6982
def sameopenfile(fp1: int, fp2: int) -> bool: ...

0 commit comments

Comments
 (0)