From 6c3e175c8dd22bcb1b56882db03a9714a3aa0fc2 Mon Sep 17 00:00:00 2001 From: David Euresti Date: Wed, 29 Mar 2017 10:36:04 -0700 Subject: [PATCH] Make os.stat_result and friends NamedTuples (#1103) --- stdlib/2/os/__init__.pyi | 69 ++++++++++++++++++++++++++++++----- stdlib/3/os/__init__.pyi | 79 ++++++++++++++++++++-------------------- 2 files changed, 98 insertions(+), 50 deletions(-) diff --git a/stdlib/2/os/__init__.pyi b/stdlib/2/os/__init__.pyi index c11d9728f..61da6e988 100644 --- a/stdlib/2/os/__init__.pyi +++ b/stdlib/2/os/__init__.pyi @@ -1,5 +1,6 @@ # created from https://docs.python.org/2/library/os.html +import sys from typing import ( Mapping, MutableMapping, Dict, List, Any, Tuple, Iterator, overload, Union, AnyStr, Optional, Generic, Set, Callable, Text, Sequence, IO, NamedTuple @@ -94,10 +95,58 @@ WUNTRACED = 0 # Unix only TMP_MAX = 0 # 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)]) + +class stat_result(NamedTuple('stat_result', [ + ('st_mode', int), + ('st_ino', int), + ('st_dev', int), + ('st_nlink', int), + ('st_uid', int), + ('st_gid', int), + ('st_size', int), + ('st_atime', float), + ('st_mtime', float), + ('st_ctime', float)])): + + # 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. + + if sys.version_info >= (3, 3): + st_atime_ns = ... # type: int + st_mtime_ns = ... # type: int + st_ctime_ns = ... # type: int + + # On some Unix systems (such as Linux), the following attributes may also + # be available: + st_blocks = ... # type: int + st_blksize = ... # type: int + st_rdev = ... # type: int + st_flags = ... # type: int + + # 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 = ... # type: int + st_birthtime = ... # type: int + + # On Mac OS systems, the following attributes may also be available: + st_rsize = ... # type: int + st_creator = ... # type: int + st_type = ... # type: int + +statvfs_result = NamedTuple('statvfs_result', [ + ('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)]) def ctermid() -> str: ... # Unix only def getegid() -> int: ... # Unix only def geteuid() -> int: ... # Unix only @@ -140,8 +189,8 @@ 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) -> Any: ... -def fstatvfs(fd: int) -> _StatVFS: ... # Unix only +def fstat(fd: int) -> stat_result: ... +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 @@ -168,7 +217,7 @@ 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]: ... -def lstat(path: _PathType) -> Any: ... +def lstat(path: _PathType) -> stat_result: ... def mkfifo(path: _PathType, mode: int = ...) -> None: ... # Unix only def mknod(filename: _PathType, mode: int = ..., device: int = ...) -> None: ... def major(device: int) -> int: ... @@ -183,12 +232,12 @@ def removedirs(path: _PathType) -> None: ... def rename(src: _PathType, dst: _PathType) -> None: ... def renames(old: _PathType, new: _PathType) -> None: ... def rmdir(path: _PathType) -> None: ... -def stat(path: _PathType) -> Any: ... @overload -def stat_float_times(newvalue: bool = ...) -> None: ... +def stat_float_times(newvalue: bool) -> None: ... @overload def stat_float_times() -> bool: ... -def statvfs(path: _PathType) -> _StatVFS: ... # Unix only +def stat(path: _PathType) -> stat_result: ... +def statvfs(path: _PathType) -> statvfs_result: ... # Unix only def symlink(source: _PathType, link_name: _PathType) -> None: ... def unlink(path: _PathType) -> None: ... def utime(path: _PathType, times: Optional[Tuple[float, float]]) -> None: ... diff --git a/stdlib/3/os/__init__.pyi b/stdlib/3/os/__init__.pyi index 2324a9343..3101582d9 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 + Optional, Generic, Set, Callable, Text, Sequence, NamedTuple ) from . import path from mypy_extensions import NoReturn @@ -145,61 +145,57 @@ elif sys.version_info >= (3, 5): def is_symlink(self) -> bool: ... def stat(self) -> stat_result: ... +class stat_result(NamedTuple('stat_result', [ + ('st_mode', int), + ('st_ino', int), + ('st_dev', int), + ('st_nlink', int), + ('st_uid', int), + ('st_gid', int), + ('st_size', int), + ('st_atime', float), + ('st_mtime', float), + ('st_ctime', float)])): -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 = 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) - 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 - - # not documented - def __init__(self, tuple: Tuple[int, ...]) -> None: ... + st_atime_ns = ... # type: int + st_mtime_ns = ... # type: int + st_ctime_ns = ... # type: int # 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 = ... # type: int + st_blksize = ... # type: int + st_rdev = ... # type: int + st_flags = ... # type: int # 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 = ... # type: int + st_birthtime = ... # type: int # On Mac OS systems, the following attributes may also be available: - st_rsize = 0 - st_creator = 0 - st_type = 0 + st_rsize = ... # type: int + st_creator = ... # type: int + st_type = ... # 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 +statvfs_result = NamedTuple('statvfs_result', [ + ('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): @@ -325,8 +321,11 @@ if sys.version_info >= (3, 5): def scandir(path: str = ...) -> Iterator[DirEntry[str]]: ... @overload def scandir(path: bytes) -> Iterator[DirEntry[bytes]]: ... +@overload +def stat_float_times(newvalue: bool) -> None: ... +@overload +def stat_float_times() -> bool: ... def stat(path: _PathType) -> stat_result: ... -def stat_float_times(newvalue: Union[bool, None] = ...) -> bool: ... def statvfs(path: _PathType) -> statvfs_result: ... # Unix only def symlink(source: _PathType, link_name: _PathType, target_is_directory: bool = ...) -> None: