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.
This commit is contained in:
Jelle Zijlstra
2017-07-04 19:27:27 -07:00
committed by Matthias Kramm
parent 7ecc979211
commit a419b696d4
2 changed files with 41 additions and 17 deletions

View File

@@ -10,7 +10,12 @@ from typing import (
)
_T = TypeVar('_T')
_PathType = Union[bytes, Text]
if sys.version_info >= (3, 6):
from builtins import _PathLike
_PathType = Union[bytes, Text, _PathLike]
else:
_PathType = Union[bytes, Text]
# ----- os.path variables -----
supports_unicode_filenames = False
@@ -55,20 +60,23 @@ def isdir(path: _PathType) -> bool: ...
def islink(path: _PathType) -> bool: ...
def ismount(path: _PathType) -> bool: ...
# 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: ...
if sys.version_info < (3, 0):
# 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: ...
else:
def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ...
def normcase(path: AnyStr) -> AnyStr: ...
def normpath(path: AnyStr) -> AnyStr: ...

View File

@@ -60,7 +60,23 @@ def isdir(path: _PathType) -> bool: ...
def islink(path: _PathType) -> bool: ...
def ismount(path: _PathType) -> bool: ...
def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ...
if sys.version_info < (3, 0):
# 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: ...
else:
def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ...
def normcase(path: AnyStr) -> AnyStr: ...
def normpath(path: AnyStr) -> AnyStr: ...
@@ -68,7 +84,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: ...