Skip to content

Commit a419b69

Browse files
JelleZijlstramatthiaskramm
authored andcommitted
make os.path identical in Python 2 and 3 (#1459)
Part of #1427. I don't think we can actually merge these until we merge os/__init__.pyi too, which will take a few more PRs.
1 parent 7ecc979 commit a419b69

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

stdlib/2/os/path.pyi

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ from typing import (
1010
)
1111

1212
_T = TypeVar('_T')
13-
_PathType = Union[bytes, Text]
13+
14+
if sys.version_info >= (3, 6):
15+
from builtins import _PathLike
16+
_PathType = Union[bytes, Text, _PathLike]
17+
else:
18+
_PathType = Union[bytes, Text]
1419

1520
# ----- os.path variables -----
1621
supports_unicode_filenames = False
@@ -55,20 +60,23 @@ def isdir(path: _PathType) -> bool: ...
5560
def islink(path: _PathType) -> bool: ...
5661
def ismount(path: _PathType) -> bool: ...
5762

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: ...
63+
if sys.version_info < (3, 0):
64+
# Make sure signatures are disjunct, and allow combinations of bytes and unicode.
65+
# (Since Python 2 allows that, too)
66+
# Note that e.g. os.path.join("a", "b", "c", "d", u"e") will still result in
67+
# a type error.
68+
@overload
69+
def join(__p1: bytes, *p: bytes) -> bytes: ...
70+
@overload
71+
def join(__p1: Text, *p: _PathType) -> Text: ...
72+
@overload
73+
def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ...
74+
@overload
75+
def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ...
76+
@overload
77+
def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ...
78+
else:
79+
def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ...
7280

7381
def normcase(path: AnyStr) -> AnyStr: ...
7482
def normpath(path: AnyStr) -> AnyStr: ...

stdlib/3/os/path.pyi

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,31 @@ def isdir(path: _PathType) -> bool: ...
6060
def islink(path: _PathType) -> bool: ...
6161
def ismount(path: _PathType) -> bool: ...
6262

63-
def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ...
63+
if sys.version_info < (3, 0):
64+
# Make sure signatures are disjunct, and allow combinations of bytes and unicode.
65+
# (Since Python 2 allows that, too)
66+
# Note that e.g. os.path.join("a", "b", "c", "d", u"e") will still result in
67+
# a type error.
68+
@overload
69+
def join(__p1: bytes, *p: bytes) -> bytes: ...
70+
@overload
71+
def join(__p1: Text, *p: _PathType) -> Text: ...
72+
@overload
73+
def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ...
74+
@overload
75+
def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ...
76+
@overload
77+
def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ...
78+
else:
79+
def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ...
6480

6581
def normcase(path: AnyStr) -> AnyStr: ...
6682
def normpath(path: AnyStr) -> AnyStr: ...
6783
if sys.platform == 'win32':
6884
def realpath(path: AnyStr) -> AnyStr: ...
6985
else:
7086
def realpath(filename: AnyStr) -> AnyStr: ...
71-
def relpath(path: AnyStr, start: AnyStr = ...) -> AnyStr: ...
87+
def relpath(path: AnyStr, start: _PathType = ...) -> AnyStr: ...
7288

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

0 commit comments

Comments
 (0)