From b38cf89e7e6c2b6d57511665ee9a7ffa126ce902 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Fri, 26 May 2017 18:37:29 -0400 Subject: [PATCH 1/3] fix types in os.path 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. --- stdlib/2/os/path.pyi | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stdlib/2/os/path.pyi b/stdlib/2/os/path.pyi index 56973e423be4..883950b146cf 100644 --- a/stdlib/2/os/path.pyi +++ b/stdlib/2/os/path.pyi @@ -55,7 +55,10 @@ def isdir(path: _PathType) -> bool: ... def islink(path: _PathType) -> bool: ... def ismount(path: _PathType) -> bool: ... -def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ... +@overload +def join(path: bytes, *paths: bytes) -> bytes: ... +@overload +def join(path: _PathType, *paths: _PathType) -> Text: ... def normcase(path: AnyStr) -> AnyStr: ... def normpath(path: AnyStr) -> AnyStr: ... @@ -63,7 +66,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: ... From a208513b80f41754c3eeec847adf749d85139d7d Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Fri, 26 May 2017 20:25:26 -0400 Subject: [PATCH 2/3] add 'type: ignore', for mypy --- stdlib/2/os/path.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/2/os/path.pyi b/stdlib/2/os/path.pyi index 883950b146cf..4e5cd78052e4 100644 --- a/stdlib/2/os/path.pyi +++ b/stdlib/2/os/path.pyi @@ -56,9 +56,9 @@ def islink(path: _PathType) -> bool: ... def ismount(path: _PathType) -> bool: ... @overload -def join(path: bytes, *paths: bytes) -> bytes: ... +def join(path: bytes, *paths: bytes) -> bytes: ... # type: ignore @overload -def join(path: _PathType, *paths: _PathType) -> Text: ... +def join(path: _PathType, *paths: _PathType) -> Text: ... # type: ignore def normcase(path: AnyStr) -> AnyStr: ... def normpath(path: AnyStr) -> AnyStr: ... From d6792bd2c227a6803542d3cef4f3a5ab716d0108 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Wed, 31 May 2017 11:06:00 -0400 Subject: [PATCH 3/3] keep join() signatures disjunct --- stdlib/2/os/path.pyi | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/stdlib/2/os/path.pyi b/stdlib/2/os/path.pyi index 4e5cd78052e4..8f82bf24674a 100644 --- a/stdlib/2/os/path.pyi +++ b/stdlib/2/os/path.pyi @@ -55,10 +55,20 @@ 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(path: bytes, *paths: bytes) -> bytes: ... # type: ignore +def join(__p1: bytes, *p: bytes) -> bytes: ... @overload -def join(path: _PathType, *paths: _PathType) -> Text: ... # type: ignore +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: ...