Skip to content

Commit ec7960a

Browse files
authored
Convert namedtuples to class syntax (#3321)
1 parent 2b9dc7b commit ec7960a

41 files changed

Lines changed: 402 additions & 388 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

stdlib/2/functools.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from abc import ABCMeta, abstractmethod
66
from typing import Any, Callable, Generic, Dict, Iterable, Optional, Sequence, Tuple, TypeVar, overload
7-
from collections import namedtuple
87

98
_AnyCallable = Callable[..., Any]
109

stdlib/2/inspect.pyi

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ CO_VARARGS: int
2222
CO_VARKEYWORDS: int
2323
TPFLAGS_IS_ABSTRACT: int
2424

25-
ModuleInfo = NamedTuple('ModuleInfo', [('name', str),
26-
('suffix', str),
27-
('mode', str),
28-
('module_type', int),
29-
])
25+
class ModuleInfo(NamedTuple):
26+
name: str
27+
suffix: str
28+
mode: str
29+
module_type: int
30+
3031
def getmembers(
3132
object: object,
3233
predicate: Optional[Callable[[Any], bool]] = ...
@@ -70,22 +71,22 @@ def indentsize(line: str) -> int: ...
7071
# Classes and functions
7172
def getclasstree(classes: List[type], unique: bool = ...) -> List[Union[Tuple[type, Tuple[type, ...]], List[Any]]]: ...
7273

73-
ArgSpec = NamedTuple('ArgSpec', [('args', List[str]),
74-
('varargs', Optional[str]),
75-
('keywords', Optional[str]),
76-
('defaults', Tuple[Any, ...]),
77-
])
74+
class ArgSpec(NamedTuple):
75+
args: List[str]
76+
varargs: Optional[str]
77+
keywords: Optional[str]
78+
defaults: Tuple[Any, ...]
7879

79-
ArgInfo = NamedTuple('ArgInfo', [('args', List[str]),
80-
('varargs', Optional[str]),
81-
('keywords', Optional[str]),
82-
('locals', Dict[str, Any]),
83-
])
80+
class ArgInfo(NamedTuple):
81+
args: List[str]
82+
varargs: Optional[str]
83+
keywords: Optional[str]
84+
locals: Dict[str, Any]
8485

85-
Arguments = NamedTuple('Arguments', [('args', List[Union[str, List[Any]]]),
86-
('varargs', Optional[str]),
87-
('keywords', Optional[str]),
88-
])
86+
class Arguments(NamedTuple):
87+
args: List[Union[str, List[Any]]]
88+
varargs: Optional[str]
89+
keywords: Optional[str]
8990

9091
def getargs(co: CodeType) -> Arguments: ...
9192
def getargspec(func: object) -> ArgSpec: ...
@@ -101,16 +102,12 @@ def getcallargs(func, *args, **kwds) -> Dict[str, Any]: ...
101102

102103
# The interpreter stack
103104

104-
Traceback = NamedTuple(
105-
'Traceback',
106-
[
107-
('filename', str),
108-
('lineno', int),
109-
('function', str),
110-
('code_context', Optional[List[str]]),
111-
('index', Optional[int]),
112-
]
113-
)
105+
class Traceback(NamedTuple):
106+
filename: str
107+
lineno: int
108+
function: str
109+
code_context: Optional[List[str]]
110+
index: Optional[int] # type: ignore
114111

115112
_FrameInfo = Tuple[FrameType, str, int, str, Optional[List[str]], Optional[int]]
116113

@@ -123,10 +120,10 @@ def currentframe(depth: int = ...) -> FrameType: ...
123120
def stack(context: int = ...) -> List[_FrameInfo]: ...
124121
def trace(context: int = ...) -> List[_FrameInfo]: ...
125122

126-
Attribute = NamedTuple('Attribute', [('name', str),
127-
('kind', str),
128-
('defining_class', type),
129-
('object', object),
130-
])
123+
class Attribute(NamedTuple):
124+
name: str
125+
kind: str
126+
defining_class: type
127+
object: object
131128

132129
def classify_class_attrs(cls: type) -> List[Attribute]: ...

stdlib/2/os/__init__.pyi

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,17 @@ if sys.version_info >= (3, 6):
136136

137137
_PathType = path._PathType
138138

139-
_StatVFS = NamedTuple('_StatVFS', [('f_bsize', int), ('f_frsize', int), ('f_blocks', int),
140-
('f_bfree', int), ('f_bavail', int), ('f_files', int),
141-
('f_ffree', int), ('f_favail', int), ('f_flag', int),
142-
('f_namemax', int)])
139+
class _StatVFS(NamedTuple):
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
143150

144151
def getlogin() -> str: ...
145152
def getpid() -> int: ...
@@ -349,20 +356,3 @@ if sys.version_info < (3, 0):
349356
P_ALL: int
350357
WEXITED: int
351358
WNOWAIT: int
352-
353-
if sys.version_info >= (3, 3):
354-
if sys.platform != 'win32':
355-
# Unix only
356-
def sync() -> None: ...
357-
358-
def truncate(path: Union[_PathType, int], length: int) -> None: ... # Unix only up to version 3.4
359-
360-
def fwalk(top: AnyStr = ..., topdown: bool = ...,
361-
onerror: Callable = ..., *, follow_symlinks: bool = ...,
362-
dir_fd: int = ...) -> Iterator[Tuple[AnyStr, List[AnyStr], List[AnyStr], int]]: ...
363-
364-
terminal_size = NamedTuple('terminal_size', [('columns', int), ('lines', int)])
365-
def get_terminal_size(fd: int = ...) -> terminal_size: ...
366-
367-
if sys.version_info >= (3, 4):
368-
def cpu_count() -> Optional[int]: ...

stdlib/2/resource.pyi

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,24 @@ RLIMIT_MEMLOCK: int
1919
RLIMIT_VMEM: int
2020
RLIMIT_AS: int
2121

22-
_RUsage = NamedTuple('_RUsage', [('ru_utime', float), ('ru_stime', float), ('ru_maxrss', int),
23-
('ru_ixrss', int), ('ru_idrss', int), ('ru_isrss', int),
24-
('ru_minflt', int), ('ru_majflt', int), ('ru_nswap', int),
25-
('ru_inblock', int), ('ru_oublock', int), ('ru_msgsnd', int),
26-
('ru_msgrcv', int), ('ru_nsignals', int), ('ru_nvcsw', int),
27-
('ru_nivcsw', int)])
22+
class _RUsage(NamedTuple):
23+
ru_utime: float
24+
ru_stime: float
25+
ru_maxrss: int
26+
ru_ixrss: int
27+
ru_idrss: int
28+
ru_isrss: int
29+
ru_minflt: int
30+
ru_majflt: int
31+
ru_nswap: int
32+
ru_inblock: int
33+
ru_oublock: int
34+
ru_msgsnd: int
35+
ru_msgrcv: int
36+
ru_nsignals: int
37+
ru_nvcsw: int
38+
ru_nivcsw: int
39+
2840
def getrusage(who: int) -> _RUsage: ...
2941
def getpagesize() -> int: ...
3042

stdlib/2/spwd.pyi

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from typing import List, NamedTuple
22

3-
struct_spwd = NamedTuple("struct_spwd", [("sp_nam", str),
4-
("sp_pwd", str),
5-
("sp_lstchg", int),
6-
("sp_min", int),
7-
("sp_max", int),
8-
("sp_warn", int),
9-
("sp_inact", int),
10-
("sp_expire", int),
11-
("sp_flag", int)])
3+
class struct_spwd(NamedTuple):
4+
sp_nam: str
5+
sp_pwd: str
6+
sp_lstchg: int
7+
sp_min: int
8+
sp_max: int
9+
sp_warn: int
10+
sp_inact: int
11+
sp_expire: int
12+
sp_flag: int
1213

1314
def getspall() -> List[struct_spwd]: ...
1415
def getspnam(name: str) -> struct_spwd: ...

stdlib/2/urlparse.pyi

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,23 @@ class ResultMixin(object):
2525
@property
2626
def port(self) -> Optional[int]: ...
2727

28-
class SplitResult(
29-
NamedTuple(
30-
'SplitResult',
31-
[
32-
('scheme', str), ('netloc', str), ('path', str), ('query', str), ('fragment', str)
33-
]
34-
),
35-
ResultMixin
36-
):
28+
class _SplitResult(NamedTuple):
29+
scheme: str
30+
netloc: str
31+
path: str
32+
query: str
33+
fragment: str
34+
class SplitResult(_SplitResult, ResultMixin):
3735
def geturl(self) -> str: ...
3836

39-
class ParseResult(
40-
NamedTuple(
41-
'ParseResult',
42-
[
43-
('scheme', str), ('netloc', str), ('path', str), ('params', str), ('query', str),
44-
('fragment', str)
45-
]
46-
),
47-
ResultMixin
48-
):
37+
class _ParseResult(NamedTuple):
38+
scheme: str
39+
netloc: str
40+
path: str
41+
params: str
42+
query: str
43+
fragment: str
44+
class ParseResult(_ParseResult, ResultMixin):
4945
def geturl(self) -> str: ...
5046

5147
def urlparse(url: _String, scheme: _String = ...,

stdlib/2and3/_curses.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,5 +454,8 @@ class _CursesWindow:
454454
def vline(self, y: int, x: int, ch: _chtype, n: int) -> None: ...
455455

456456
if sys.version_info >= (3, 8):
457-
_ncurses_version = NamedTuple("ncurses_version", [("major", int), ("minor", int), ("patch", int)])
457+
class _ncurses_version(NamedTuple):
458+
major: int
459+
minor: int
460+
patch: int
458461
ncurses_version: _ncurses_version

stdlib/2and3/aifc.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
21
from typing import Union, IO, Optional, Type, NamedTuple, List, Tuple, Any, Text, overload
32
from typing_extensions import Literal
43
from types import TracebackType
54
import sys
65

76
class Error(Exception): ...
87

9-
_aifc_params = NamedTuple('_aifc_params', [('nchannels', int), ('sampwidth', int), ('framerate', int),
10-
('nframes', int), ('comptype', bytes), ('compname', bytes)])
8+
class _aifc_params(NamedTuple):
9+
nchannels: int
10+
sampwidth: int
11+
framerate: int
12+
nframes: int
13+
comptype: bytes
14+
compname: bytes
1115

1216
_File = Union[Text, IO[bytes]]
1317
_Marker = Tuple[int, int, bytes]

stdlib/2and3/crypt.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sys
2-
from typing import List, NamedTuple, Optional, Union
3-
2+
from typing import List, Optional, Union
43

54
if sys.version_info >= (3, 3):
65
class _Method: ...

stdlib/2and3/decimal.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ else:
1313
_ComparableNum = Union[Decimal, float]
1414
_DecimalT = TypeVar('_DecimalT', bound=Decimal)
1515

16-
DecimalTuple = NamedTuple('DecimalTuple',
17-
[('sign', int),
18-
('digits', Tuple[int, ...]),
19-
('exponent', int)])
16+
class DecimalTuple(NamedTuple):
17+
sign: int
18+
digits: Tuple[int, ...]
19+
exponent: int
2020

2121
ROUND_DOWN: str
2222
ROUND_HALF_UP: str

0 commit comments

Comments
 (0)