From c1c3c20fd95456d6b2f3c14a8818de3cedc66fc5 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 2 Jul 2017 16:49:21 -0700 Subject: [PATCH] make os.path identical in Python 2 and 3 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. --- stdlib/2/os/path.pyi | 38 +++++++++++++++++++++++--------------- stdlib/3/os/path.pyi | 20 ++++++++++++++++++-- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/stdlib/2/os/path.pyi b/stdlib/2/os/path.pyi index 8f82bf24674a..961c7a8838cc 100644 --- a/stdlib/2/os/path.pyi +++ b/stdlib/2/os/path.pyi @@ -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: ... diff --git a/stdlib/3/os/path.pyi b/stdlib/3/os/path.pyi index fca189a7438c..961c7a8838cc 100644 --- a/stdlib/3/os/path.pyi +++ b/stdlib/3/os/path.pyi @@ -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: ...