Skip to content

Commit 6c3e175

Browse files
eurestiJelleZijlstra
authored andcommitted
Make os.stat_result and friends NamedTuples (#1103)
1 parent da43a23 commit 6c3e175

File tree

2 files changed

+99
-51
lines changed

2 files changed

+99
-51
lines changed

stdlib/2/os/__init__.pyi

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# created from https://docs.python.org/2/library/os.html
22

3+
import sys
34
from typing import (
45
Mapping, MutableMapping, Dict, List, Any, Tuple, Iterator, overload, Union, AnyStr,
56
Optional, Generic, Set, Callable, Text, Sequence, IO, NamedTuple
@@ -94,10 +95,58 @@ WUNTRACED = 0 # Unix only
9495

9596
TMP_MAX = 0 # Undocumented, but used by tempfile
9697
_PathType = Union[bytes, Text]
97-
_StatVFS = NamedTuple('_StatVFS', [('f_bsize', int), ('f_frsize', int), ('f_blocks', int),
98-
('f_bfree', int), ('f_bavail', int), ('f_files', int),
99-
('f_ffree', int), ('f_favail', int), ('f_flag', int),
100-
('f_namemax', int)])
98+
99+
class stat_result(NamedTuple('stat_result', [
100+
('st_mode', int),
101+
('st_ino', int),
102+
('st_dev', int),
103+
('st_nlink', int),
104+
('st_uid', int),
105+
('st_gid', int),
106+
('st_size', int),
107+
('st_atime', float),
108+
('st_mtime', float),
109+
('st_ctime', float)])):
110+
111+
# For backward compatibility, the return value of stat() is also
112+
# accessible as a tuple of at least 10 integers giving the most important
113+
# (and portable) members of the stat structure, in the order st_mode,
114+
# st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime,
115+
# st_ctime. More items may be added at the end by some implementations.
116+
117+
if sys.version_info >= (3, 3):
118+
st_atime_ns = ... # type: int
119+
st_mtime_ns = ... # type: int
120+
st_ctime_ns = ... # type: int
121+
122+
# On some Unix systems (such as Linux), the following attributes may also
123+
# be available:
124+
st_blocks = ... # type: int
125+
st_blksize = ... # type: int
126+
st_rdev = ... # type: int
127+
st_flags = ... # type: int
128+
129+
# On other Unix systems (such as FreeBSD), the following attributes may be
130+
# available (but may be only filled out if root tries to use them):
131+
st_gen = ... # type: int
132+
st_birthtime = ... # type: int
133+
134+
# On Mac OS systems, the following attributes may also be available:
135+
st_rsize = ... # type: int
136+
st_creator = ... # type: int
137+
st_type = ... # type: int
138+
139+
statvfs_result = NamedTuple('statvfs_result', [
140+
('f_bsize', int),
141+
('f_frsize', int),
142+
('f_blocks', int),
143+
('f_bfree', int),
144+
('f_bavail', int),
145+
('f_files', int),
146+
('f_ffree', int),
147+
('f_favail', int),
148+
('f_flag', int),
149+
('f_namemax', int)])
101150
def ctermid() -> str: ... # Unix only
102151
def getegid() -> int: ... # Unix only
103152
def geteuid() -> int: ... # Unix only
@@ -140,8 +189,8 @@ def fchmod(fd: int, mode: int) -> None: ... # Unix only
140189
def fchown(fd: int, uid: int, gid: int) -> None: ... # Unix only
141190
def fdatasync(fd: int) -> None: ... # Unix only, not Mac
142191
def fpathconf(fd: int, name: Union[str, int]) -> int: ... # Unix only
143-
def fstat(fd: int) -> Any: ...
144-
def fstatvfs(fd: int) -> _StatVFS: ... # Unix only
192+
def fstat(fd: int) -> stat_result: ...
193+
def fstatvfs(fd: int) -> statvfs_result: ... # Unix only
145194
def fsync(fd: int) -> None: ...
146195
def ftruncate(fd: int, length: int) -> None: ... # Unix only
147196
def isatty(fd: int) -> bool: ... # Unix only
@@ -168,7 +217,7 @@ def lchmod(path: _PathType, mode: int) -> None: ... # Unix only
168217
def lchown(path: _PathType, uid: int, gid: int) -> None: ... # Unix only
169218
def link(src: _PathType, link_name: _PathType) -> None: ...
170219
def listdir(path: AnyStr) -> List[AnyStr]: ...
171-
def lstat(path: _PathType) -> Any: ...
220+
def lstat(path: _PathType) -> stat_result: ...
172221
def mkfifo(path: _PathType, mode: int = ...) -> None: ... # Unix only
173222
def mknod(filename: _PathType, mode: int = ..., device: int = ...) -> None: ...
174223
def major(device: int) -> int: ...
@@ -183,12 +232,12 @@ def removedirs(path: _PathType) -> None: ...
183232
def rename(src: _PathType, dst: _PathType) -> None: ...
184233
def renames(old: _PathType, new: _PathType) -> None: ...
185234
def rmdir(path: _PathType) -> None: ...
186-
def stat(path: _PathType) -> Any: ...
187235
@overload
188-
def stat_float_times(newvalue: bool = ...) -> None: ...
236+
def stat_float_times(newvalue: bool) -> None: ...
189237
@overload
190238
def stat_float_times() -> bool: ...
191-
def statvfs(path: _PathType) -> _StatVFS: ... # Unix only
239+
def stat(path: _PathType) -> stat_result: ...
240+
def statvfs(path: _PathType) -> statvfs_result: ... # Unix only
192241
def symlink(source: _PathType, link_name: _PathType) -> None: ...
193242
def unlink(path: _PathType) -> None: ...
194243
def utime(path: _PathType, times: Optional[Tuple[float, float]]) -> None: ...

stdlib/3/os/__init__.pyi

Lines changed: 40 additions & 41 deletions
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, Text, Sequence
11+
Optional, Generic, Set, Callable, Text, Sequence, NamedTuple
1212
)
1313
from . import path
1414
from mypy_extensions import NoReturn
@@ -145,61 +145,57 @@ elif sys.version_info >= (3, 5):
145145
def is_symlink(self) -> bool: ...
146146
def stat(self) -> stat_result: ...
147147

148+
class stat_result(NamedTuple('stat_result', [
149+
('st_mode', int),
150+
('st_ino', int),
151+
('st_dev', int),
152+
('st_nlink', int),
153+
('st_uid', int),
154+
('st_gid', int),
155+
('st_size', int),
156+
('st_atime', float),
157+
('st_mtime', float),
158+
('st_ctime', float)])):
148159

149-
class stat_result:
150160
# For backward compatibility, the return value of stat() is also
151161
# accessible as a tuple of at least 10 integers giving the most important
152162
# (and portable) members of the stat structure, in the order st_mode,
153163
# st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime,
154164
# st_ctime. More items may be added at the end by some implementations.
155165

156-
st_mode = 0 # protection bits,
157-
st_ino = 0 # inode number,
158-
st_dev = 0 # device,
159-
st_nlink = 0 # number of hard links,
160-
st_uid = 0 # user id of owner,
161-
st_gid = 0 # group id of owner,
162-
st_size = 0 # size of file, in bytes,
163-
st_atime = 0.0 # time of most recent access,
164-
st_mtime = 0.0 # time of most recent content modification,
165-
st_ctime = 0.0 # platform dependent (time of most recent metadata change on Unix, or the time of creation on Windows)
166-
167166
if sys.version_info >= (3, 3):
168-
st_atime_ns = 0 # time of most recent access, in nanoseconds
169-
st_mtime_ns = 0 # time of most recent content modification in nanoseconds
170-
st_ctime_ns = 0 # platform dependent (time of most recent metadata change on Unix, or the time of creation on Windows) in nanoseconds
171-
172-
# not documented
173-
def __init__(self, tuple: Tuple[int, ...]) -> None: ...
167+
st_atime_ns = ... # type: int
168+
st_mtime_ns = ... # type: int
169+
st_ctime_ns = ... # type: int
174170

175171
# On some Unix systems (such as Linux), the following attributes may also
176172
# be available:
177-
st_blocks = 0 # number of blocks allocated for file
178-
st_blksize = 0 # filesystem blocksize
179-
st_rdev = 0 # type of device if an inode device
180-
st_flags = 0 # user defined flags for file
173+
st_blocks = ... # type: int
174+
st_blksize = ... # type: int
175+
st_rdev = ... # type: int
176+
st_flags = ... # type: int
181177

182178
# On other Unix systems (such as FreeBSD), the following attributes may be
183179
# available (but may be only filled out if root tries to use them):
184-
st_gen = 0 # file generation number
185-
st_birthtime = 0 # time of file creation
180+
st_gen = ... # type: int
181+
st_birthtime = ... # type: int
186182

187183
# On Mac OS systems, the following attributes may also be available:
188-
st_rsize = 0
189-
st_creator = 0
190-
st_type = 0
191-
192-
class statvfs_result: # Unix only
193-
f_bsize = 0
194-
f_frsize = 0
195-
f_blocks = 0
196-
f_bfree = 0
197-
f_bavail = 0
198-
f_files = 0
199-
f_ffree = 0
200-
f_favail = 0
201-
f_flag = 0
202-
f_namemax = 0
184+
st_rsize = ... # type: int
185+
st_creator = ... # type: int
186+
st_type = ... # type: int
187+
188+
statvfs_result = NamedTuple('statvfs_result', [
189+
('f_bsize', int),
190+
('f_frsize', int),
191+
('f_blocks', int),
192+
('f_bfree', int),
193+
('f_bavail', int),
194+
('f_files', int),
195+
('f_ffree', int),
196+
('f_favail', int),
197+
('f_flag', int),
198+
('f_namemax', int)])
203199

204200
# ----- os function stubs -----
205201
if sys.version_info >= (3, 6):
@@ -325,8 +321,11 @@ if sys.version_info >= (3, 5):
325321
def scandir(path: str = ...) -> Iterator[DirEntry[str]]: ...
326322
@overload
327323
def scandir(path: bytes) -> Iterator[DirEntry[bytes]]: ...
324+
@overload
325+
def stat_float_times(newvalue: bool) -> None: ...
326+
@overload
327+
def stat_float_times() -> bool: ...
328328
def stat(path: _PathType) -> stat_result: ...
329-
def stat_float_times(newvalue: Union[bool, None] = ...) -> bool: ...
330329
def statvfs(path: _PathType) -> statvfs_result: ... # Unix only
331330
def symlink(source: _PathType, link_name: _PathType,
332331
target_is_directory: bool = ...) -> None:

0 commit comments

Comments
 (0)