Skip to content

Commit 2e1ebab

Browse files
Merge pull request #1054 from euresti/path_type
Use Union[bytes, Text] for paths in os.py
2 parents c628cd1 + b362177 commit 2e1ebab

File tree

2 files changed

+99
-98
lines changed

2 files changed

+99
-98
lines changed

stdlib/2/os/__init__.pyi

+48-47
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import (
44
List, Tuple, Union, Sequence, Mapping, IO, Any, Optional, AnyStr, Iterator,
5-
Dict, MutableMapping, NamedTuple, overload
5+
Dict, MutableMapping, NamedTuple, overload, Text
66
)
77
from . import path
88
from mypy_extensions import NoReturn
@@ -93,6 +93,7 @@ WCONTINUED = 0 # some Unix systems
9393
WUNTRACED = 0 # Unix only
9494

9595
TMP_MAX = 0
96+
_PathType = Union[bytes, Text]
9697
_StatVFS = NamedTuple('_StatVFS', [('f_bsize', int), ('f_frsize', int), ('f_blocks', int),
9798
('f_bfree', int), ('f_bavail', int), ('f_files', int),
9899
('f_ffree', int), ('f_favail', int), ('f_flag', int),
@@ -144,66 +145,66 @@ def fsync(fd: int) -> None: ...
144145
def ftruncate(fd: int, length: int) -> None: ...
145146
def isatty(fd: int) -> bool: ...
146147
def lseek(fd: int, pos: int, how: int) -> None: ...
147-
def open(file: unicode, flags: int, mode: int = ...) -> int: ...
148+
def open(file: _PathType, flags: int, mode: int = ...) -> int: ...
148149
def openpty() -> Tuple[int, int]: ...
149150
def pipe() -> Tuple[int, int]: ...
150151
def read(fd: int, n: int) -> str: ...
151152
def tcgetpgrp(fd: int) -> int: ...
152153
def tcsetpgrp(fd: int, pg: int) -> None: ...
153154
def ttyname(fd: int) -> str: ...
154155
def write(fd: int, str: str) -> int: ...
155-
def access(path: unicode, mode: int) -> bool: ...
156+
def access(path: _PathType, mode: int) -> bool: ...
156157
def fpathconf(fd: int, name: str) -> None: ...
157-
def chdir(path: unicode) -> None: ...
158+
def chdir(path: _PathType) -> None: ...
158159
def fchdir(fd: int) -> None: ...
159160
def getcwd() -> str: ...
160161
def getcwdu() -> unicode: ...
161-
def chflags(path: unicode, flags: int) -> None: ...
162-
def chroot(path: unicode) -> None: ...
163-
def chmod(path: unicode, mode: int) -> None: ...
164-
def chown(path: unicode, uid: int, gid: int) -> None: ...
165-
def lchflags(path: unicode, flags: int) -> None: ...
166-
def lchmod(path: unicode, uid: int, gid: int) -> None: ...
167-
def lchown(path: unicode, uid: int, gid: int) -> None: ...
168-
def link(source: unicode, link_name: unicode) -> None: ...
162+
def chflags(path: _PathType, flags: int) -> None: ...
163+
def chroot(path: _PathType) -> None: ...
164+
def chmod(path: _PathType, mode: int) -> None: ...
165+
def chown(path: _PathType, uid: int, gid: int) -> None: ...
166+
def lchflags(path: _PathType, flags: int) -> None: ...
167+
def lchmod(path: _PathType, uid: int, gid: int) -> None: ...
168+
def lchown(path: _PathType, uid: int, gid: int) -> None: ...
169+
def link(source: _PathType, link_name: _PathType) -> None: ...
169170
def listdir(path: AnyStr) -> List[AnyStr]: ...
170-
def lstat(path: unicode) -> Any: ...
171-
def mkfifo(path: unicode, mode: int = ...) -> None: ...
172-
def mknod(filename: unicode, mode: int = ..., device: int = ...) -> None: ...
171+
def lstat(path: _PathType) -> Any: ...
172+
def mkfifo(path: _PathType, mode: int = ...) -> None: ...
173+
def mknod(filename: _PathType, mode: int = ..., device: int = ...) -> None: ...
173174
def major(device: int) -> int: ...
174175
def minor(device: int) -> int: ...
175176
def makedev(major: int, minor: int) -> int: ...
176-
def mkdir(path: unicode, mode: int = ...) -> None: ...
177-
def makedirs(path: unicode, mode: int = ...) -> None: ...
178-
def pathconf(path: unicode, name: str) -> str: ...
177+
def mkdir(path: _PathType, mode: int = ...) -> None: ...
178+
def makedirs(path: _PathType, mode: int = ...) -> None: ...
179+
def pathconf(path: _PathType, name: str) -> str: ...
179180
def readlink(path: AnyStr) -> AnyStr: ...
180-
def remove(path: unicode) -> None: ...
181-
def removedirs(path: unicode) -> None: ...
182-
def rename(src: unicode, dst: unicode) -> None: ...
183-
def renames(old: unicode, new: unicode) -> None: ...
184-
def rmdir(path: unicode) -> None: ...
185-
def stat(path: unicode) -> Any: ...
181+
def remove(path: _PathType) -> None: ...
182+
def removedirs(path: _PathType) -> None: ...
183+
def rename(src: _PathType, dst: _PathType) -> None: ...
184+
def renames(old: _PathType, new: _PathType) -> None: ...
185+
def rmdir(path: _PathType) -> None: ...
186+
def stat(path: _PathType) -> Any: ...
186187
@overload
187188
def stat_float_times(newvalue: bool = ...) -> None: ...
188189
@overload
189190
def stat_float_times() -> bool: ...
190-
def statvfs(path: unicode) -> _StatVFS: ...
191-
def symlink(source: unicode, link_name: unicode) -> None: ...
192-
def unlink(path: unicode) -> None: ...
193-
def utime(path: unicode, times: Optional[Tuple[int, int]]) -> None: ...
191+
def statvfs(path: _PathType) -> _StatVFS: ...
192+
def symlink(source: _PathType, link_name: _PathType) -> None: ...
193+
def unlink(path: _PathType) -> None: ...
194+
def utime(path: _PathType, times: Optional[Tuple[int, int]]) -> None: ...
194195
def walk(top: AnyStr, topdown: bool = ..., onerror: Any = ...,
195196
followlinks: bool = ...) -> Iterator[Tuple[AnyStr, List[AnyStr],
196197
List[AnyStr]]]: ...
197198

198199
def abort() -> None: ...
199-
def execl(file: AnyStr, *args) -> None: ...
200-
def execle(file: AnyStr, *args) -> None: ...
201-
def execlp(file: AnyStr, *args) -> None: ...
202-
def execlpe(file: AnyStr, *args) -> None: ...
203-
def execv(path: AnyStr, args: Union[Tuple[AnyStr], List[AnyStr]]) -> None: ...
204-
def execve(path: AnyStr, args: Union[Tuple[AnyStr], List[AnyStr]], env: Mapping[AnyStr, AnyStr]) -> None: ...
205-
def execvp(file: AnyStr, args: Union[Tuple[AnyStr], List[AnyStr]]) -> None: ...
206-
def execvpe(file: AnyStr, args: Union[Tuple[AnyStr], List[AnyStr]], env: Mapping[AnyStr, AnyStr]) -> None: ...
200+
def execl(file: _PathType, *args) -> None: ...
201+
def execle(file: _PathType, *args) -> None: ...
202+
def execlp(file: _PathType, *args) -> None: ...
203+
def execlpe(file: _PathType, *args) -> None: ...
204+
def execv(path: _PathType, args: Union[Tuple[Union[bytes, Text]], List[Union[bytes, Text]]]) -> None: ...
205+
def execve(path: _PathType, args: Union[Tuple[Union[bytes, Text]], List[Union[bytes, Text]]], env: Mapping[str, str]) -> None: ...
206+
def execvp(file: _PathType, args: Union[Tuple[Union[bytes, Text]], List[Union[bytes, Text]]]) -> None: ...
207+
def execvpe(file: _PathType, args: Union[Tuple[Union[bytes, Text]], List[Union[bytes, Text]]], env: Mapping[str, str]) -> None: ...
207208
def _exit(n: int) -> NoReturn: ...
208209
def fork() -> int: ...
209210
def forkpty() -> Tuple[int, int]: ...
@@ -216,22 +217,22 @@ def popen2(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ...
216217
def popen3(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any], IO[Any]]: ...
217218
def popen4(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ...
218219

219-
def spawnl(mode: int, path: AnyStr, arg0: AnyStr, *args: AnyStr) -> int: ...
220-
def spawnle(mode: int, path: AnyStr, arg0: AnyStr,
220+
def spawnl(mode: int, path: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ...
221+
def spawnle(mode: int, path: _PathType, arg0: Union[bytes, Text],
221222
*args: Any) -> int: ... # Imprecise sig
222-
def spawnlp(mode: int, file: AnyStr, arg0: AnyStr,
223-
*args: AnyStr) -> int: ... # Unix only TODO
224-
def spawnlpe(mode: int, file: AnyStr, arg0: AnyStr, *args: Any) -> int:
223+
def spawnlp(mode: int, file: _PathType, arg0: Union[bytes, Text],
224+
*args: Union[bytes, Text]) -> int: ... # Unix only TODO
225+
def spawnlpe(mode: int, file: _PathType, arg0: Union[bytes, Text], *args: Any) -> int:
225226
... # Imprecise signature; Unix only TODO
226-
def spawnv(mode: int, path: AnyStr, args: List[AnyStr]) -> int: ...
227-
def spawnve(mode: int, path: AnyStr, args: List[AnyStr],
227+
def spawnv(mode: int, path: _PathType, args: List[Union[bytes, Text]]) -> int: ...
228+
def spawnve(mode: int, path: _PathType, args: List[Union[bytes, Text]],
228229
env: Mapping[str, str]) -> int: ...
229-
def spawnvp(mode: int, file: AnyStr, args: List[AnyStr]) -> int: ... # Unix only
230-
def spawnvpe(mode: int, file: AnyStr, args: List[AnyStr],
230+
def spawnvp(mode: int, file: _PathType, args: List[Union[bytes, Text]]) -> int: ... # Unix only
231+
def spawnvpe(mode: int, file: _PathType, args: List[Union[bytes, Text]],
231232
env: Mapping[str, str]) -> int:
232233
... # Unix only
233-
def startfile(path: unicode, operation: str = ...) -> None: ... # Windows only
234-
def system(command: unicode) -> int: ...
234+
def startfile(path: _PathType, operation: Optional[str] = ...) -> None: ... # Windows only
235+
def system(command: _PathType) -> int: ...
235236
def times() -> Tuple[float, float, float, float, float]: ...
236237
def wait() -> Tuple[int, int]: ... # Unix only
237238
def waitpid(pid: int, options: int) -> Tuple[int, int]: ...

stdlib/3/os/__init__.pyi

+51-51
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ from io import TextIOWrapper as _TextIOWrapper
88
import sys
99
from typing import (
1010
Mapping, MutableMapping, Dict, List, Any, Tuple, Iterator, overload, Union, AnyStr,
11-
Optional, Generic, Set, Callable
11+
Optional, Generic, Set, Callable, Text
1212
)
1313
from . import path
1414
from mypy_extensions import NoReturn
@@ -117,6 +117,7 @@ if sys.version_info >= (3, 6):
117117
class PathLike:
118118
def __fspath__(self) -> AnyStr: ...
119119

120+
_PathType = Union[bytes, Text]
120121

121122
if sys.version_info >= (3, 5):
122123
class DirEntry:
@@ -226,7 +227,7 @@ def setuid(uid: int) -> None: ... # Unix only
226227
def strerror(code: int) -> str: ...
227228
def umask(mask: int) -> int: ...
228229
def uname() -> Tuple[str, str, str, str, str]: ... # Unix only
229-
def unsetenv(key: AnyStr) -> None: ...
230+
def unsetenv(key: _PathType) -> None: ...
230231
# Return IO or TextIO
231232
def fdopen(fd: int, mode: str = ..., buffering: int = ..., encoding: str = ...,
232233
errors: str = ..., newline: str = ..., closefd: bool = ...) -> Any: ...
@@ -245,82 +246,81 @@ def fsync(fd: int) -> None: ...
245246
def ftruncate(fd: int, length: int) -> None: ... # Unix only
246247
def isatty(fd: int) -> bool: ... # Unix only
247248
def lseek(fd: int, pos: int, how: int) -> int: ...
248-
def open(file: AnyStr, flags: int, mode: int = ...) -> int: ...
249+
def open(file: _PathType, flags: int, mode: int = ...) -> int: ...
249250
def openpty() -> Tuple[int, int]: ... # some flavors of Unix
250251
def pipe() -> Tuple[int, int]: ...
251252
def read(fd: int, n: int) -> bytes: ...
252253
def tcgetpgrp(fd: int) -> int: ... # Unix only
253254
def tcsetpgrp(fd: int, pg: int) -> None: ... # Unix only
254255
def ttyname(fd: int) -> str: ... # Unix only
255256
def write(fd: int, string: bytes) -> int: ...
256-
def access(path: AnyStr, mode: int) -> bool: ...
257-
def chdir(path: AnyStr) -> None: ...
257+
def access(path: _PathType, mode: int) -> bool: ...
258+
def chdir(path: _PathType) -> None: ...
258259
def fchdir(fd: int) -> None: ...
259260
def getcwd() -> str: ...
260261
def getcwdb() -> bytes: ...
261-
def chflags(path: str, flags: int) -> None: ... # Unix only
262-
def chroot(path: str) -> None: ... # Unix only
263-
def chmod(path: AnyStr, mode: int) -> None: ...
264-
def chown(path: AnyStr, uid: int, gid: int) -> None: ... # Unix only
265-
def lchflags(path: str, flags: int) -> None: ... # Unix only
266-
def lchmod(path: str, mode: int) -> None: ... # Unix only
267-
def lchown(path: str, uid: int, gid: int) -> None: ... # Unix only
268-
def link(src: AnyStr, link_name: AnyStr) -> None: ...
262+
def chflags(path: _PathType, flags: int) -> None: ... # Unix only
263+
def chroot(path: _PathType) -> None: ... # Unix only
264+
def chmod(path: _PathType, mode: int) -> None: ...
265+
def chown(path: _PathType, uid: int, gid: int) -> None: ... # Unix only
266+
def lchflags(path: _PathType, flags: int) -> None: ... # Unix only
267+
def lchmod(path: _PathType, mode: int) -> None: ... # Unix only
268+
def lchown(path: _PathType, uid: int, gid: int) -> None: ... # Unix only
269+
def link(src: _PathType, link_name: _PathType) -> None: ...
269270

270271
@overload
271272
def listdir(path: str = ...) -> List[str]: ...
272273
@overload
273274
def listdir(path: bytes) -> List[bytes]: ...
274275

275-
def lstat(path: AnyStr) -> stat_result: ...
276-
def mkfifo(path: str, mode: int = ...) -> None: ... # Unix only
277-
def mknod(filename: AnyStr, mode: int = ..., device: int = ...) -> None: ...
276+
def lstat(path: _PathType) -> stat_result: ...
277+
def mkfifo(path: _PathType, mode: int = ...) -> None: ... # Unix only
278+
def mknod(filename: _PathType, mode: int = ..., device: int = ...) -> None: ...
278279
def major(device: int) -> int: ...
279280
def minor(device: int) -> int: ...
280281
def makedev(major: int, minor: int) -> int: ...
281-
def mkdir(path: AnyStr, mode: int = ...) -> None: ...
282-
def makedirs(path: AnyStr, mode: int = ...,
282+
def mkdir(path: _PathType, mode: int = ...) -> None: ...
283+
def makedirs(path: _PathType, mode: int = ...,
283284
exist_ok: bool = ...) -> None: ...
284-
def pathconf(path: str, name: str) -> int: ... # Unix only
285+
def pathconf(path: _PathType, name: str) -> int: ... # Unix only
285286
def readlink(path: AnyStr) -> AnyStr: ...
286-
def remove(path: AnyStr) -> None: ...
287-
def removedirs(path: AnyStr) -> None: ...
288-
def rename(src: AnyStr, dst: AnyStr) -> None: ...
289-
def renames(old: AnyStr, new: AnyStr) -> None: ...
287+
def remove(path: _PathType) -> None: ...
288+
def removedirs(path: _PathType) -> None: ...
289+
def rename(src: _PathType, dst: _PathType) -> None: ...
290+
def renames(old: _PathType, new: _PathType) -> None: ...
290291
if sys.version_info >= (3, 3):
291-
def replace(src: AnyStr, dst: AnyStr) -> None: ...
292-
def rmdir(path: AnyStr) -> None: ...
292+
def replace(src: _PathType, dst: _PathType) -> None: ...
293+
def rmdir(path: _PathType) -> None: ...
293294
if sys.version_info >= (3, 5):
294295
@overload
295296
def scandir(path: str = ...) -> Iterator[DirEntry]: ...
296297
@overload
297298
def scandir(path: bytes) -> Iterator[DirEntry]: ...
298-
def stat(path: AnyStr) -> stat_result: ...
299+
def stat(path: _PathType) -> stat_result: ...
299300
def stat_float_times(newvalue: Union[bool, None] = ...) -> bool: ...
300-
def statvfs(path: str) -> statvfs_result: ... # Unix only
301-
def symlink(source: AnyStr, link_name: AnyStr,
301+
def statvfs(path: _PathType) -> statvfs_result: ... # Unix only
302+
def symlink(source: _PathType, link_name: _PathType,
302303
target_is_directory: bool = ...) -> None:
303304
... # final argument in Windows only
304-
def unlink(path: AnyStr) -> None: ...
305-
def utime(path: AnyStr, times: Union[Tuple[int, int], Tuple[float, float]] = ...) -> None: ...
305+
def unlink(path: _PathType) -> None: ...
306+
def utime(path: _PathType, times: Union[Tuple[int, int], Tuple[float, float]] = ...) -> None: ...
306307

307308
# TODO onerror: function from OSError to void
308309
def walk(top: AnyStr, topdown: bool = ..., onerror: Any = ...,
309310
followlinks: bool = ...) -> Iterator[Tuple[AnyStr, List[AnyStr],
310311
List[AnyStr]]]: ...
311312

312313
def abort() -> 'None': ...
313-
def execl(path: AnyStr, arg0: AnyStr, *args: AnyStr) -> None: ...
314-
def execle(path: AnyStr, arg0: AnyStr,
314+
def execl(path: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> None: ...
315+
def execle(path: _PathType, arg0: Union[bytes, Text],
315316
*args: Any) -> None: ... # Imprecise signature
316-
def execlp(path: AnyStr, arg0: AnyStr, *args: AnyStr) -> None: ...
317-
def execlpe(path: AnyStr, arg0: AnyStr,
317+
def execlp(path: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> None: ...
318+
def execlpe(path: _PathType, arg0: Union[bytes, Text],
318319
*args: Any) -> None: ... # Imprecise signature
319-
def execv(path: AnyStr, args: Union[Tuple[AnyStr], List[AnyStr]]) -> None: ...
320-
def execve(path: AnyStr, args: Union[Tuple[AnyStr], List[AnyStr]], env: Mapping[AnyStr, AnyStr]) -> None: ...
321-
def execvp(file: AnyStr, args: Union[Tuple[AnyStr], List[AnyStr]]) -> None: ...
322-
def execvpe(file: AnyStr, args: Union[Tuple[AnyStr], List[AnyStr]],
323-
env: Mapping[str, str]) -> None: ...
320+
def execv(path: _PathType, args: Union[Tuple[Union[bytes, Text]], List[Union[bytes, Text]]]) -> None: ...
321+
def execve(path: _PathType, args: Union[Tuple[Union[bytes, Text]], List[Union[bytes, Text]]], env: Mapping[str, str]) -> None: ...
322+
def execvp(file: _PathType, args: Union[Tuple[Union[bytes, Text]], List[Union[bytes, Text]]]) -> None: ...
323+
def execvpe(file: _PathType, args: Union[Tuple[Union[bytes, Text]], List[Union[bytes, Text]]], env: Mapping[str, str]) -> None: ...
324324
def _exit(n: int) -> NoReturn: ...
325325
def fork() -> int: ... # Unix only
326326
def forkpty() -> Tuple[int, int]: ... # some flavors of Unix
@@ -335,22 +335,22 @@ class popen(_TextIOWrapper):
335335
bufsize: int = ...) -> None: ...
336336
def close(self) -> Any: ... # may return int
337337

338-
def spawnl(mode: int, path: AnyStr, arg0: AnyStr, *args: AnyStr) -> int: ...
339-
def spawnle(mode: int, path: AnyStr, arg0: AnyStr,
338+
def spawnl(mode: int, path: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ...
339+
def spawnle(mode: int, path: _PathType, arg0: Union[bytes, Text],
340340
*args: Any) -> int: ... # Imprecise sig
341-
def spawnlp(mode: int, file: AnyStr, arg0: AnyStr,
342-
*args: AnyStr) -> int: ... # Unix only TODO
343-
def spawnlpe(mode: int, file: AnyStr, arg0: AnyStr, *args: Any) -> int:
341+
def spawnlp(mode: int, file: _PathType, arg0: Union[bytes, Text],
342+
*args: Union[bytes, Text]) -> int: ... # Unix only TODO
343+
def spawnlpe(mode: int, file: _PathType, arg0: Union[bytes, Text], *args: Any) -> int:
344344
... # Imprecise signature; Unix only TODO
345-
def spawnv(mode: int, path: AnyStr, args: List[AnyStr]) -> int: ...
346-
def spawnve(mode: int, path: AnyStr, args: List[AnyStr],
345+
def spawnv(mode: int, path: _PathType, args: List[Union[bytes, Text]]) -> int: ...
346+
def spawnve(mode: int, path: _PathType, args: List[Union[bytes, Text]],
347347
env: Mapping[str, str]) -> int: ...
348-
def spawnvp(mode: int, file: AnyStr, args: List[AnyStr]) -> int: ... # Unix only
349-
def spawnvpe(mode: int, file: AnyStr, args: List[AnyStr],
348+
def spawnvp(mode: int, file: _PathType, args: List[Union[bytes, Text]]) -> int: ... # Unix only
349+
def spawnvpe(mode: int, file: _PathType, args: List[Union[bytes, Text]],
350350
env: Mapping[str, str]) -> int:
351351
... # Unix only
352-
def startfile(path: str, operation: Union[str, None] = ...) -> None: ... # Windows only
353-
def system(command: AnyStr) -> int: ...
352+
def startfile(path: _PathType, operation: Optional[str] = ...) -> None: ... # Windows only
353+
def system(command: _PathType) -> int: ...
354354
def times() -> Tuple[float, float, float, float, float]: ...
355355
def wait() -> Tuple[int, int]: ... # Unix only
356356
def waitpid(pid: int, options: int) -> Tuple[int, int]: ...
@@ -381,7 +381,7 @@ WNOWAIT = 0
381381
if sys.version_info >= (3, 3):
382382
def sync() -> None: ... # Unix only
383383

384-
def truncate(path: Union[AnyStr, int], length: int) -> None: ... # Unix only up to version 3.4
384+
def truncate(path: Union[_PathType, int], length: int) -> None: ... # Unix only up to version 3.4
385385

386386
def fwalk(top: AnyStr = ..., topdown: bool = ...,
387387
onerror: Callable = ..., *, follow_symlinks: bool = ...,

0 commit comments

Comments
 (0)