use _typeshed's Path aliases (#4214)

This commit is contained in:
Jelle Zijlstra
2020-06-10 20:57:09 -07:00
committed by GitHub
parent 86f03f2d7b
commit 43e93f803f
42 changed files with 483 additions and 757 deletions

View File

@@ -1,19 +1,20 @@
# Stubs for os
# Ron Murawski <ron@horizonchess.com>
from io import TextIOWrapper as _TextIOWrapper
from posix import listdir as listdir, times_result
import sys
from typing import (
Mapping, MutableMapping, Dict, List, Any, Tuple, Iterable, Iterator, NoReturn, overload, Union, AnyStr,
Optional, Generic, Set, Callable, Text, Sequence, NamedTuple, TypeVar, ContextManager
Optional, Generic, Set, Callable, Text, Sequence, NamedTuple, ContextManager, TypeVar
)
from _typeshed import AnyPath
# Re-exported names from other modules.
from builtins import OSError as error
from . import path as path
_T = TypeVar('_T')
# We need to use something from path, or flake8 and pytype get unhappy
_supports_unicode_filenames = path.supports_unicode_filenames
_T = TypeVar("_T")
# ----- os variables -----
@@ -244,8 +245,7 @@ class stat_result:
if sys.version_info >= (3, 6):
from builtins import _PathLike as PathLike # See comment in builtins
_PathType = path._PathType
_FdOrPathType = Union[int, _PathType]
_FdOrAnyPath = Union[int, AnyPath]
if sys.version_info >= (3, 6):
class DirEntry(PathLike[AnyStr]):
@@ -393,7 +393,7 @@ else:
def fstat(fd: int) -> stat_result: ...
def fsync(fd: int) -> None: ...
def lseek(__fd: int, __position: int, __how: int) -> int: ...
def open(path: _PathType, flags: int, mode: int = ..., *, dir_fd: Optional[int] = ...) -> int: ...
def open(path: AnyPath, flags: int, mode: int = ..., *, dir_fd: Optional[int] = ...) -> int: ...
def pipe() -> Tuple[int, int]: ...
def read(__fd: int, __length: int) -> bytes: ...
@@ -440,59 +440,59 @@ if sys.platform != 'win32':
def ttyname(__fd: int) -> str: ...
def write(__fd: int, __data: bytes) -> int: ...
def access(
path: _FdOrPathType,
path: _FdOrAnyPath,
mode: int,
*,
dir_fd: Optional[int] = ...,
effective_ids: bool = ...,
follow_symlinks: bool = ...,
) -> bool: ...
def chdir(path: _FdOrPathType) -> None: ...
def chdir(path: _FdOrAnyPath) -> None: ...
if sys.platform != "win32":
def fchdir(fd: int) -> None: ...
def getcwd() -> str: ...
def getcwdb() -> bytes: ...
def chmod(path: _FdOrPathType, mode: int, *, dir_fd: Optional[int] = ..., follow_symlinks: bool = ...) -> None: ...
def chmod(path: _FdOrAnyPath, mode: int, *, dir_fd: Optional[int] = ..., follow_symlinks: bool = ...) -> None: ...
if sys.platform != 'win32':
def chflags(path: _PathType, flags: int, follow_symlinks: bool = ...) -> None: ... # some flavors of Unix
def chown(path: _FdOrPathType, uid: int, gid: int, *, dir_fd: Optional[int] = ..., follow_symlinks: bool = ...) -> None: ... # Unix only
def chflags(path: AnyPath, flags: int, follow_symlinks: bool = ...) -> None: ... # some flavors of Unix
def chown(path: _FdOrAnyPath, uid: int, gid: int, *, dir_fd: Optional[int] = ..., follow_symlinks: bool = ...) -> None: ... # Unix only
if sys.platform != 'win32':
# Unix only
def chroot(path: _PathType) -> None: ...
def lchflags(path: _PathType, flags: int) -> None: ...
def lchmod(path: _PathType, mode: int) -> None: ...
def lchown(path: _PathType, uid: int, gid: int) -> None: ...
def chroot(path: AnyPath) -> None: ...
def lchflags(path: AnyPath, flags: int) -> None: ...
def lchmod(path: AnyPath, mode: int) -> None: ...
def lchown(path: AnyPath, uid: int, gid: int) -> None: ...
def link(
src: _PathType,
dst: _PathType,
src: AnyPath,
dst: AnyPath,
*,
src_dir_fd: Optional[int] = ...,
dst_dir_fd: Optional[int] = ...,
follow_symlinks: bool = ...,
) -> None: ...
def lstat(path: _PathType, *, dir_fd: Optional[int] = ...) -> stat_result: ...
def mkdir(path: _PathType, mode: int = ..., *, dir_fd: Optional[int] = ...) -> None: ...
def lstat(path: AnyPath, *, dir_fd: Optional[int] = ...) -> stat_result: ...
def mkdir(path: AnyPath, mode: int = ..., *, dir_fd: Optional[int] = ...) -> None: ...
if sys.platform != 'win32':
def mkfifo(path: _PathType, mode: int = ..., *, dir_fd: Optional[int] = ...) -> None: ... # Unix only
def makedirs(name: _PathType, mode: int = ..., exist_ok: bool = ...) -> None: ...
def mkfifo(path: AnyPath, mode: int = ..., *, dir_fd: Optional[int] = ...) -> None: ... # Unix only
def makedirs(name: AnyPath, mode: int = ..., exist_ok: bool = ...) -> None: ...
if sys.platform != "win32":
def mknod(path: _PathType, mode: int = ..., device: int = ..., *, dir_fd: Optional[int] = ...) -> None: ...
def mknod(path: AnyPath, mode: int = ..., device: int = ..., *, dir_fd: Optional[int] = ...) -> None: ...
def major(__device: int) -> int: ...
def minor(__device: int) -> int: ...
def makedev(__major: int, __minor: int) -> int: ...
def pathconf(path: _FdOrPathType, name: Union[str, int]) -> int: ... # Unix only
def pathconf(path: _FdOrAnyPath, name: Union[str, int]) -> int: ... # Unix only
if sys.version_info >= (3, 6):
def readlink(path: Union[AnyStr, PathLike[AnyStr]], *, dir_fd: Optional[int] = ...) -> AnyStr: ...
else:
def readlink(path: AnyStr, *, dir_fd: Optional[int] = ...) -> AnyStr: ...
def remove(path: _PathType, *, dir_fd: Optional[int] = ...) -> None: ...
def removedirs(name: _PathType) -> None: ...
def rename(src: _PathType, dst: _PathType, *, src_dir_fd: Optional[int] = ..., dst_dir_fd: Optional[int] = ...) -> None: ...
def renames(old: _PathType, new: _PathType) -> None: ...
def replace(src: _PathType, dst: _PathType, *, src_dir_fd: Optional[int] = ..., dst_dir_fd: Optional[int] = ...) -> None: ...
def rmdir(path: _PathType, *, dir_fd: Optional[int] = ...) -> None: ...
def remove(path: AnyPath, *, dir_fd: Optional[int] = ...) -> None: ...
def removedirs(name: AnyPath) -> None: ...
def rename(src: AnyPath, dst: AnyPath, *, src_dir_fd: Optional[int] = ..., dst_dir_fd: Optional[int] = ...) -> None: ...
def renames(old: AnyPath, new: AnyPath) -> None: ...
def replace(src: AnyPath, dst: AnyPath, *, src_dir_fd: Optional[int] = ..., dst_dir_fd: Optional[int] = ...) -> None: ...
def rmdir(path: AnyPath, *, dir_fd: Optional[int] = ...) -> None: ...
if sys.version_info >= (3, 7):
class _ScandirIterator(Iterator[DirEntry[AnyStr]], ContextManager[_ScandirIterator[AnyStr]]):
def __next__(self) -> DirEntry[AnyStr]: ...
@@ -516,27 +516,27 @@ else:
def scandir(path: None = ...) -> Iterator[DirEntry[str]]: ...
@overload
def scandir(path: AnyStr) -> Iterator[DirEntry[AnyStr]]: ...
def stat(path: _FdOrPathType, *, dir_fd: Optional[int] = ..., follow_symlinks: bool = ...) -> stat_result: ...
def stat(path: _FdOrAnyPath, *, dir_fd: Optional[int] = ..., follow_symlinks: bool = ...) -> stat_result: ...
if sys.version_info < (3, 7):
@overload
def stat_float_times() -> bool: ...
@overload
def stat_float_times(__newvalue: bool) -> None: ...
if sys.platform != 'win32':
def statvfs(path: _FdOrPathType) -> statvfs_result: ... # Unix only
def statvfs(path: _FdOrAnyPath) -> statvfs_result: ... # Unix only
def symlink(
src: _PathType,
dst: _PathType,
src: AnyPath,
dst: AnyPath,
target_is_directory: bool = ...,
*,
dir_fd: Optional[int] = ...,
) -> None: ...
if sys.platform != 'win32':
def sync() -> None: ... # Unix only
def truncate(path: _FdOrPathType, length: int) -> None: ... # Unix only up to version 3.4
def unlink(path: _PathType, *, dir_fd: Optional[int] = ...) -> None: ...
def truncate(path: _FdOrAnyPath, length: int) -> None: ... # Unix only up to version 3.4
def unlink(path: AnyPath, *, dir_fd: Optional[int] = ...) -> None: ...
def utime(
path: _FdOrPathType,
path: _FdOrAnyPath,
times: Optional[Union[Tuple[int, int], Tuple[float, float]]] = ...,
*,
ns: Tuple[int, int] = ...,
@@ -574,29 +574,29 @@ if sys.platform != 'win32':
onerror: Optional[_OnError] = ..., *, follow_symlinks: bool = ...,
dir_fd: Optional[int] = ...) -> Iterator[Tuple[str, List[str], List[str], int]]: ...
if sys.platform == "linux":
def getxattr(path: _FdOrPathType, attribute: _PathType, *, follow_symlinks: bool = ...) -> bytes: ...
def listxattr(path: _FdOrPathType, *, follow_symlinks: bool = ...) -> List[str]: ...
def removexattr(path: _FdOrPathType, attribute: _PathType, *, follow_symlinks: bool = ...) -> None: ...
def setxattr(path: _FdOrPathType, attribute: _PathType, value: bytes, flags: int = ..., *,
def getxattr(path: _FdOrAnyPath, attribute: AnyPath, *, follow_symlinks: bool = ...) -> bytes: ...
def listxattr(path: _FdOrAnyPath, *, follow_symlinks: bool = ...) -> List[str]: ...
def removexattr(path: _FdOrAnyPath, attribute: AnyPath, *, follow_symlinks: bool = ...) -> None: ...
def setxattr(path: _FdOrAnyPath, attribute: AnyPath, value: bytes, flags: int = ..., *,
follow_symlinks: bool = ...) -> None: ...
def abort() -> NoReturn: ...
# These are defined as execl(file, *args) but the first *arg is mandatory.
def execl(file: _PathType, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ...
def execlp(file: _PathType, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ...
def execl(file: AnyPath, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ...
def execlp(file: AnyPath, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ...
# These are: execle(file, *args, env) but env is pulled from the last element of the args.
def execle(file: _PathType, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ...
def execlpe(file: _PathType, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ...
def execle(file: AnyPath, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ...
def execlpe(file: AnyPath, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ...
# The docs say `args: tuple or list of strings`
# The implementation enforces tuple or list so we can't use Sequence.
_ExecVArgs = Union[Tuple[Union[bytes, Text], ...], List[bytes], List[Text], List[Union[bytes, Text]]]
_ExecEnv = Union[Mapping[bytes, Union[bytes, str]], Mapping[str, Union[bytes, str]]]
def execv(__path: _PathType, __argv: _ExecVArgs) -> NoReturn: ...
def execve(path: _FdOrPathType, argv: _ExecVArgs, env: _ExecEnv) -> NoReturn: ...
def execvp(file: _PathType, args: _ExecVArgs) -> NoReturn: ...
def execvpe(file: _PathType, args: _ExecVArgs, env: _ExecEnv) -> NoReturn: ...
def execv(__path: AnyPath, __argv: _ExecVArgs) -> NoReturn: ...
def execve(path: _FdOrAnyPath, argv: _ExecVArgs, env: _ExecEnv) -> NoReturn: ...
def execvp(file: AnyPath, args: _ExecVArgs) -> NoReturn: ...
def execvpe(file: AnyPath, args: _ExecVArgs, env: _ExecEnv) -> NoReturn: ...
def _exit(status: int) -> NoReturn: ...
def kill(__pid: int, __signal: int) -> None: ...
@@ -613,29 +613,29 @@ class _wrap_close(_TextIOWrapper):
def close(self) -> Optional[int]: ... # type: ignore
def popen(cmd: str, mode: str = ..., buffering: int = ...) -> _wrap_close: ...
def spawnl(mode: int, file: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ...
def spawnle(mode: int, file: _PathType, arg0: Union[bytes, Text],
def spawnl(mode: int, file: AnyPath, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ...
def spawnle(mode: int, file: AnyPath, arg0: Union[bytes, Text],
*args: Any) -> int: ... # Imprecise sig
if sys.platform != "win32":
def spawnv(mode: int, file: _PathType, args: List[Union[bytes, Text]]) -> int: ...
def spawnve(mode: int, file: _PathType, args: List[Union[bytes, Text]],
def spawnv(mode: int, file: AnyPath, args: List[Union[bytes, Text]]) -> int: ...
def spawnve(mode: int, file: AnyPath, args: List[Union[bytes, Text]],
env: _ExecEnv) -> int: ...
else:
def spawnv(__mode: int, __path: _PathType, __argv: List[Union[bytes, Text]]) -> int: ...
def spawnve(__mode: int, __path: _PathType, __argv: List[Union[bytes, Text]],
def spawnv(__mode: int, __path: AnyPath, __argv: List[Union[bytes, Text]]) -> int: ...
def spawnve(__mode: int, __path: AnyPath, __argv: List[Union[bytes, Text]],
__env: _ExecEnv) -> int: ...
def system(command: _PathType) -> int: ...
def system(command: AnyPath) -> int: ...
def times() -> times_result: ...
def waitpid(__pid: int, __options: int) -> Tuple[int, int]: ...
if sys.platform == 'win32':
def startfile(path: _PathType, operation: Optional[str] = ...) -> None: ...
def startfile(path: AnyPath, operation: Optional[str] = ...) -> None: ...
else:
# Unix only
def spawnlp(mode: int, file: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ...
def spawnlpe(mode: int, file: _PathType, arg0: Union[bytes, Text], *args: Any) -> int: ... # Imprecise signature
def spawnvp(mode: int, file: _PathType, args: List[Union[bytes, Text]]) -> int: ...
def spawnvpe(mode: int, file: _PathType, args: List[Union[bytes, Text]], env: _ExecEnv) -> int: ...
def spawnlp(mode: int, file: AnyPath, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ...
def spawnlpe(mode: int, file: AnyPath, arg0: Union[bytes, Text], *args: Any) -> int: ... # Imprecise signature
def spawnvp(mode: int, file: AnyPath, args: List[Union[bytes, Text]]) -> int: ...
def spawnvpe(mode: int, file: AnyPath, args: List[Union[bytes, Text]], env: _ExecEnv) -> int: ...
def wait() -> Tuple[int, int]: ... # Unix only
from posix import waitid_result
def waitid(idtype: int, ident: int, options: int) -> waitid_result: ...

View File

@@ -1,23 +1,14 @@
# NB: path.pyi and stdlib/2 and stdlib/3 must remain consistent!
# Stubs for os.path
# Ron Murawski <ron@horizonchess.com>
import os
import sys
from typing import overload, List, Any, AnyStr, Sequence, Tuple, TypeVar, Union, Text, Callable, Optional
from typing import overload, List, Any, AnyStr, Sequence, Tuple, TypeVar, Text, Callable, Optional
from genericpath import exists as exists
from _typeshed import StrPath, BytesPath, AnyPath
_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: bool
@@ -90,31 +81,31 @@ 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: ...
# 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[AnyPath]) -> 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(m: Sequence[_PathType]) -> Any: ...
def commonprefix(m: Sequence[AnyPath]) -> Any: ...
def lexists(path: _PathType) -> bool: ...
def lexists(path: AnyPath) -> bool: ...
# These return float if os.stat_float_times() == True,
# but int is a subclass of float.
def getatime(filename: _PathType) -> float: ...
def getmtime(filename: _PathType) -> float: ...
def getctime(filename: _PathType) -> float: ...
def getatime(filename: AnyPath) -> float: ...
def getmtime(filename: AnyPath) -> float: ...
def getctime(filename: AnyPath) -> float: ...
def getsize(filename: _PathType) -> int: ...
def isabs(s: _PathType) -> bool: ...
def isfile(path: _PathType) -> bool: ...
def isdir(s: _PathType) -> bool: ...
def islink(path: _PathType) -> bool: ...
def ismount(path: _PathType) -> bool: ...
def getsize(filename: AnyPath) -> int: ...
def isabs(s: AnyPath) -> bool: ...
def isfile(path: AnyPath) -> bool: ...
def isdir(s: AnyPath) -> bool: ...
def islink(path: AnyPath) -> bool: ...
def ismount(path: AnyPath) -> bool: ...
if sys.version_info < (3, 0):
# Make sure signatures are disjunct, and allow combinations of bytes and unicode.
@@ -124,28 +115,27 @@ if sys.version_info < (3, 0):
@overload
def join(__p1: bytes, *p: bytes) -> bytes: ...
@overload
def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ...
def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: AnyPath) -> Text: ...
@overload
def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ...
def join(__p1: bytes, __p2: bytes, __p3: Text, *p: AnyPath) -> Text: ...
@overload
def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ...
def join(__p1: bytes, __p2: Text, *p: AnyPath) -> Text: ...
@overload
def join(__p1: Text, *p: _PathType) -> Text: ...
def join(__p1: Text, *p: AnyPath) -> 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(a: _StrPath, *paths: _StrPath) -> Text: ...
def join(a: StrPath, *paths: StrPath) -> Text: ...
@overload
def join(a: _BytesPath, *paths: _BytesPath) -> bytes: ...
def join(a: BytesPath, *paths: BytesPath) -> bytes: ...
else:
def join(a: AnyStr, *paths: AnyStr) -> AnyStr: ...
@overload
def relpath(path: _BytesPath, start: Optional[_BytesPath] = ...) -> bytes: ...
def relpath(path: BytesPath, start: Optional[BytesPath] = ...) -> bytes: ...
@overload
def relpath(path: _StrPath, start: Optional[_StrPath] = ...) -> Text: ...
def relpath(path: StrPath, start: Optional[StrPath] = ...) -> Text: ...
def samefile(f1: _PathType, f2: _PathType) -> bool: ...
def samefile(f1: AnyPath, f2: AnyPath) -> bool: ...
def sameopenfile(fp1: int, fp2: int) -> bool: ...
def samestat(s1: os.stat_result, s2: os.stat_result) -> bool: ...