|
1 | 1 | # Stubs for os.path
|
2 | 2 | # Ron Murawski <[email protected]>
|
3 | 3 |
|
4 |
| -# based on http://docs.python.org/3.2/library/os.path.html |
5 |
| -# adapted for 2.7 by Michal Pokorny |
| 4 | +from posix import stat_result |
6 | 5 | import sys
|
7 | 6 | from typing import (
|
8 | 7 | overload, List, Any, AnyStr, Sequence, Tuple, BinaryIO, TextIO,
|
9 |
| - TypeVar, Union, Text, Callable |
| 8 | + TypeVar, Union, Text, Callable, Optional |
10 | 9 | )
|
11 | 10 |
|
12 | 11 | _T = TypeVar('_T')
|
13 | 12 |
|
14 | 13 | if sys.version_info >= (3, 6):
|
15 | 14 | from builtins import _PathLike
|
16 | 15 | _PathType = Union[bytes, Text, _PathLike]
|
| 16 | + _StrPath = Union[Text, _PathLike[Text]] |
| 17 | + _BytesPath = Union[bytes, _PathLike[bytes]] |
17 | 18 | else:
|
18 | 19 | _PathType = Union[bytes, Text]
|
| 20 | + _StrPath = Text |
| 21 | + _BytesPath = bytes |
19 | 22 |
|
20 | 23 | # ----- os.path variables -----
|
21 |
| -supports_unicode_filenames = False |
| 24 | +supports_unicode_filenames: bool |
22 | 25 | # aliases (also in os)
|
23 |
| -curdir = ... # type: str |
24 |
| -pardir = ... # type: str |
25 |
| -sep = ... # type: str |
26 |
| -altsep = ... # type: str |
27 |
| -extsep = ... # type: str |
28 |
| -pathsep = ... # type: str |
29 |
| -defpath = ... # type: str |
30 |
| -devnull = ... # type: str |
| 26 | +curdir: str |
| 27 | +pardir: str |
| 28 | +sep: str |
| 29 | +altsep: str |
| 30 | +extsep: str |
| 31 | +pathsep: str |
| 32 | +defpath: str |
| 33 | +devnull: str |
31 | 34 |
|
32 | 35 | # ----- os.path function stubs -----
|
33 |
| -def abspath(path: AnyStr) -> AnyStr: ... |
34 |
| -def basename(path: AnyStr) -> AnyStr: ... |
| 36 | +if sys.version_info >= (3, 6): |
| 37 | + # Overloads are necessary to work around python/mypy#3644. |
| 38 | + @overload |
| 39 | + def abspath(path: _PathLike[AnyStr]) -> AnyStr: ... |
| 40 | + @overload |
| 41 | + def abspath(path: AnyStr) -> AnyStr: ... |
| 42 | + @overload |
| 43 | + def basename(path: _PathLike[AnyStr]) -> AnyStr: ... |
| 44 | + @overload |
| 45 | + def basename(path: AnyStr) -> AnyStr: ... |
| 46 | + @overload |
| 47 | + def dirname(path: _PathLike[AnyStr]) -> AnyStr: ... |
| 48 | + @overload |
| 49 | + def dirname(path: AnyStr) -> AnyStr: ... |
| 50 | + @overload |
| 51 | + def expanduser(path: _PathLike[AnyStr]) -> AnyStr: ... |
| 52 | + @overload |
| 53 | + def expanduser(path: AnyStr) -> AnyStr: ... |
| 54 | + @overload |
| 55 | + def expandvars(path: _PathLike[AnyStr]) -> AnyStr: ... |
| 56 | + @overload |
| 57 | + def expandvars(path: AnyStr) -> AnyStr: ... |
| 58 | + @overload |
| 59 | + def normcase(path: _PathLike[AnyStr]) -> AnyStr: ... |
| 60 | + @overload |
| 61 | + def normcase(path: AnyStr) -> AnyStr: ... |
| 62 | + @overload |
| 63 | + def normpath(path: _PathLike[AnyStr]) -> AnyStr: ... |
| 64 | + @overload |
| 65 | + def normpath(path: AnyStr) -> AnyStr: ... |
| 66 | + if sys.platform == 'win32': |
| 67 | + @overload |
| 68 | + def realpath(path: _PathLike[AnyStr]) -> AnyStr: ... |
| 69 | + @overload |
| 70 | + def realpath(path: AnyStr) -> AnyStr: ... |
| 71 | + else: |
| 72 | + @overload |
| 73 | + def realpath(filename: _PathLike[AnyStr]) -> AnyStr: ... |
| 74 | + @overload |
| 75 | + def realpath(filename: AnyStr) -> AnyStr: ... |
| 76 | + |
| 77 | +else: |
| 78 | + def abspath(path: AnyStr) -> AnyStr: ... |
| 79 | + def basename(path: AnyStr) -> AnyStr: ... |
| 80 | + def dirname(path: AnyStr) -> AnyStr: ... |
| 81 | + def expanduser(path: AnyStr) -> AnyStr: ... |
| 82 | + def expandvars(path: AnyStr) -> AnyStr: ... |
| 83 | + def normcase(path: AnyStr) -> AnyStr: ... |
| 84 | + def normpath(path: AnyStr) -> AnyStr: ... |
| 85 | + if sys.platform == 'win32': |
| 86 | + def realpath(path: AnyStr) -> AnyStr: ... |
| 87 | + else: |
| 88 | + def realpath(filename: AnyStr) -> AnyStr: ... |
35 | 89 |
|
36 |
| -if sys.version_info >= (3, 5): |
| 90 | +if sys.version_info >= (3, 6): |
| 91 | + # In reality it returns str for sequences of _StrPath and bytes for sequences |
| 92 | + # of _BytesPath, but mypy does not accept such a signature. |
| 93 | + def commonpath(paths: Sequence[_PathType]) -> Any: ... |
| 94 | +elif sys.version_info >= (3, 5): |
37 | 95 | def commonpath(paths: Sequence[AnyStr]) -> AnyStr: ...
|
38 | 96 |
|
39 | 97 | # NOTE: Empty lists results in '' (str) regardless of contained type.
|
40 | 98 | # Also, in Python 2 mixed sequences of Text and bytes results in either Text or bytes
|
41 | 99 | # So, fall back to Any
|
42 |
| -def commonprefix(list: Sequence[AnyStr]) -> Any: ... |
| 100 | +def commonprefix(list: Sequence[_PathType]) -> Any: ... |
43 | 101 |
|
44 |
| -def dirname(path: AnyStr) -> AnyStr: ... |
45 | 102 | def exists(path: _PathType) -> bool: ...
|
46 | 103 | def lexists(path: _PathType) -> bool: ...
|
47 |
| -def expanduser(path: AnyStr) -> AnyStr: ... |
48 |
| -def expandvars(path: AnyStr) -> AnyStr: ... |
49 | 104 |
|
50 | 105 | # These return float if os.stat_float_times() == True,
|
51 | 106 | # but int is a subclass of float.
|
@@ -75,29 +130,41 @@ if sys.version_info < (3, 0):
|
75 | 130 | def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ...
|
76 | 131 | @overload
|
77 | 132 | def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ...
|
| 133 | +elif sys.version_info >= (3, 6): |
| 134 | + # Mypy complains that the signatures overlap (same for relpath below), but things seem to behave correctly anyway. |
| 135 | + @overload |
| 136 | + def join(path: _StrPath, *paths: _StrPath) -> Text: ... # type: ignore |
| 137 | + @overload |
| 138 | + def join(path: _BytesPath, *paths: _BytesPath) -> bytes: ... |
78 | 139 | else:
|
79 | 140 | def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ...
|
80 | 141 |
|
81 |
| -def normcase(path: AnyStr) -> AnyStr: ... |
82 |
| -def normpath(path: AnyStr) -> AnyStr: ... |
83 |
| -if sys.platform == 'win32': |
84 |
| - def realpath(path: AnyStr) -> AnyStr: ... |
85 |
| -else: |
86 |
| - def realpath(filename: AnyStr) -> AnyStr: ... |
87 |
| -def relpath(path: AnyStr, start: _PathType = ...) -> AnyStr: ... |
| 142 | +@overload |
| 143 | +def relpath(path: _StrPath, start: Optional[_StrPath] = ...) -> Text: ... # type: ignore |
| 144 | +@overload |
| 145 | +def relpath(path: _BytesPath, start: _BytesPath) -> bytes: ... |
88 | 146 |
|
89 | 147 | def samefile(path1: _PathType, path2: _PathType) -> bool: ...
|
90 | 148 | def sameopenfile(fp1: int, fp2: int) -> bool: ...
|
91 |
| -# TODO |
92 |
| -# def samestat(stat1: stat_result, |
93 |
| -# stat2: stat_result) -> bool: ... # Unix only |
| 149 | +def samestat(stat1: stat_result, stat2: stat_result) -> bool: ... |
94 | 150 |
|
95 | 151 | if sys.version_info >= (3, 6):
|
96 |
| - def split(path: Union[AnyStr, _PathLike[AnyStr]]) -> Tuple[AnyStr, AnyStr]: ... |
| 152 | + @overload |
| 153 | + def split(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... |
| 154 | + @overload |
| 155 | + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... |
| 156 | + @overload |
| 157 | + def splitdrive(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... |
| 158 | + @overload |
| 159 | + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... |
| 160 | + @overload |
| 161 | + def splitext(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... |
| 162 | + @overload |
| 163 | + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... |
97 | 164 | else:
|
98 | 165 | def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
|
99 |
| -def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... |
100 |
| -def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... |
| 166 | + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... |
| 167 | + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... |
101 | 168 |
|
102 | 169 | def splitunc(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... # Windows only, deprecated
|
103 | 170 |
|
|
0 commit comments