From 254c52c215dd0b352a105cc72a2f145d54db0da2 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 23 May 2017 16:18:40 -0700 Subject: [PATCH 1/2] use PEP 526-style annotations in os stubs And fix some TODOs. This will help me check these stubs with my stubcheck tool, and is better for consistency anyway. --- stdlib/2/os/__init__.pyi | 155 ++++++++++++------------ stdlib/3/os/__init__.pyi | 255 ++++++++++++++++++++------------------- 2 files changed, 206 insertions(+), 204 deletions(-) diff --git a/stdlib/2/os/__init__.pyi b/stdlib/2/os/__init__.pyi index 881bcf73fc26..4bec1fd8527c 100644 --- a/stdlib/2/os/__init__.pyi +++ b/stdlib/2/os/__init__.pyi @@ -11,93 +11,94 @@ from mypy_extensions import NoReturn _T = TypeVar('_T') -SEEK_SET = 0 -SEEK_CUR = 0 -SEEK_END = 0 +SEEK_SET: int +SEEK_CUR: int +SEEK_END: int # More constants, copied from stdlib/3/os/__init__.pyi -O_RDONLY = 0 -O_WRONLY = 0 -O_RDWR = 0 -O_APPEND = 0 -O_CREAT = 0 -O_EXCL = 0 -O_TRUNC = 0 -O_DSYNC = 0 # Unix only -O_RSYNC = 0 # Unix only -O_SYNC = 0 # Unix only -O_NDELAY = 0 # Unix only -O_NONBLOCK = 0 # Unix only -O_NOCTTY = 0 # Unix only -O_SHLOCK = 0 # Unix only -O_EXLOCK = 0 # Unix only -O_BINARY = 0 # Windows only -O_NOINHERIT = 0 # Windows only -O_SHORT_LIVED = 0 # Windows only -O_TEMPORARY = 0 # Windows only -O_RANDOM = 0 # Windows only -O_SEQUENTIAL = 0 # Windows only -O_TEXT = 0 # Windows only -O_ASYNC = 0 # Gnu extension if in C library -O_DIRECT = 0 # Gnu extension if in C library -O_DIRECTORY = 0 # Gnu extension if in C library -O_NOFOLLOW = 0 # Gnu extension if in C library -O_NOATIME = 0 # Gnu extension if in C library -O_LARGEFILE = 0 # Gnu extension if in C library +O_RDONLY: int +O_WRONLY: int +O_RDWR: int +O_APPEND: int +O_CREAT: int +O_EXCL: int +O_TRUNC: int +O_DSYNC: int # Unix only +O_RSYNC: int # Unix only +O_SYNC: int # Unix only +O_NDELAY: int # Unix only +O_NONBLOCK: int # Unix only +O_NOCTTY: int # Unix only +O_SHLOCK: int # Unix only +O_EXLOCK: int # Unix only +O_BINARY: int # Windows only +O_NOINHERIT: int # Windows only +O_SHORT_LIVED: int # Windows only +O_TEMPORARY: int # Windows only +O_RANDOM: int # Windows only +O_SEQUENTIAL: int # Windows only +O_TEXT: int # Windows only +O_ASYNC: int # Gnu extension if in C library +O_DIRECT: int # Gnu extension if in C library +O_DIRECTORY: int # Gnu extension if in C library +O_NOFOLLOW: int # Gnu extension if in C library +O_NOATIME: int # Gnu extension if in C library +O_LARGEFILE: int # Gnu extension if in C library -curdir = ... # type: str -pardir = ... # type: str -sep = ... # type: str -altsep = ... # type: str -extsep = ... # type: str -pathsep = ... # type: str -defpath = ... # type: str -linesep = ... # type: str -devnull = ... # type: str -name = ... # type: str +curdir: str +pardir: str +sep: str +altsep: str +extsep: str +pathsep: str +defpath: str +linesep: str +devnull: str +name: str -F_OK = 0 -R_OK = 0 -W_OK = 0 -X_OK = 0 +F_OK: int +R_OK: int +W_OK: int +X_OK: int class _Environ(MutableMapping[AnyStr, AnyStr], Generic[AnyStr]): def copy(self) -> Dict[AnyStr, AnyStr]: ... -environ = ... # type: _Environ[str] +environ: _Environ[str] -confstr_names = ... # type: Dict[str, int] # Unix only -pathconf_names = ... # type: Dict[str, int] # Unix only -sysconf_names = ... # type: Dict[str, int] # Unix only +confstr_names: Dict[str, int] # Unix only +pathconf_names: Dict[str, int] # Unix only +sysconf_names: Dict[str, int] # Unix only -EX_OK = 0 # Unix only -EX_USAGE = 0 # Unix only -EX_DATAERR = 0 # Unix only -EX_NOINPUT = 0 # Unix only -EX_NOUSER = 0 # Unix only -EX_NOHOST = 0 # Unix only -EX_UNAVAILABLE = 0 # Unix only -EX_SOFTWARE = 0 # Unix only -EX_OSERR = 0 # Unix only -EX_OSFILE = 0 # Unix only -EX_CANTCREAT = 0 # Unix only -EX_IOERR = 0 # Unix only -EX_TEMPFAIL = 0 # Unix only -EX_PROTOCOL = 0 # Unix only -EX_NOPERM = 0 # Unix only -EX_CONFIG = 0 # Unix only -P_NOWAIT = 0 -P_NOWAITO = 0 -P_WAIT = 0 -# P_DETACH = 0 # Windows only -# P_OVERLAY = 0 # Windows only +EX_OK: int # Unix only +EX_USAGE: int # Unix only +EX_DATAERR: int # Unix only +EX_NOINPUT: int # Unix only +EX_NOUSER: int # Unix only +EX_NOHOST: int # Unix only +EX_UNAVAILABLE: int # Unix only +EX_SOFTWARE: int # Unix only +EX_OSERR: int # Unix only +EX_OSFILE: int # Unix only +EX_CANTCREAT: int # Unix only +EX_IOERR: int # Unix only +EX_TEMPFAIL: int # Unix only +EX_PROTOCOL: int # Unix only +EX_NOPERM: int # Unix only +EX_CONFIG: int # Unix only +P_NOWAIT: int +P_NOWAITO: int +P_WAIT: int +if sys.platform == 'win32': + P_DETACH: int # Windows only + P_OVERLAY: int # Windows only # wait()/waitpid() options -WNOHANG = 0 # Unix only -WCONTINUED = 0 # some Unix systems -WUNTRACED = 0 # Unix only +WNOHANG: int # Unix only +WCONTINUED: int # some Unix systems +WUNTRACED: int # Unix only -TMP_MAX = 0 # Undocumented, but used by tempfile +TMP_MAX: int # Undocumented, but used by tempfile _PathType = Union[bytes, Text] _StatVFS = NamedTuple('_StatVFS', [('f_bsize', int), ('f_frsize', int), ('f_blocks', int), ('f_bfree', int), ('f_bavail', int), ('f_files', int), @@ -275,6 +276,6 @@ def tmpfile() -> IO[Any]: ... def tmpnam() -> str: ... def tempnam(dir: str = ..., prefix: str = ...) -> str: ... -P_ALL = 0 -WEXITED = 0 -WNOWAIT = 0 +P_ALL: int +WEXITED: int +WNOWAIT: int diff --git a/stdlib/3/os/__init__.pyi b/stdlib/3/os/__init__.pyi index 9cda30259549..91271fba07e3 100644 --- a/stdlib/3/os/__init__.pyi +++ b/stdlib/3/os/__init__.pyi @@ -17,102 +17,103 @@ _T = TypeVar('_T') # ----- os variables ----- -supports_bytes_environ = False # TODO: True when bytes implemented? +supports_bytes_environ: bool if sys.version_info >= (3, 3): - supports_dir_fd = ... # type: Set[Callable[..., Any]] - supports_fd = ... # type: Set[Callable[..., Any]] - supports_effective_ids = ... # type: Set[Callable[..., Any]] - supports_follow_symlinks = ... # type: Set[Callable[..., Any]] - -SEEK_SET = 0 -SEEK_CUR = 0 -SEEK_END = 0 - -O_RDONLY = 0 -O_WRONLY = 0 -O_RDWR = 0 -O_APPEND = 0 -O_CREAT = 0 -O_EXCL = 0 -O_TRUNC = 0 -O_DSYNC = 0 # Unix only -O_RSYNC = 0 # Unix only -O_SYNC = 0 # Unix only -O_NDELAY = 0 # Unix only -O_NONBLOCK = 0 # Unix only -O_NOCTTY = 0 # Unix only -O_SHLOCK = 0 # Unix only -O_EXLOCK = 0 # Unix only -O_BINARY = 0 # Windows only -O_NOINHERIT = 0 # Windows only -O_SHORT_LIVED = 0 # Windows only -O_TEMPORARY = 0 # Windows only -O_RANDOM = 0 # Windows only -O_SEQUENTIAL = 0 # Windows only -O_TEXT = 0 # Windows only -O_ASYNC = 0 # Gnu extension if in C library -O_DIRECT = 0 # Gnu extension if in C library -O_DIRECTORY = 0 # Gnu extension if in C library -O_NOFOLLOW = 0 # Gnu extension if in C library -O_NOATIME = 0 # Gnu extension if in C library - -curdir = ... # type: str -pardir = ... # type: str -sep = ... # type: str -altsep = ... # type: str -extsep = ... # type: str -pathsep = ... # type: str -defpath = ... # type: str -linesep = ... # type: str -devnull = ... # type: str -name = ... # type: str - -F_OK = 0 -R_OK = 0 -W_OK = 0 -X_OK = 0 + supports_dir_fd: Set[Callable[..., Any]] + supports_fd: Set[Callable[..., Any]] + supports_effective_ids: Set[Callable[..., Any]] + supports_follow_symlinks: Set[Callable[..., Any]] + +SEEK_SET: int +SEEK_CUR: int +SEEK_END: int + +O_RDONLY: int +O_WRONLY: int +O_RDWR: int +O_APPEND: int +O_CREAT: int +O_EXCL: int +O_TRUNC: int +O_DSYNC: int # Unix only +O_RSYNC: int # Unix only +O_SYNC: int # Unix only +O_NDELAY: int # Unix only +O_NONBLOCK: int # Unix only +O_NOCTTY: int # Unix only +O_SHLOCK: int # Unix only +O_EXLOCK: int # Unix only +O_BINARY: int # Windows only +O_NOINHERIT: int # Windows only +O_SHORT_LIVED: int # Windows only +O_TEMPORARY: int # Windows only +O_RANDOM: int # Windows only +O_SEQUENTIAL: int # Windows only +O_TEXT: int # Windows only +O_ASYNC: int # Gnu extension if in C library +O_DIRECT: int # Gnu extension if in C library +O_DIRECTORY: int # Gnu extension if in C library +O_NOFOLLOW: int # Gnu extension if in C library +O_NOATIME: int # Gnu extension if in C library + +curdir: str +pardir: str +sep: str +altsep: str +extsep: str +pathsep: str +defpath: str +linesep: str +devnull: str +name: str + +F_OK: int +R_OK: int +W_OK: int +X_OK: int class _Environ(MutableMapping[AnyStr, AnyStr], Generic[AnyStr]): def copy(self) -> Dict[AnyStr, AnyStr]: ... -environ = ... # type: _Environ[str] -environb = ... # type: _Environ[bytes] - -confstr_names = ... # type: Dict[str, int] # Unix only -pathconf_names = ... # type: Dict[str, int] # Unix only -sysconf_names = ... # type: Dict[str, int] # Unix only - -EX_OK = 0 # Unix only -EX_USAGE = 0 # Unix only -EX_DATAERR = 0 # Unix only -EX_NOINPUT = 0 # Unix only -EX_NOUSER = 0 # Unix only -EX_NOHOST = 0 # Unix only -EX_UNAVAILABLE = 0 # Unix only -EX_SOFTWARE = 0 # Unix only -EX_OSERR = 0 # Unix only -EX_OSFILE = 0 # Unix only -EX_CANTCREAT = 0 # Unix only -EX_IOERR = 0 # Unix only -EX_TEMPFAIL = 0 # Unix only -EX_PROTOCOL = 0 # Unix only -EX_NOPERM = 0 # Unix only -EX_CONFIG = 0 # Unix only -EX_NOTFOUND = 0 # Unix only - -P_NOWAIT = 0 -P_NOWAITO = 0 -P_WAIT = 0 -# P_DETACH = 0 # Windows only -# P_OVERLAY = 0 # Windows only +environ: _Environ[str] +environb: _Environ[bytes] + +confstr_names: Dict[str, int] # Unix only +pathconf_names: Dict[str, int] # Unix only +sysconf_names: Dict[str, int] # Unix only + +EX_OK: int # Unix only +EX_USAGE: int # Unix only +EX_DATAERR: int # Unix only +EX_NOINPUT: int # Unix only +EX_NOUSER: int # Unix only +EX_NOHOST: int # Unix only +EX_UNAVAILABLE: int # Unix only +EX_SOFTWARE: int # Unix only +EX_OSERR: int # Unix only +EX_OSFILE: int # Unix only +EX_CANTCREAT: int # Unix only +EX_IOERR: int # Unix only +EX_TEMPFAIL: int # Unix only +EX_PROTOCOL: int # Unix only +EX_NOPERM: int # Unix only +EX_CONFIG: int # Unix only +EX_NOTFOUND: int # Unix only + +P_NOWAIT: int +P_NOWAITO: int +P_WAIT: int +if sys.platform == 'win32': + P_DETACH: int # Windows only + P_OVERLAY: int # Windows only # wait()/waitpid() options -WNOHANG = 0 # Unix only -# WCONTINUED = 0 # some Unix systems -# WUNTRACED = 0 # Unix only +WNOHANG: int # Unix only +WCONTINUED: int # some Unix systems +WUNTRACED: int # Unix only -TMP_MAX = 0 # Undocumented, but used by tempfile +TMP_MAX: int # Undocumented, but used by tempfile # ----- os classes (structures) ----- if sys.version_info >= (3, 6): @@ -125,8 +126,8 @@ if sys.version_info >= (3, 6): # This is what the scandir interator yields # The constructor is hidden - name = ... # type: AnyStr - path = ... # type: AnyStr + name: AnyStr + path: AnyStr def inode(self) -> int: ... def is_dir(self, follow_symlinks: bool = ...) -> bool: ... def is_file(self, follow_symlinks: bool = ...) -> bool: ... @@ -139,8 +140,8 @@ elif sys.version_info >= (3, 5): # This is what the scandir interator yields # The constructor is hidden - name = ... # type: AnyStr - path = ... # type: AnyStr + name: AnyStr + path: AnyStr def inode(self) -> int: ... def is_dir(self, follow_symlinks: bool = ...) -> bool: ... def is_file(self, follow_symlinks: bool = ...) -> bool: ... @@ -155,53 +156,53 @@ class stat_result: # st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, # st_ctime. More items may be added at the end by some implementations. - st_mode = 0 # protection bits, - st_ino = 0 # inode number, - st_dev = 0 # device, - st_nlink = 0 # number of hard links, - st_uid = 0 # user id of owner, - st_gid = 0 # group id of owner, - st_size = 0 # size of file, in bytes, - st_atime = 0.0 # time of most recent access, - st_mtime = 0.0 # time of most recent content modification, - st_ctime = 0.0 # platform dependent (time of most recent metadata change on Unix, or the time of creation on Windows) + st_mode: int # protection bits, + st_ino: int # inode number, + st_dev: int # device, + st_nlink: int # number of hard links, + st_uid: int # user id of owner, + st_gid: int # group id of owner, + st_size: int # size of file, in bytes, + st_atime: float # time of most recent access, + st_mtime: float # time of most recent content modification, + st_ctime: float # platform dependent (time of most recent metadata change on Unix, or the time of creation on Windows) if sys.version_info >= (3, 3): - st_atime_ns = 0 # time of most recent access, in nanoseconds - st_mtime_ns = 0 # time of most recent content modification in nanoseconds - st_ctime_ns = 0 # platform dependent (time of most recent metadata change on Unix, or the time of creation on Windows) in nanoseconds + st_atime_ns: int # time of most recent access, in nanoseconds + st_mtime_ns: int # time of most recent content modification in nanoseconds + st_ctime_ns: int # platform dependent (time of most recent metadata change on Unix, or the time of creation on Windows) in nanoseconds # not documented def __init__(self, tuple: Tuple[int, ...]) -> None: ... # On some Unix systems (such as Linux), the following attributes may also # be available: - st_blocks = 0 # number of blocks allocated for file - st_blksize = 0 # filesystem blocksize - st_rdev = 0 # type of device if an inode device - st_flags = 0 # user defined flags for file + st_blocks: int # number of blocks allocated for file + st_blksize: int # filesystem blocksize + st_rdev: int # type of device if an inode device + st_flags: int # user defined flags for file # On other Unix systems (such as FreeBSD), the following attributes may be # available (but may be only filled out if root tries to use them): - st_gen = 0 # file generation number - st_birthtime = 0 # time of file creation + st_gen: int # file generation number + st_birthtime: int # time of file creation # On Mac OS systems, the following attributes may also be available: - st_rsize = 0 - st_creator = 0 - st_type = 0 + st_rsize: int + st_creator: int + st_type: int class statvfs_result: # Unix only - f_bsize = 0 - f_frsize = 0 - f_blocks = 0 - f_bfree = 0 - f_bavail = 0 - f_files = 0 - f_ffree = 0 - f_favail = 0 - f_flag = 0 - f_namemax = 0 + f_bsize: int + f_frsize: int + f_blocks: int + f_bfree: int + f_bavail: int + f_files: int + f_ffree: int + f_favail: int + f_flag: int + f_namemax: int # ----- os function stubs ----- if sys.version_info >= (3, 6): @@ -417,11 +418,11 @@ def urandom(n: int) -> bytes: ... def sched_getaffinity(id: int) -> Set[int]: ... class waitresult: - si_pid = 0 + si_pid: int def waitid(idtype: int, id: int, options: int) -> waitresult: ... -P_ALL = 0 -WEXITED = 0 -WNOWAIT = 0 +P_ALL: int +WEXITED: int +WNOWAIT: int if sys.version_info >= (3, 3): def sync() -> None: ... # Unix only From 803b32af1749227fe22470d14a47ed66d1c5001c Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 23 May 2017 17:02:39 -0700 Subject: [PATCH 2/2] make python 2 and 3 os stubs identical In preparation for merging. I avoided making other changes to the bugs but added some TODOs for missing stuff that I noticed. --- stdlib/2/os/__init__.pyi | 257 +++++++++++++++++++++++++++++++++++---- stdlib/3/os/__init__.pyi | 117 ++++++++++++------ 2 files changed, 312 insertions(+), 62 deletions(-) diff --git a/stdlib/2/os/__init__.pyi b/stdlib/2/os/__init__.pyi index 4bec1fd8527c..de5b77c266b0 100644 --- a/stdlib/2/os/__init__.pyi +++ b/stdlib/2/os/__init__.pyi @@ -1,6 +1,10 @@ +# Stubs for os +# Ron Murawski + # created from https://docs.python.org/2/library/os.html from builtins import OSError as error +from io import TextIOWrapper as _TextIOWrapper import sys from typing import ( Mapping, MutableMapping, Dict, List, Any, Tuple, Iterator, overload, Union, AnyStr, @@ -11,11 +15,21 @@ from mypy_extensions import NoReturn _T = TypeVar('_T') +# ----- os variables ----- + +if sys.version_info >= (3, 2): + supports_bytes_environ: bool + +if sys.version_info >= (3, 3): + supports_dir_fd: Set[Callable[..., Any]] + supports_fd: Set[Callable[..., Any]] + supports_effective_ids: Set[Callable[..., Any]] + supports_follow_symlinks: Set[Callable[..., Any]] + SEEK_SET: int SEEK_CUR: int SEEK_END: int -# More constants, copied from stdlib/3/os/__init__.pyi O_RDONLY: int O_WRONLY: int O_RDWR: int @@ -65,6 +79,8 @@ class _Environ(MutableMapping[AnyStr, AnyStr], Generic[AnyStr]): def copy(self) -> Dict[AnyStr, AnyStr]: ... environ: _Environ[str] +if sys.version_info >= (3, 2): + environb: _Environ[bytes] confstr_names: Dict[str, int] # Unix only pathconf_names: Dict[str, int] # Unix only @@ -86,6 +102,8 @@ EX_TEMPFAIL: int # Unix only EX_PROTOCOL: int # Unix only EX_NOPERM: int # Unix only EX_CONFIG: int # Unix only +EX_NOTFOUND: int # Unix only + P_NOWAIT: int P_NOWAITO: int P_WAIT: int @@ -99,11 +117,118 @@ WCONTINUED: int # some Unix systems WUNTRACED: int # Unix only TMP_MAX: int # Undocumented, but used by tempfile -_PathType = Union[bytes, Text] -_StatVFS = NamedTuple('_StatVFS', [('f_bsize', int), ('f_frsize', int), ('f_blocks', int), - ('f_bfree', int), ('f_bavail', int), ('f_files', int), - ('f_ffree', int), ('f_favail', int), ('f_flag', int), - ('f_namemax', int)]) + +# ----- os classes (structures) ----- +if sys.version_info >= (3, 6): + from builtins import _PathLike as PathLike # See comment in builtins + +_PathType = path._PathType + +if sys.version_info >= (3, 6): + class DirEntry(PathLike[AnyStr]): + # This is what the scandir interator yields + # The constructor is hidden + + name: AnyStr + path: AnyStr + def inode(self) -> int: ... + def is_dir(self, follow_symlinks: bool = ...) -> bool: ... + def is_file(self, follow_symlinks: bool = ...) -> bool: ... + def is_symlink(self) -> bool: ... + def stat(self) -> stat_result: ... + + def __fspath__(self) -> AnyStr: ... +elif sys.version_info >= (3, 5): + class DirEntry(Generic[AnyStr]): + # This is what the scandir interator yields + # The constructor is hidden + + name: AnyStr + path: AnyStr + def inode(self) -> int: ... + def is_dir(self, follow_symlinks: bool = ...) -> bool: ... + def is_file(self, follow_symlinks: bool = ...) -> bool: ... + def is_symlink(self) -> bool: ... + def stat(self) -> stat_result: ... + + +class stat_result: + # For backward compatibility, the return value of stat() is also + # accessible as a tuple of at least 10 integers giving the most important + # (and portable) members of the stat structure, in the order st_mode, + # st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, + # st_ctime. More items may be added at the end by some implementations. + + st_mode: int # protection bits, + st_ino: int # inode number, + st_dev: int # device, + st_nlink: int # number of hard links, + st_uid: int # user id of owner, + st_gid: int # group id of owner, + st_size: int # size of file, in bytes, + st_atime: float # time of most recent access, + st_mtime: float # time of most recent content modification, + st_ctime: float # platform dependent (time of most recent metadata change on Unix, or the time of creation on Windows) + + if sys.version_info >= (3, 3): + st_atime_ns: int # time of most recent access, in nanoseconds + st_mtime_ns: int # time of most recent content modification in nanoseconds + st_ctime_ns: int # platform dependent (time of most recent metadata change on Unix, or the time of creation on Windows) in nanoseconds + + # not documented + def __init__(self, tuple: Tuple[int, ...]) -> None: ... + + # On some Unix systems (such as Linux), the following attributes may also + # be available: + st_blocks: int # number of blocks allocated for file + st_blksize: int # filesystem blocksize + st_rdev: int # type of device if an inode device + st_flags: int # user defined flags for file + + # On other Unix systems (such as FreeBSD), the following attributes may be + # available (but may be only filled out if root tries to use them): + st_gen: int # file generation number + st_birthtime: int # time of file creation + + # On Mac OS systems, the following attributes may also be available: + st_rsize: int + st_creator: int + st_type: int + +class statvfs_result: # Unix only + f_bsize: int + f_frsize: int + f_blocks: int + f_bfree: int + f_bavail: int + f_files: int + f_ffree: int + f_favail: int + f_flag: int + f_namemax: int + +# ----- os function stubs ----- +if sys.version_info >= (3, 6): + def fsencode(filename: Union[str, bytes, PathLike]) -> bytes: ... +elif sys.version_info >= (3, 2): + def fsencode(filename: Union[str, bytes]) -> bytes: ... + +if sys.version_info >= (3, 6): + def fsdecode(filename: Union[str, bytes, PathLike]) -> str: ... +elif sys.version_info >= (3, 2): + def fsdecode(filename: Union[str, bytes]) -> str: ... + +if sys.version_info >= (3, 6): + @overload + def fspath(path: str) -> str: ... + @overload + def fspath(path: bytes) -> bytes: ... + @overload + def fspath(path: PathLike) -> Any: ... + +if sys.version_info >= (3, 2): + def get_exec_path(env: Optional[Mapping[str, str]] = ...) -> List[str]: ... + # NOTE: get_exec_path(): returns List[bytes] when env not None def ctermid() -> str: ... # Unix only def getegid() -> int: ... # Unix only def geteuid() -> int: ... # Unix only @@ -139,12 +264,20 @@ def uname() -> Tuple[str, str, str, str, str]: ... # Unix only def getenv(key: Text) -> Optional[str]: ... @overload def getenv(key: Text, default: _T) -> Union[str, _T]: ... +if sys.version_info >= (3, 2): + def getenvb(key: bytes, default: bytes = ...) -> bytes: ... def putenv(key: Union[bytes, Text], value: Union[bytes, Text]) -> None: ... def unsetenv(key: Union[bytes, Text]) -> None: ... -def fdopen(fd: int, *args, **kwargs) -> IO[Any]: ... +if sys.version_info >= (3, 0): + def fdopen(fd: int, mode: str = ..., buffering: int = ..., encoding: str = ..., + errors: str = ..., newline: str = ..., closefd: bool = ...) -> IO[Any]: ... +else: + def fdopen(fd: int, mode: str = ..., bufsize: int = ...) -> IO[Any]: ... def close(fd: int) -> None: ... def closerange(fd_low: int, fd_high: int) -> None: ... +if sys.version_info >= (3, 0): + def device_encoding(fd: int) -> Optional[str]: ... def dup(fd: int) -> int: ... def dup2(fd: int, fd2: int) -> None: ... def fchmod(fd: int, mode: int) -> None: ... # Unix only @@ -152,7 +285,7 @@ def fchown(fd: int, uid: int, gid: int) -> None: ... # Unix only def fdatasync(fd: int) -> None: ... # Unix only, not Mac def fpathconf(fd: int, name: Union[str, int]) -> int: ... # Unix only def fstat(fd: int) -> Any: ... -def fstatvfs(fd: int) -> _StatVFS: ... # Unix only +def fstatvfs(fd: int) -> statvfs_result: ... # Unix only def fsync(fd: int) -> None: ... def ftruncate(fd: int, length: int) -> None: ... # Unix only def isatty(fd: int) -> bool: ... # Unix only @@ -169,7 +302,10 @@ def access(path: _PathType, mode: int) -> bool: ... def chdir(path: _PathType) -> None: ... def fchdir(fd: int) -> None: ... def getcwd() -> str: ... -def getcwdu() -> unicode: ... +if sys.version_info >= (3, 0): + def getcwdb() -> bytes: ... +else: + def getcwdu() -> unicode: ... def chflags(path: _PathType, flags: int) -> None: ... # Unix only def chroot(path: _PathType) -> None: ... # Unix only def chmod(path: _PathType, mode: int) -> None: ... @@ -178,7 +314,16 @@ def lchflags(path: _PathType, flags: int) -> None: ... # Unix only def lchmod(path: _PathType, mode: int) -> None: ... # Unix only def lchown(path: _PathType, uid: int, gid: int) -> None: ... # Unix only def link(src: _PathType, link_name: _PathType) -> None: ... -def listdir(path: AnyStr) -> List[AnyStr]: ... + +# TODO: support PathLike and fd arguments +if sys.version_info >= (3, 2): + @overload + def listdir(path: str = ...) -> List[str]: ... + @overload + def listdir(path: bytes) -> List[bytes]: ... +else: + def listdir(path: AnyStr) -> List[AnyStr]: ... + def lstat(path: _PathType) -> Any: ... def mkfifo(path: _PathType, mode: int = ...) -> None: ... # Unix only def mknod(filename: _PathType, mode: int = ..., device: int = ...) -> None: ... @@ -186,28 +331,56 @@ def major(device: int) -> int: ... def minor(device: int) -> int: ... def makedev(major: int, minor: int) -> int: ... def mkdir(path: _PathType, mode: int = ...) -> None: ... -def makedirs(path: _PathType, mode: int = ...) -> None: ... +if sys.version_info >= (3, 2): + def makedirs(path: _PathType, mode: int = ..., + exist_ok: bool = ...) -> None: ... +else: + def makedirs(path: _PathType, mode: int = ...) -> None: ... def pathconf(path: _PathType, name: Union[str, int]) -> int: ... # Unix only def readlink(path: AnyStr) -> AnyStr: ... def remove(path: _PathType) -> None: ... def removedirs(path: _PathType) -> None: ... def rename(src: _PathType, dst: _PathType) -> None: ... def renames(old: _PathType, new: _PathType) -> None: ... +if sys.version_info >= (3, 3): + def replace(src: _PathType, dst: _PathType) -> None: ... def rmdir(path: _PathType) -> None: ... +if sys.version_info >= (3, 5): + @overload + def scandir(path: str = ...) -> Iterator[DirEntry[str]]: ... + @overload + def scandir(path: bytes) -> Iterator[DirEntry[bytes]]: ... +# This cannot return stat_result because of the issues discussed in +# https://github.com/python/typeshed/pull/1103 def stat(path: _PathType) -> Any: ... @overload def stat_float_times(newvalue: bool = ...) -> None: ... @overload def stat_float_times() -> bool: ... -def statvfs(path: _PathType) -> _StatVFS: ... # Unix only -def symlink(source: _PathType, link_name: _PathType) -> None: ... +def statvfs(path: _PathType) -> statvfs_result: ... # Unix only +# TODO add dir_fd argument +if sys.version_info >= (3, 0): + def symlink(source: _PathType, link_name: _PathType, + target_is_directory: bool = ...) -> None: + ... # final argument in Windows only +else: + def symlink(source: _PathType, link_name: _PathType) -> None: ... def unlink(path: _PathType) -> None: ... -def utime(path: _PathType, times: Optional[Tuple[float, float]]) -> None: ... +# TODO: add ns, dir_fd, follow_symlinks argument +if sys.version_info >= (3, 0): + def utime(path: _PathType, times: Optional[Tuple[float, float]] = ...) -> None: ... +else: + def utime(path: _PathType, times: Optional[Tuple[float, float]]) -> None: ... -# TODO onerror: function from OSError to void -def walk(top: AnyStr, topdown: bool = ..., onerror: Any = ..., - followlinks: bool = ...) -> Iterator[Tuple[AnyStr, List[AnyStr], - List[AnyStr]]]: ... +if sys.version_info >= (3, 6): + def walk(top: Union[AnyStr, PathLike[AnyStr]], topdown: bool = ..., + onerror: Optional[Callable[[OSError], Any]] = ..., + followlinks: bool = ...) -> Iterator[Tuple[AnyStr, List[AnyStr], + List[AnyStr]]]: ... +else: + def walk(top: AnyStr, topdown: bool = ..., onerror: Optional[Callable[[OSError], Any]] = ..., + followlinks: bool = ...) -> Iterator[Tuple[AnyStr, List[AnyStr], + List[AnyStr]]]: ... def abort() -> NoReturn: ... # These are defined as execl(file, *args) but the first *arg is mandatory. @@ -232,11 +405,19 @@ def forkpty() -> Tuple[int, int]: ... # some flavors of Unix def kill(pid: int, sig: int) -> None: ... def killpg(pgid: int, sig: int) -> None: ... # Unix only def nice(increment: int) -> int: ... # Unix only -# TODO: plock, popen*, P_* -def popen(command: str, *args, **kwargs) -> Optional[IO[Any]]: ... -def popen2(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ... -def popen3(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any], IO[Any]]: ... -def popen4(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ... +def plock(op: int) -> None: ... # Unix only ???op is int? + +if sys.version_info >= (3, 0): + class popen(_TextIOWrapper): + # TODO 'b' modes or bytes command not accepted? + def __init__(self, command: str, mode: str = ..., + bufsize: int = ...) -> None: ... + def close(self) -> Any: ... # may return int +else: + def popen(command: str, *args, **kwargs) -> Optional[IO[Any]]: ... + def popen2(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ... + def popen3(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any], IO[Any]]: ... + def popen4(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ... def spawnl(mode: int, path: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ... def spawnle(mode: int, path: _PathType, arg0: Union[bytes, Text], @@ -272,10 +453,34 @@ def getloadavg() -> Tuple[float, float, float]: ... # Unix only def sysconf(name: Union[str, int]) -> int: ... # Unix only def urandom(n: int) -> bytes: ... -def tmpfile() -> IO[Any]: ... -def tmpnam() -> str: ... -def tempnam(dir: str = ..., prefix: str = ...) -> str: ... +if sys.version_info >= (3, 0): + def sched_getaffinity(id: int) -> Set[int]: ... +if sys.version_info >= (3, 3): + class waitresult: + si_pid: int + def waitid(idtype: int, id: int, options: int) -> waitresult: ... + +if sys.version_info < (3, 0): + def tmpfile() -> IO[Any]: ... + def tmpnam() -> str: ... + def tempnam(dir: str = ..., prefix: str = ...) -> str: ... P_ALL: int WEXITED: int WNOWAIT: int + +if sys.version_info >= (3, 3): + def sync() -> None: ... # Unix only + + def truncate(path: Union[_PathType, int], length: int) -> None: ... # Unix only up to version 3.4 + + def fwalk(top: AnyStr = ..., topdown: bool = ..., + onerror: Callable = ..., *, follow_symlinks: bool = ..., + dir_fd: int = ...) -> Iterator[Tuple[AnyStr, List[AnyStr], + List[AnyStr], int]]: ... # Unix only + + terminal_size = NamedTuple('terminal_size', [('columns', int), ('lines', int)]) + def get_terminal_size(fd: int = ...) -> terminal_size: ... + +if sys.version_info >= (3, 4): + def cpu_count() -> Optional[int]: ... diff --git a/stdlib/3/os/__init__.pyi b/stdlib/3/os/__init__.pyi index 91271fba07e3..ad2c3c5119ca 100644 --- a/stdlib/3/os/__init__.pyi +++ b/stdlib/3/os/__init__.pyi @@ -8,7 +8,7 @@ from io import TextIOWrapper as _TextIOWrapper import sys from typing import ( Mapping, MutableMapping, Dict, List, Any, Tuple, Iterator, overload, Union, AnyStr, - Optional, Generic, Set, Callable, Text, Sequence, NamedTuple, TypeVar + Optional, Generic, Set, Callable, Text, Sequence, IO, NamedTuple, TypeVar ) from . import path as path from mypy_extensions import NoReturn @@ -17,7 +17,8 @@ _T = TypeVar('_T') # ----- os variables ----- -supports_bytes_environ: bool +if sys.version_info >= (3, 2): + supports_bytes_environ: bool if sys.version_info >= (3, 3): supports_dir_fd: Set[Callable[..., Any]] @@ -56,6 +57,7 @@ O_DIRECT: int # Gnu extension if in C library O_DIRECTORY: int # Gnu extension if in C library O_NOFOLLOW: int # Gnu extension if in C library O_NOATIME: int # Gnu extension if in C library +O_LARGEFILE: int # Gnu extension if in C library curdir: str pardir: str @@ -77,7 +79,8 @@ class _Environ(MutableMapping[AnyStr, AnyStr], Generic[AnyStr]): def copy(self) -> Dict[AnyStr, AnyStr]: ... environ: _Environ[str] -environb: _Environ[bytes] +if sys.version_info >= (3, 2): + environb: _Environ[bytes] confstr_names: Dict[str, int] # Unix only pathconf_names: Dict[str, int] # Unix only @@ -207,12 +210,12 @@ class statvfs_result: # Unix only # ----- os function stubs ----- if sys.version_info >= (3, 6): def fsencode(filename: Union[str, bytes, PathLike]) -> bytes: ... -else: +elif sys.version_info >= (3, 2): def fsencode(filename: Union[str, bytes]) -> bytes: ... if sys.version_info >= (3, 6): def fsdecode(filename: Union[str, bytes, PathLike]) -> str: ... -else: +elif sys.version_info >= (3, 2): def fsdecode(filename: Union[str, bytes]) -> str: ... if sys.version_info >= (3, 6): @@ -223,8 +226,9 @@ if sys.version_info >= (3, 6): @overload def fspath(path: PathLike) -> Any: ... -def get_exec_path(env: Optional[Mapping[str, str]] = ...) -> List[str]: ... -# NOTE: get_exec_path(): returns List[bytes] when env not None +if sys.version_info >= (3, 2): + def get_exec_path(env: Optional[Mapping[str, str]] = ...) -> List[str]: ... + # NOTE: get_exec_path(): returns List[bytes] when env not None def ctermid() -> str: ... # Unix only def getegid() -> int: ... # Unix only def geteuid() -> int: ... # Unix only @@ -260,23 +264,27 @@ def uname() -> Tuple[str, str, str, str, str]: ... # Unix only def getenv(key: Text) -> Optional[str]: ... @overload def getenv(key: Text, default: _T) -> Union[str, _T]: ... -def getenvb(key: bytes, default: bytes = ...) -> bytes: ... +if sys.version_info >= (3, 2): + def getenvb(key: bytes, default: bytes = ...) -> bytes: ... def putenv(key: Union[bytes, Text], value: Union[bytes, Text]) -> None: ... def unsetenv(key: Union[bytes, Text]) -> None: ... -# Return IO or TextIO -def fdopen(fd: int, mode: str = ..., buffering: int = ..., encoding: str = ..., - errors: str = ..., newline: str = ..., closefd: bool = ...) -> Any: ... +if sys.version_info >= (3, 0): + def fdopen(fd: int, mode: str = ..., buffering: int = ..., encoding: str = ..., + errors: str = ..., newline: str = ..., closefd: bool = ...) -> IO[Any]: ... +else: + def fdopen(fd: int, mode: str = ..., bufsize: int = ...) -> IO[Any]: ... def close(fd: int) -> None: ... def closerange(fd_low: int, fd_high: int) -> None: ... -def device_encoding(fd: int) -> Optional[str]: ... +if sys.version_info >= (3, 0): + def device_encoding(fd: int) -> Optional[str]: ... def dup(fd: int) -> int: ... def dup2(fd: int, fd2: int) -> None: ... def fchmod(fd: int, mode: int) -> None: ... # Unix only def fchown(fd: int, uid: int, gid: int) -> None: ... # Unix only def fdatasync(fd: int) -> None: ... # Unix only, not Mac def fpathconf(fd: int, name: Union[str, int]) -> int: ... # Unix only -def fstat(fd: int) -> stat_result: ... +def fstat(fd: int) -> Any: ... def fstatvfs(fd: int) -> statvfs_result: ... # Unix only def fsync(fd: int) -> None: ... def ftruncate(fd: int, length: int) -> None: ... # Unix only @@ -294,7 +302,10 @@ def access(path: _PathType, mode: int) -> bool: ... def chdir(path: _PathType) -> None: ... def fchdir(fd: int) -> None: ... def getcwd() -> str: ... -def getcwdb() -> bytes: ... +if sys.version_info >= (3, 0): + def getcwdb() -> bytes: ... +else: + def getcwdu() -> unicode: ... def chflags(path: _PathType, flags: int) -> None: ... # Unix only def chroot(path: _PathType) -> None: ... # Unix only def chmod(path: _PathType, mode: int) -> None: ... @@ -304,20 +315,27 @@ def lchmod(path: _PathType, mode: int) -> None: ... # Unix only def lchown(path: _PathType, uid: int, gid: int) -> None: ... # Unix only def link(src: _PathType, link_name: _PathType) -> None: ... -@overload -def listdir(path: str = ...) -> List[str]: ... -@overload -def listdir(path: bytes) -> List[bytes]: ... +# TODO: support PathLike and fd arguments +if sys.version_info >= (3, 2): + @overload + def listdir(path: str = ...) -> List[str]: ... + @overload + def listdir(path: bytes) -> List[bytes]: ... +else: + def listdir(path: AnyStr) -> List[AnyStr]: ... -def lstat(path: _PathType) -> stat_result: ... +def lstat(path: _PathType) -> Any: ... def mkfifo(path: _PathType, mode: int = ...) -> None: ... # Unix only def mknod(filename: _PathType, mode: int = ..., device: int = ...) -> None: ... def major(device: int) -> int: ... def minor(device: int) -> int: ... def makedev(major: int, minor: int) -> int: ... def mkdir(path: _PathType, mode: int = ...) -> None: ... -def makedirs(path: _PathType, mode: int = ..., - exist_ok: bool = ...) -> None: ... +if sys.version_info >= (3, 2): + def makedirs(path: _PathType, mode: int = ..., + exist_ok: bool = ...) -> None: ... +else: + def makedirs(path: _PathType, mode: int = ...) -> None: ... def pathconf(path: _PathType, name: Union[str, int]) -> int: ... # Unix only def readlink(path: AnyStr) -> AnyStr: ... def remove(path: _PathType) -> None: ... @@ -332,14 +350,27 @@ if sys.version_info >= (3, 5): def scandir(path: str = ...) -> Iterator[DirEntry[str]]: ... @overload def scandir(path: bytes) -> Iterator[DirEntry[bytes]]: ... -def stat(path: _PathType) -> stat_result: ... -def stat_float_times(newvalue: Union[bool, None] = ...) -> bool: ... +# This cannot return stat_result because of the issues discussed in +# https://github.com/python/typeshed/pull/1103 +def stat(path: _PathType) -> Any: ... +@overload +def stat_float_times(newvalue: bool = ...) -> None: ... +@overload +def stat_float_times() -> bool: ... def statvfs(path: _PathType) -> statvfs_result: ... # Unix only -def symlink(source: _PathType, link_name: _PathType, - target_is_directory: bool = ...) -> None: - ... # final argument in Windows only +# TODO add dir_fd argument +if sys.version_info >= (3, 0): + def symlink(source: _PathType, link_name: _PathType, + target_is_directory: bool = ...) -> None: + ... # final argument in Windows only +else: + def symlink(source: _PathType, link_name: _PathType) -> None: ... def unlink(path: _PathType) -> None: ... -def utime(path: _PathType, times: Optional[Tuple[float, float]] = ...) -> None: ... +# TODO: add ns, dir_fd, follow_symlinks argument +if sys.version_info >= (3, 0): + def utime(path: _PathType, times: Optional[Tuple[float, float]] = ...) -> None: ... +else: + def utime(path: _PathType, times: Optional[Tuple[float, float]]) -> None: ... if sys.version_info >= (3, 6): def walk(top: Union[AnyStr, PathLike[AnyStr]], topdown: bool = ..., @@ -376,11 +407,17 @@ def killpg(pgid: int, sig: int) -> None: ... # Unix only def nice(increment: int) -> int: ... # Unix only def plock(op: int) -> None: ... # Unix only ???op is int? -class popen(_TextIOWrapper): - # TODO 'b' modes or bytes command not accepted? - def __init__(self, command: str, mode: str = ..., - bufsize: int = ...) -> None: ... - def close(self) -> Any: ... # may return int +if sys.version_info >= (3, 0): + class popen(_TextIOWrapper): + # TODO 'b' modes or bytes command not accepted? + def __init__(self, command: str, mode: str = ..., + bufsize: int = ...) -> None: ... + def close(self) -> Any: ... # may return int +else: + def popen(command: str, *args, **kwargs) -> Optional[IO[Any]]: ... + def popen2(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ... + def popen3(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any], IO[Any]]: ... + def popen4(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ... def spawnl(mode: int, path: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ... def spawnle(mode: int, path: _PathType, arg0: Union[bytes, Text], @@ -416,10 +453,18 @@ def getloadavg() -> Tuple[float, float, float]: ... # Unix only def sysconf(name: Union[str, int]) -> int: ... # Unix only def urandom(n: int) -> bytes: ... -def sched_getaffinity(id: int) -> Set[int]: ... -class waitresult: - si_pid: int -def waitid(idtype: int, id: int, options: int) -> waitresult: ... +if sys.version_info >= (3, 0): + def sched_getaffinity(id: int) -> Set[int]: ... +if sys.version_info >= (3, 3): + class waitresult: + si_pid: int + def waitid(idtype: int, id: int, options: int) -> waitresult: ... + +if sys.version_info < (3, 0): + def tmpfile() -> IO[Any]: ... + def tmpnam() -> str: ... + def tempnam(dir: str = ..., prefix: str = ...) -> str: ... + P_ALL: int WEXITED: int WNOWAIT: int