mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-07 20:54:28 +08:00
add __fspath__ support to os.path (#2053)
Fixes #1997, #2068. This is tricky because we need to get the return values right (see #1960 for prior attempts) and we often run into python/mypy#3644. I found that I could express most signatures correctly using a series of overloads. A few other changes in here: - Added splitunc, which according to https://docs.python.org/3/library/os.path.html should exist in both Unix and Windows. - Made the second argument to os.path.curdir Optional to match the implementation. - Fixed os.path.split, whose previous Path-aware signature triggered python/mypy#3644.
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
# Stubs for os.path
|
||||
# Ron Murawski <ron@horizonchess.com>
|
||||
|
||||
# based on http://docs.python.org/3.2/library/os.path.html
|
||||
# adapted for 2.7 by Michal Pokorny
|
||||
from posix import stat_result
|
||||
import sys
|
||||
from typing import (
|
||||
overload, List, Any, AnyStr, Sequence, Tuple, BinaryIO, TextIO,
|
||||
TypeVar, Union, Text, Callable
|
||||
TypeVar, Union, Text, Callable, Optional
|
||||
)
|
||||
|
||||
_T = TypeVar('_T')
|
||||
@@ -14,38 +13,94 @@ _T = TypeVar('_T')
|
||||
if sys.version_info >= (3, 6):
|
||||
from builtins import _PathLike
|
||||
_PathType = Union[bytes, Text, _PathLike]
|
||||
_StrPath = Union[Text, _PathLike[Text]]
|
||||
_BytesPath = Union[bytes, _PathLike[bytes]]
|
||||
else:
|
||||
_PathType = Union[bytes, Text]
|
||||
_StrPath = Text
|
||||
_BytesPath = bytes
|
||||
|
||||
# ----- os.path variables -----
|
||||
supports_unicode_filenames = False
|
||||
supports_unicode_filenames: bool
|
||||
# aliases (also in os)
|
||||
curdir = ... # type: str
|
||||
pardir = ... # type: str
|
||||
sep = ... # type: str
|
||||
altsep = ... # type: str
|
||||
extsep = ... # type: str
|
||||
pathsep = ... # type: str
|
||||
defpath = ... # type: str
|
||||
devnull = ... # type: str
|
||||
curdir: str
|
||||
pardir: str
|
||||
sep: str
|
||||
altsep: str
|
||||
extsep: str
|
||||
pathsep: str
|
||||
defpath: str
|
||||
devnull: str
|
||||
|
||||
# ----- os.path function stubs -----
|
||||
def abspath(path: AnyStr) -> AnyStr: ...
|
||||
def basename(path: AnyStr) -> AnyStr: ...
|
||||
if sys.version_info >= (3, 6):
|
||||
# Overloads are necessary to work around python/mypy#3644.
|
||||
@overload
|
||||
def abspath(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def abspath(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def basename(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def basename(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def dirname(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def dirname(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def expanduser(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def expanduser(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def expandvars(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def expandvars(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def normcase(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def normcase(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def normpath(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def normpath(path: AnyStr) -> AnyStr: ...
|
||||
if sys.platform == 'win32':
|
||||
@overload
|
||||
def realpath(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def realpath(path: AnyStr) -> AnyStr: ...
|
||||
else:
|
||||
@overload
|
||||
def realpath(filename: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def realpath(filename: AnyStr) -> AnyStr: ...
|
||||
|
||||
if sys.version_info >= (3, 5):
|
||||
else:
|
||||
def abspath(path: AnyStr) -> AnyStr: ...
|
||||
def basename(path: AnyStr) -> AnyStr: ...
|
||||
def dirname(path: AnyStr) -> AnyStr: ...
|
||||
def expanduser(path: AnyStr) -> AnyStr: ...
|
||||
def expandvars(path: AnyStr) -> AnyStr: ...
|
||||
def normcase(path: AnyStr) -> AnyStr: ...
|
||||
def normpath(path: AnyStr) -> AnyStr: ...
|
||||
if sys.platform == 'win32':
|
||||
def realpath(path: AnyStr) -> AnyStr: ...
|
||||
else:
|
||||
def realpath(filename: AnyStr) -> AnyStr: ...
|
||||
|
||||
if sys.version_info >= (3, 6):
|
||||
# In reality it returns str for sequences of _StrPath and bytes for sequences
|
||||
# of _BytesPath, but mypy does not accept such a signature.
|
||||
def commonpath(paths: Sequence[_PathType]) -> Any: ...
|
||||
elif sys.version_info >= (3, 5):
|
||||
def commonpath(paths: Sequence[AnyStr]) -> AnyStr: ...
|
||||
|
||||
# NOTE: Empty lists results in '' (str) regardless of contained type.
|
||||
# Also, in Python 2 mixed sequences of Text and bytes results in either Text or bytes
|
||||
# So, fall back to Any
|
||||
def commonprefix(list: Sequence[AnyStr]) -> Any: ...
|
||||
def commonprefix(list: Sequence[_PathType]) -> Any: ...
|
||||
|
||||
def dirname(path: AnyStr) -> AnyStr: ...
|
||||
def exists(path: _PathType) -> bool: ...
|
||||
def lexists(path: _PathType) -> bool: ...
|
||||
def expanduser(path: AnyStr) -> AnyStr: ...
|
||||
def expandvars(path: AnyStr) -> AnyStr: ...
|
||||
|
||||
# These return float if os.stat_float_times() == True,
|
||||
# but int is a subclass of float.
|
||||
@@ -75,26 +130,41 @@ if sys.version_info < (3, 0):
|
||||
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: ...
|
||||
elif sys.version_info >= (3, 6):
|
||||
# Mypy complains that the signatures overlap (same for relpath below), but things seem to behave correctly anyway.
|
||||
@overload
|
||||
def join(path: _StrPath, *paths: _StrPath) -> Text: ... # type: ignore
|
||||
@overload
|
||||
def join(path: _BytesPath, *paths: _BytesPath) -> bytes: ...
|
||||
else:
|
||||
def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ...
|
||||
|
||||
def normcase(path: AnyStr) -> AnyStr: ...
|
||||
def normpath(path: AnyStr) -> AnyStr: ...
|
||||
if sys.platform == 'win32':
|
||||
def realpath(path: AnyStr) -> AnyStr: ...
|
||||
else:
|
||||
def realpath(filename: AnyStr) -> AnyStr: ...
|
||||
def relpath(path: AnyStr, start: _PathType = ...) -> AnyStr: ...
|
||||
@overload
|
||||
def relpath(path: _StrPath, start: Optional[_StrPath] = ...) -> Text: ... # type: ignore
|
||||
@overload
|
||||
def relpath(path: _BytesPath, start: _BytesPath) -> bytes: ...
|
||||
|
||||
def samefile(path1: _PathType, path2: _PathType) -> bool: ...
|
||||
def sameopenfile(fp1: int, fp2: int) -> bool: ...
|
||||
# TODO
|
||||
# def samestat(stat1: stat_result,
|
||||
# stat2: stat_result) -> bool: ... # Unix only
|
||||
def samestat(stat1: stat_result, stat2: stat_result) -> bool: ...
|
||||
|
||||
def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
if sys.version_info >= (3, 6):
|
||||
@overload
|
||||
def split(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def splitdrive(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def splitext(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
else:
|
||||
def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
|
||||
def splitunc(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... # Windows only, deprecated
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
# Stubs for os.path
|
||||
# Ron Murawski <ron@horizonchess.com>
|
||||
|
||||
# based on http://docs.python.org/3.2/library/os.path.html
|
||||
# adapted for 2.7 by Michal Pokorny
|
||||
from posix import stat_result
|
||||
import sys
|
||||
from typing import (
|
||||
overload, List, Any, AnyStr, Sequence, Tuple, BinaryIO, TextIO,
|
||||
TypeVar, Union, Text, Callable
|
||||
TypeVar, Union, Text, Callable, Optional
|
||||
)
|
||||
|
||||
_T = TypeVar('_T')
|
||||
@@ -14,38 +13,94 @@ _T = TypeVar('_T')
|
||||
if sys.version_info >= (3, 6):
|
||||
from builtins import _PathLike
|
||||
_PathType = Union[bytes, Text, _PathLike]
|
||||
_StrPath = Union[Text, _PathLike[Text]]
|
||||
_BytesPath = Union[bytes, _PathLike[bytes]]
|
||||
else:
|
||||
_PathType = Union[bytes, Text]
|
||||
_StrPath = Text
|
||||
_BytesPath = bytes
|
||||
|
||||
# ----- os.path variables -----
|
||||
supports_unicode_filenames = False
|
||||
supports_unicode_filenames: bool
|
||||
# aliases (also in os)
|
||||
curdir = ... # type: str
|
||||
pardir = ... # type: str
|
||||
sep = ... # type: str
|
||||
altsep = ... # type: str
|
||||
extsep = ... # type: str
|
||||
pathsep = ... # type: str
|
||||
defpath = ... # type: str
|
||||
devnull = ... # type: str
|
||||
curdir: str
|
||||
pardir: str
|
||||
sep: str
|
||||
altsep: str
|
||||
extsep: str
|
||||
pathsep: str
|
||||
defpath: str
|
||||
devnull: str
|
||||
|
||||
# ----- os.path function stubs -----
|
||||
def abspath(path: AnyStr) -> AnyStr: ...
|
||||
def basename(path: AnyStr) -> AnyStr: ...
|
||||
if sys.version_info >= (3, 6):
|
||||
# Overloads are necessary to work around python/mypy#3644.
|
||||
@overload
|
||||
def abspath(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def abspath(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def basename(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def basename(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def dirname(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def dirname(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def expanduser(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def expanduser(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def expandvars(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def expandvars(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def normcase(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def normcase(path: AnyStr) -> AnyStr: ...
|
||||
@overload
|
||||
def normpath(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def normpath(path: AnyStr) -> AnyStr: ...
|
||||
if sys.platform == 'win32':
|
||||
@overload
|
||||
def realpath(path: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def realpath(path: AnyStr) -> AnyStr: ...
|
||||
else:
|
||||
@overload
|
||||
def realpath(filename: _PathLike[AnyStr]) -> AnyStr: ...
|
||||
@overload
|
||||
def realpath(filename: AnyStr) -> AnyStr: ...
|
||||
|
||||
if sys.version_info >= (3, 5):
|
||||
else:
|
||||
def abspath(path: AnyStr) -> AnyStr: ...
|
||||
def basename(path: AnyStr) -> AnyStr: ...
|
||||
def dirname(path: AnyStr) -> AnyStr: ...
|
||||
def expanduser(path: AnyStr) -> AnyStr: ...
|
||||
def expandvars(path: AnyStr) -> AnyStr: ...
|
||||
def normcase(path: AnyStr) -> AnyStr: ...
|
||||
def normpath(path: AnyStr) -> AnyStr: ...
|
||||
if sys.platform == 'win32':
|
||||
def realpath(path: AnyStr) -> AnyStr: ...
|
||||
else:
|
||||
def realpath(filename: AnyStr) -> AnyStr: ...
|
||||
|
||||
if sys.version_info >= (3, 6):
|
||||
# In reality it returns str for sequences of _StrPath and bytes for sequences
|
||||
# of _BytesPath, but mypy does not accept such a signature.
|
||||
def commonpath(paths: Sequence[_PathType]) -> Any: ...
|
||||
elif sys.version_info >= (3, 5):
|
||||
def commonpath(paths: Sequence[AnyStr]) -> AnyStr: ...
|
||||
|
||||
# NOTE: Empty lists results in '' (str) regardless of contained type.
|
||||
# Also, in Python 2 mixed sequences of Text and bytes results in either Text or bytes
|
||||
# So, fall back to Any
|
||||
def commonprefix(list: Sequence[AnyStr]) -> Any: ...
|
||||
def commonprefix(list: Sequence[_PathType]) -> Any: ...
|
||||
|
||||
def dirname(path: AnyStr) -> AnyStr: ...
|
||||
def exists(path: _PathType) -> bool: ...
|
||||
def lexists(path: _PathType) -> bool: ...
|
||||
def expanduser(path: AnyStr) -> AnyStr: ...
|
||||
def expandvars(path: AnyStr) -> AnyStr: ...
|
||||
|
||||
# These return float if os.stat_float_times() == True,
|
||||
# but int is a subclass of float.
|
||||
@@ -75,29 +130,41 @@ if sys.version_info < (3, 0):
|
||||
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: ...
|
||||
elif sys.version_info >= (3, 6):
|
||||
# Mypy complains that the signatures overlap (same for relpath below), but things seem to behave correctly anyway.
|
||||
@overload
|
||||
def join(path: _StrPath, *paths: _StrPath) -> Text: ... # type: ignore
|
||||
@overload
|
||||
def join(path: _BytesPath, *paths: _BytesPath) -> bytes: ...
|
||||
else:
|
||||
def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ...
|
||||
|
||||
def normcase(path: AnyStr) -> AnyStr: ...
|
||||
def normpath(path: AnyStr) -> AnyStr: ...
|
||||
if sys.platform == 'win32':
|
||||
def realpath(path: AnyStr) -> AnyStr: ...
|
||||
else:
|
||||
def realpath(filename: AnyStr) -> AnyStr: ...
|
||||
def relpath(path: AnyStr, start: _PathType = ...) -> AnyStr: ...
|
||||
@overload
|
||||
def relpath(path: _StrPath, start: Optional[_StrPath] = ...) -> Text: ... # type: ignore
|
||||
@overload
|
||||
def relpath(path: _BytesPath, start: _BytesPath) -> bytes: ...
|
||||
|
||||
def samefile(path1: _PathType, path2: _PathType) -> bool: ...
|
||||
def sameopenfile(fp1: int, fp2: int) -> bool: ...
|
||||
# TODO
|
||||
# def samestat(stat1: stat_result,
|
||||
# stat2: stat_result) -> bool: ... # Unix only
|
||||
def samestat(stat1: stat_result, stat2: stat_result) -> bool: ...
|
||||
|
||||
if sys.version_info >= (3, 6):
|
||||
def split(path: Union[AnyStr, _PathLike[AnyStr]]) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def split(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def splitdrive(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def splitext(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ...
|
||||
@overload
|
||||
def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
else:
|
||||
def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
||||
|
||||
def splitunc(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... # Windows only, deprecated
|
||||
|
||||
|
||||
Reference in New Issue
Block a user