From ec7960a8cb2e2ff465ee0fff841cd28fa85fe3ba Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 20 Oct 2019 10:37:33 +0200 Subject: [PATCH] Convert namedtuples to class syntax (#3321) --- stdlib/2/functools.pyi | 1 - stdlib/2/inspect.pyi | 65 ++++++++-------- stdlib/2/os/__init__.pyi | 32 +++----- stdlib/2/resource.pyi | 24 ++++-- stdlib/2/spwd.pyi | 19 ++--- stdlib/2/urlparse.pyi | 34 ++++----- stdlib/2and3/_curses.pyi | 5 +- stdlib/2and3/aifc.pyi | 10 ++- stdlib/2and3/crypt.pyi | 3 +- stdlib/2and3/decimal.pyi | 8 +- stdlib/2and3/difflib.pyi | 9 +-- stdlib/2and3/dis.pyi | 22 +++--- stdlib/2and3/doctest.pyi | 7 +- stdlib/2and3/grp.pyi | 9 ++- stdlib/2and3/pkgutil.pyi | 5 +- stdlib/2and3/sched.pyi | 13 ++-- stdlib/2and3/shutil.pyi | 7 +- stdlib/2and3/sndhdr.pyi | 13 ++-- stdlib/2and3/ssl.pyi | 20 +++-- stdlib/2and3/sunau.pyi | 15 ++-- stdlib/2and3/time.pyi | 41 +++++----- stdlib/2and3/warnings.pyi | 14 ++-- stdlib/2and3/wave.pyi | 15 ++-- stdlib/3/_thread.pyi | 14 ++-- stdlib/3/functools.pyi | 12 ++- stdlib/3/inspect.pyi | 107 +++++++++++++-------------- stdlib/3/nntplib.pyi | 20 +++-- stdlib/3/os/__init__.pyi | 4 +- stdlib/3/platform.pyi | 8 +- stdlib/3/posix.pyi | 45 +++++------ stdlib/3/resource.pyi | 23 ++++-- stdlib/3/selectors.pyi | 14 ++-- stdlib/3/spwd.pyi | 19 ++--- stdlib/3/tokenize.pyi | 13 ++-- stdlib/3/urllib/parse.pyi | 50 +++++++------ stdlib/3/urllib/robotparser.pyi | 4 +- third_party/2/tornado/gen.pyi | 7 +- third_party/2/tornado/httputil.pyi | 14 +++- third_party/2and3/decorator.pyi | 16 ++-- third_party/2and3/jinja2/filters.pyi | 4 +- third_party/2and3/werkzeug/urls.pyi | 15 ++-- 41 files changed, 397 insertions(+), 383 deletions(-) diff --git a/stdlib/2/functools.pyi b/stdlib/2/functools.pyi index 5d65d23c9..dc812962a 100644 --- a/stdlib/2/functools.pyi +++ b/stdlib/2/functools.pyi @@ -4,7 +4,6 @@ from abc import ABCMeta, abstractmethod from typing import Any, Callable, Generic, Dict, Iterable, Optional, Sequence, Tuple, TypeVar, overload -from collections import namedtuple _AnyCallable = Callable[..., Any] diff --git a/stdlib/2/inspect.pyi b/stdlib/2/inspect.pyi index b1acc3040..c5ed0ca7f 100644 --- a/stdlib/2/inspect.pyi +++ b/stdlib/2/inspect.pyi @@ -22,11 +22,12 @@ CO_VARARGS: int CO_VARKEYWORDS: int TPFLAGS_IS_ABSTRACT: int -ModuleInfo = NamedTuple('ModuleInfo', [('name', str), - ('suffix', str), - ('mode', str), - ('module_type', int), - ]) +class ModuleInfo(NamedTuple): + name: str + suffix: str + mode: str + module_type: int + def getmembers( object: object, predicate: Optional[Callable[[Any], bool]] = ... @@ -70,22 +71,22 @@ def indentsize(line: str) -> int: ... # Classes and functions def getclasstree(classes: List[type], unique: bool = ...) -> List[Union[Tuple[type, Tuple[type, ...]], List[Any]]]: ... -ArgSpec = NamedTuple('ArgSpec', [('args', List[str]), - ('varargs', Optional[str]), - ('keywords', Optional[str]), - ('defaults', Tuple[Any, ...]), - ]) +class ArgSpec(NamedTuple): + args: List[str] + varargs: Optional[str] + keywords: Optional[str] + defaults: Tuple[Any, ...] -ArgInfo = NamedTuple('ArgInfo', [('args', List[str]), - ('varargs', Optional[str]), - ('keywords', Optional[str]), - ('locals', Dict[str, Any]), - ]) +class ArgInfo(NamedTuple): + args: List[str] + varargs: Optional[str] + keywords: Optional[str] + locals: Dict[str, Any] -Arguments = NamedTuple('Arguments', [('args', List[Union[str, List[Any]]]), - ('varargs', Optional[str]), - ('keywords', Optional[str]), - ]) +class Arguments(NamedTuple): + args: List[Union[str, List[Any]]] + varargs: Optional[str] + keywords: Optional[str] def getargs(co: CodeType) -> Arguments: ... def getargspec(func: object) -> ArgSpec: ... @@ -101,16 +102,12 @@ def getcallargs(func, *args, **kwds) -> Dict[str, Any]: ... # The interpreter stack -Traceback = NamedTuple( - 'Traceback', - [ - ('filename', str), - ('lineno', int), - ('function', str), - ('code_context', Optional[List[str]]), - ('index', Optional[int]), - ] -) +class Traceback(NamedTuple): + filename: str + lineno: int + function: str + code_context: Optional[List[str]] + index: Optional[int] # type: ignore _FrameInfo = Tuple[FrameType, str, int, str, Optional[List[str]], Optional[int]] @@ -123,10 +120,10 @@ def currentframe(depth: int = ...) -> FrameType: ... def stack(context: int = ...) -> List[_FrameInfo]: ... def trace(context: int = ...) -> List[_FrameInfo]: ... -Attribute = NamedTuple('Attribute', [('name', str), - ('kind', str), - ('defining_class', type), - ('object', object), - ]) +class Attribute(NamedTuple): + name: str + kind: str + defining_class: type + object: object def classify_class_attrs(cls: type) -> List[Attribute]: ... diff --git a/stdlib/2/os/__init__.pyi b/stdlib/2/os/__init__.pyi index 63407c289..7041e1fbe 100644 --- a/stdlib/2/os/__init__.pyi +++ b/stdlib/2/os/__init__.pyi @@ -136,10 +136,17 @@ if sys.version_info >= (3, 6): _PathType = path._PathType -_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 _StatVFS(NamedTuple): + 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 getlogin() -> str: ... def getpid() -> int: ... @@ -349,20 +356,3 @@ if sys.version_info < (3, 0): P_ALL: int WEXITED: int WNOWAIT: int - -if sys.version_info >= (3, 3): - if sys.platform != 'win32': - # Unix only - def sync() -> None: ... - - 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]]: ... - - 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/2/resource.pyi b/stdlib/2/resource.pyi index 2a6c6947a..3763bc25b 100644 --- a/stdlib/2/resource.pyi +++ b/stdlib/2/resource.pyi @@ -19,12 +19,24 @@ RLIMIT_MEMLOCK: int RLIMIT_VMEM: int RLIMIT_AS: int -_RUsage = NamedTuple('_RUsage', [('ru_utime', float), ('ru_stime', float), ('ru_maxrss', int), - ('ru_ixrss', int), ('ru_idrss', int), ('ru_isrss', int), - ('ru_minflt', int), ('ru_majflt', int), ('ru_nswap', int), - ('ru_inblock', int), ('ru_oublock', int), ('ru_msgsnd', int), - ('ru_msgrcv', int), ('ru_nsignals', int), ('ru_nvcsw', int), - ('ru_nivcsw', int)]) +class _RUsage(NamedTuple): + ru_utime: float + ru_stime: float + ru_maxrss: int + ru_ixrss: int + ru_idrss: int + ru_isrss: int + ru_minflt: int + ru_majflt: int + ru_nswap: int + ru_inblock: int + ru_oublock: int + ru_msgsnd: int + ru_msgrcv: int + ru_nsignals: int + ru_nvcsw: int + ru_nivcsw: int + def getrusage(who: int) -> _RUsage: ... def getpagesize() -> int: ... diff --git a/stdlib/2/spwd.pyi b/stdlib/2/spwd.pyi index 1d5899031..756c142a6 100644 --- a/stdlib/2/spwd.pyi +++ b/stdlib/2/spwd.pyi @@ -1,14 +1,15 @@ from typing import List, NamedTuple -struct_spwd = NamedTuple("struct_spwd", [("sp_nam", str), - ("sp_pwd", str), - ("sp_lstchg", int), - ("sp_min", int), - ("sp_max", int), - ("sp_warn", int), - ("sp_inact", int), - ("sp_expire", int), - ("sp_flag", int)]) +class struct_spwd(NamedTuple): + sp_nam: str + sp_pwd: str + sp_lstchg: int + sp_min: int + sp_max: int + sp_warn: int + sp_inact: int + sp_expire: int + sp_flag: int def getspall() -> List[struct_spwd]: ... def getspnam(name: str) -> struct_spwd: ... diff --git a/stdlib/2/urlparse.pyi b/stdlib/2/urlparse.pyi index f1d1e3f28..4f54dde15 100644 --- a/stdlib/2/urlparse.pyi +++ b/stdlib/2/urlparse.pyi @@ -25,27 +25,23 @@ class ResultMixin(object): @property def port(self) -> Optional[int]: ... -class SplitResult( - NamedTuple( - 'SplitResult', - [ - ('scheme', str), ('netloc', str), ('path', str), ('query', str), ('fragment', str) - ] - ), - ResultMixin -): +class _SplitResult(NamedTuple): + scheme: str + netloc: str + path: str + query: str + fragment: str +class SplitResult(_SplitResult, ResultMixin): def geturl(self) -> str: ... -class ParseResult( - NamedTuple( - 'ParseResult', - [ - ('scheme', str), ('netloc', str), ('path', str), ('params', str), ('query', str), - ('fragment', str) - ] - ), - ResultMixin -): +class _ParseResult(NamedTuple): + scheme: str + netloc: str + path: str + params: str + query: str + fragment: str +class ParseResult(_ParseResult, ResultMixin): def geturl(self) -> str: ... def urlparse(url: _String, scheme: _String = ..., diff --git a/stdlib/2and3/_curses.pyi b/stdlib/2and3/_curses.pyi index fcfa2f1d8..4126f36de 100644 --- a/stdlib/2and3/_curses.pyi +++ b/stdlib/2and3/_curses.pyi @@ -454,5 +454,8 @@ class _CursesWindow: def vline(self, y: int, x: int, ch: _chtype, n: int) -> None: ... if sys.version_info >= (3, 8): - _ncurses_version = NamedTuple("ncurses_version", [("major", int), ("minor", int), ("patch", int)]) + class _ncurses_version(NamedTuple): + major: int + minor: int + patch: int ncurses_version: _ncurses_version diff --git a/stdlib/2and3/aifc.pyi b/stdlib/2and3/aifc.pyi index a141125a9..c2fd608de 100644 --- a/stdlib/2and3/aifc.pyi +++ b/stdlib/2and3/aifc.pyi @@ -1,4 +1,3 @@ - from typing import Union, IO, Optional, Type, NamedTuple, List, Tuple, Any, Text, overload from typing_extensions import Literal from types import TracebackType @@ -6,8 +5,13 @@ import sys class Error(Exception): ... -_aifc_params = NamedTuple('_aifc_params', [('nchannels', int), ('sampwidth', int), ('framerate', int), - ('nframes', int), ('comptype', bytes), ('compname', bytes)]) +class _aifc_params(NamedTuple): + nchannels: int + sampwidth: int + framerate: int + nframes: int + comptype: bytes + compname: bytes _File = Union[Text, IO[bytes]] _Marker = Tuple[int, int, bytes] diff --git a/stdlib/2and3/crypt.pyi b/stdlib/2and3/crypt.pyi index d55fc263e..621ce0bd7 100644 --- a/stdlib/2and3/crypt.pyi +++ b/stdlib/2and3/crypt.pyi @@ -1,6 +1,5 @@ import sys -from typing import List, NamedTuple, Optional, Union - +from typing import List, Optional, Union if sys.version_info >= (3, 3): class _Method: ... diff --git a/stdlib/2and3/decimal.pyi b/stdlib/2and3/decimal.pyi index 2018ed844..06193c7a7 100644 --- a/stdlib/2and3/decimal.pyi +++ b/stdlib/2and3/decimal.pyi @@ -13,10 +13,10 @@ else: _ComparableNum = Union[Decimal, float] _DecimalT = TypeVar('_DecimalT', bound=Decimal) -DecimalTuple = NamedTuple('DecimalTuple', - [('sign', int), - ('digits', Tuple[int, ...]), - ('exponent', int)]) +class DecimalTuple(NamedTuple): + sign: int + digits: Tuple[int, ...] + exponent: int ROUND_DOWN: str ROUND_HALF_UP: str diff --git a/stdlib/2and3/difflib.pyi b/stdlib/2and3/difflib.pyi index ddcec11dd..197c5d03c 100644 --- a/stdlib/2and3/difflib.pyi +++ b/stdlib/2and3/difflib.pyi @@ -16,11 +16,10 @@ else: _JunkCallback = Union[Callable[[Text], bool], Callable[[str], bool]] -Match = NamedTuple('Match', [ - ('a', int), - ('b', int), - ('size', int), -]) +class Match(NamedTuple): + a: int + b: int + size: int class SequenceMatcher(Generic[_T]): def __init__(self, isjunk: Optional[Callable[[_T], bool]] = ..., diff --git a/stdlib/2and3/dis.pyi b/stdlib/2and3/dis.pyi index 0ef27f4af..22630a5b2 100644 --- a/stdlib/2and3/dis.pyi +++ b/stdlib/2and3/dis.pyi @@ -21,19 +21,15 @@ _have_code_or_string = Union[_have_code, str, bytes] if sys.version_info >= (3, 4): - Instruction = NamedTuple( - "Instruction", - [ - ('opname', str), - ('opcode', int), - ('arg', Optional[int]), - ('argval', Any), - ('argrepr', str), - ('offset', int), - ('starts_line', Optional[int]), - ('is_jump_target', bool) - ] - ) + class Instruction(NamedTuple): + opname: str + opcode: int + arg: Optional[int] + argval: Any + argrepr: str + offset: int + starts_line: Optional[int] + is_jump_target: bool class Bytecode: codeobj: types.CodeType diff --git a/stdlib/2and3/doctest.pyi b/stdlib/2and3/doctest.pyi index b6df8c5b0..54a8c8745 100644 --- a/stdlib/2and3/doctest.pyi +++ b/stdlib/2and3/doctest.pyi @@ -4,10 +4,9 @@ import sys import types import unittest -TestResults = NamedTuple('TestResults', [ - ('failed', int), - ('attempted', int), -]) +class TestResults(NamedTuple): + failed: int + attempted: int OPTIONFLAGS_BY_NAME: Dict[str, int] def register_optionflag(name: str) -> int: ... diff --git a/stdlib/2and3/grp.pyi b/stdlib/2and3/grp.pyi index 605472721..5b5855583 100644 --- a/stdlib/2and3/grp.pyi +++ b/stdlib/2and3/grp.pyi @@ -1,9 +1,10 @@ from typing import List, NamedTuple, Optional -struct_group = NamedTuple("struct_group", [("gr_name", str), - ("gr_passwd", Optional[str]), - ("gr_gid", int), - ("gr_mem", List[str])]) +class struct_group(NamedTuple): + gr_name: str + gr_passwd: Optional[str] + gr_gid: int + gr_mem: List[str] def getgrall() -> List[struct_group]: ... def getgrgid(gid: int) -> struct_group: ... diff --git a/stdlib/2and3/pkgutil.pyi b/stdlib/2and3/pkgutil.pyi index 122dff000..138a0f8b5 100644 --- a/stdlib/2and3/pkgutil.pyi +++ b/stdlib/2and3/pkgutil.pyi @@ -9,7 +9,10 @@ else: Loader = Any if sys.version_info >= (3, 6): - ModuleInfo = NamedTuple('ModuleInfo', [('module_finder', Any), ('name', str), ('ispkg', bool)]) + class ModuleInfo(NamedTuple): + module_finder: Any + name: str + ispkg: bool _YMFNI = Generator[ModuleInfo, None, None] else: _YMFNI = Generator[Tuple[Any, str, bool], None, None] diff --git a/stdlib/2and3/sched.pyi b/stdlib/2and3/sched.pyi index 5d5cf33d2..6a820c836 100644 --- a/stdlib/2and3/sched.pyi +++ b/stdlib/2and3/sched.pyi @@ -1,13 +1,12 @@ import sys from typing import Any, Callable, Dict, List, NamedTuple, Optional, Text, Tuple -Event = NamedTuple('Event', [ - ('time', float), - ('priority', Any), - ('action', Callable[..., Any]), - ('argument', Tuple[Any, ...]), - ('kwargs', Dict[Text, Any]), -]) +class Event(NamedTuple): + time: float + priority: Any + action: Callable[..., Any] + argument: Tuple[Any, ...] + kwargs: Dict[Text, Any] class scheduler: if sys.version_info >= (3, 3): diff --git a/stdlib/2and3/shutil.pyi b/stdlib/2and3/shutil.pyi index 81ef46f56..b4c6cc129 100644 --- a/stdlib/2and3/shutil.pyi +++ b/stdlib/2and3/shutil.pyi @@ -114,9 +114,10 @@ else: def move(src: _Path, dst: _Path) -> _PathReturn: ... if sys.version_info >= (3,): - _ntuple_diskusage = NamedTuple('usage', [('total', int), - ('used', int), - ('free', int)]) + class _ntuple_diskusage(NamedTuple): + total: int + used: int + free: int def disk_usage(path: _Path) -> _ntuple_diskusage: ... def chown(path: _Path, user: Optional[str] = ..., group: Optional[str] = ...) -> None: ... diff --git a/stdlib/2and3/sndhdr.pyi b/stdlib/2and3/sndhdr.pyi index aecd70b46..7b742c0a9 100644 --- a/stdlib/2and3/sndhdr.pyi +++ b/stdlib/2and3/sndhdr.pyi @@ -5,13 +5,12 @@ import sys from typing import Any, NamedTuple, Optional, Tuple, Union if sys.version_info >= (3, 5): - SndHeaders = NamedTuple('SndHeaders', [ - ('filetype', str), - ('framerate', int), - ('nchannels', int), - ('nframes', int), - ('sampwidth', Union[int, str]), - ]) + class SndHeaders(NamedTuple): + filetype: str + framerate: int + nchannels: int + nframes: int + sampwidth: Union[int, str] _SndHeaders = SndHeaders else: _SndHeaders = Tuple[str, int, int, int, Union[int, str]] diff --git a/stdlib/2and3/ssl.pyi b/stdlib/2and3/ssl.pyi index cbc4c89f7..3dd685ff0 100644 --- a/stdlib/2and3/ssl.pyi +++ b/stdlib/2and3/ssl.pyi @@ -87,12 +87,13 @@ def get_server_certificate(addr: Tuple[str, int], ssl_version: int = ..., ca_certs: Optional[str] = ...) -> str: ... def DER_cert_to_PEM_cert(der_cert_bytes: bytes) -> str: ... def PEM_cert_to_DER_cert(pem_cert_string: str) -> bytes: ... -DefaultVerifyPaths = NamedTuple('DefaultVerifyPaths', - [('cafile', str), ('capath', str), - ('openssl_cafile_env', str), - ('openssl_cafile', str), - ('openssl_capath_env', str), - ('openssl_capath', str)]) +class DefaultVerifyPaths(NamedTuple): + cafile: str + capath: str + openssl_cafile_env: str + openssl_cafile: str + openssl_capath_env: str + openssl_capath: str def get_default_verify_paths() -> DefaultVerifyPaths: ... if sys.platform == 'win32': @@ -173,13 +174,16 @@ ALERT_DESCRIPTION_UNSUPPORTED_CERTIFICATE: int ALERT_DESCRIPTION_UNSUPPORTED_EXTENSION: int ALERT_DESCRIPTION_USER_CANCELLED: int +class _ASN1Object(NamedTuple): + nid: int + shortname: str + longname: str + oid: str if sys.version_info < (3,): - class _ASN1Object(NamedTuple('_ASN1Object', [('nid', int), ('shortname', str), ('longname', str), ('oid', str)])): ... class Purpose(_ASN1Object): SERVER_AUTH: ClassVar[Purpose] CLIENT_AUTH: ClassVar[Purpose] else: - class _ASN1Object(NamedTuple('_ASN1Object', [('nid', int), ('shortname', str), ('longname', str), ('oid', str)])): ... class Purpose(_ASN1Object, enum.Enum): SERVER_AUTH: _ASN1Object CLIENT_AUTH: _ASN1Object diff --git a/stdlib/2and3/sunau.pyi b/stdlib/2and3/sunau.pyi index 6577c316c..b957f5808 100644 --- a/stdlib/2and3/sunau.pyi +++ b/stdlib/2and3/sunau.pyi @@ -25,14 +25,13 @@ AUDIO_UNKNOWN_SIZE: int if sys.version_info < (3, 0): _sunau_params = Tuple[int, int, int, int, str, str] else: - _sunau_params = NamedTuple('_sunau_params', [ - ('nchannels', int), - ('sampwidth', int), - ('framerate', int), - ('nframes', int), - ('comptype', str), - ('compname', str), - ]) + class _sunau_params(NamedTuple): + nchannels: int + sampwidth: int + framerate: int + nframes: int + comptype: str + compname: str class Au_read: def __init__(self, f: _File) -> None: ... diff --git a/stdlib/2and3/time.pyi b/stdlib/2and3/time.pyi index e3cdcdd8f..0c2370ad6 100644 --- a/stdlib/2and3/time.pyi +++ b/stdlib/2and3/time.pyi @@ -32,15 +32,19 @@ if sys.version_info >= (3, 8) and sys.platform == "darwin": CLOCK_UPTIME_RAW: int if sys.version_info >= (3, 3): - class struct_time( - NamedTuple( - '_struct_time', - [('tm_year', int), ('tm_mon', int), ('tm_mday', int), - ('tm_hour', int), ('tm_min', int), ('tm_sec', int), - ('tm_wday', int), ('tm_yday', int), ('tm_isdst', int), - ('tm_zone', str), ('tm_gmtoff', int)] - ) - ): + class _struct_time(NamedTuple): + tm_year: int + tm_mon: int + tm_mday: int + tm_hour: int + tm_min: int + tm_sec: int + tm_wday: int + tm_yday: int + tm_isdst: int + tm_zone: str + tm_gmtoff: int + class struct_time(_struct_time): def __init__( self, o: Union[ @@ -60,14 +64,17 @@ if sys.version_info >= (3, 3): _arg: Any = ..., ) -> struct_time: ... else: - class struct_time( - NamedTuple( - '_struct_time', - [('tm_year', int), ('tm_mon', int), ('tm_mday', int), - ('tm_hour', int), ('tm_min', int), ('tm_sec', int), - ('tm_wday', int), ('tm_yday', int), ('tm_isdst', int)] - ) - ): + class _struct_time(NamedTuple): + tm_year: int + tm_mon: int + tm_mday: int + tm_hour: int + tm_min: int + tm_sec: int + tm_wday: int + tm_yday: int + tm_isdst: int + class struct_time(_struct_time): def __init__(self, o: _TimeTuple, _arg: Any = ...) -> None: ... def __new__(cls, o: _TimeTuple, _arg: Any = ...) -> struct_time: ... diff --git a/stdlib/2and3/warnings.pyi b/stdlib/2and3/warnings.pyi index d9ecec74f..138b71341 100644 --- a/stdlib/2and3/warnings.pyi +++ b/stdlib/2and3/warnings.pyi @@ -29,13 +29,13 @@ def simplefilter(action: str, category: Type[Warning] = ..., lineno: int = ..., append: bool = ...) -> None: ... def resetwarnings() -> None: ... -_Record = NamedTuple('_Record', - [('message', str), - ('category', Type[Warning]), - ('filename', str), - ('lineno', int), - ('file', Optional[TextIO]), - ('line', Optional[str])]) +class _Record(NamedTuple): + message: str + category: Type[Warning] + filename: str + lineno: int + file: Optional[TextIO] + line: Optional[str] class catch_warnings: def __init__(self, *, record: bool = ..., diff --git a/stdlib/2and3/wave.pyi b/stdlib/2and3/wave.pyi index 951142aa3..52bc9b156 100644 --- a/stdlib/2and3/wave.pyi +++ b/stdlib/2and3/wave.pyi @@ -14,14 +14,13 @@ WAVE_FORMAT_PCM: int if sys.version_info < (3, 0): _wave_params = Tuple[int, int, int, int, str, str] else: - _wave_params = NamedTuple('_wave_params', [ - ('nchannels', int), - ('sampwidth', int), - ('framerate', int), - ('nframes', int), - ('comptype', str), - ('compname', str), - ]) + class _wave_params(NamedTuple): + nchannels: int + sampwidth: int + framerate: int + nframes: int + comptype: str + compname: str class Wave_read: def __init__(self, f: _File) -> None: ... diff --git a/stdlib/3/_thread.pyi b/stdlib/3/_thread.pyi index 7cd34a045..051edefbb 100644 --- a/stdlib/3/_thread.pyi +++ b/stdlib/3/_thread.pyi @@ -35,14 +35,10 @@ TIMEOUT_MAX: int if sys.version_info >= (3, 8): def get_native_id() -> int: ... # only available on some platforms - ExceptHookArgs = NamedTuple( - "ExceptHookArgs", - [ - ("exc_type", Type[BaseException]), - ("exc_value", Optional[BaseException]), - ("exc_traceback", Optional[TracebackType]), - ("thread", Optional[Thread]), - ] - ) + class ExceptHookArgs(NamedTuple): + exc_type: Type[BaseException] + exc_value: Optional[BaseException] + exc_traceback: Optional[TracebackType] + thread: Optional[Thread] def _ExceptHookArgs(args) -> ExceptHookArgs: ... _excepthook: Callable[[ExceptHookArgs], Any] diff --git a/stdlib/3/functools.pyi b/stdlib/3/functools.pyi index afffb3a41..78a3466ea 100644 --- a/stdlib/3/functools.pyi +++ b/stdlib/3/functools.pyi @@ -13,13 +13,11 @@ def reduce(function: Callable[[_T, _S], _T], def reduce(function: Callable[[_T, _T], _T], sequence: Iterable[_T]) -> _T: ... - -class _CacheInfo(NamedTuple('CacheInfo', [ - ('hits', int), - ('misses', int), - ('maxsize', int), - ('currsize', int) -])): ... +class _CacheInfo(NamedTuple): + hits: int + misses: int + maxsize: int + currsize: int class _lru_cache_wrapper(Generic[_T]): __wrapped__: Callable[..., _T] diff --git a/stdlib/3/inspect.pyi b/stdlib/3/inspect.pyi index 975b47828..6c1afd5e4 100644 --- a/stdlib/3/inspect.pyi +++ b/stdlib/3/inspect.pyi @@ -37,11 +37,11 @@ if sys.version_info >= (3, 6): TPFLAGS_IS_ABSTRACT: int if sys.version_info < (3, 6): - ModuleInfo = NamedTuple('ModuleInfo', [('name', str), - ('suffix', str), - ('mode', str), - ('module_type', int), - ]) + class ModuleInfo(NamedTuple): + name: str + suffix: str + mode: str + module_type: int def getmoduleinfo(path: str) -> Optional[ModuleInfo]: ... def getmembers(object: object, @@ -170,37 +170,36 @@ class BoundArguments: # _ClassTreeItem = Union[List[_ClassTreeItem], Tuple[type, Tuple[type, ...]]] def getclasstree(classes: List[type], unique: bool = ...) -> Any: ... -ArgSpec = NamedTuple('ArgSpec', [('args', List[str]), - ('varargs', str), - ('keywords', str), - ('defaults', Tuple[Any, ...]), - ]) +class ArgSpec(NamedTuple): + args: List[str] + varargs: str + keywords: str + defaults: Tuple[Any, ...] -Arguments = NamedTuple('Arguments', [('args', List[str]), - ('varargs', Optional[str]), - ('varkw', Optional[str]), - ]) +class Arguments(NamedTuple): + args: List[str] + varargs: Optional[str] + varkw: Optional[str] def getargs(co: CodeType) -> Arguments: ... def getargspec(func: object) -> ArgSpec: ... -FullArgSpec = NamedTuple('FullArgSpec', [('args', List[str]), - ('varargs', Optional[str]), - ('varkw', Optional[str]), - ('defaults', Optional[Tuple[Any, ...]]), - ('kwonlyargs', List[str]), - ('kwonlydefaults', Optional[Dict[str, Any]]), - ('annotations', Dict[str, Any]), - ]) +class FullArgSpec(NamedTuple): + args: List[str] + varargs: Optional[str] + varkw: Optional[str] + defaults: Optional[Tuple[Any, ...]] + kwonlyargs: List[str] + kwonlydefaults: Optional[Dict[str, Any]] + annotations: Dict[str, Any] def getfullargspec(func: object) -> FullArgSpec: ... -# TODO make the field types more specific here -ArgInfo = NamedTuple('ArgInfo', [('args', List[str]), - ('varargs', Optional[str]), - ('keywords', Optional[str]), - ('locals', Dict[str, Any]), - ]) +class ArgInfo(NamedTuple): + args: List[str] + varargs: Optional[str] + keywords: Optional[str] + locals: Dict[str, Any] def getargvalues(frame: FrameType) -> ArgInfo: ... def formatannotation(annotation: object, base_module: Optional[str] = ...) -> str: ... @@ -234,12 +233,11 @@ def getcallargs(func: Callable[..., Any], *args: Any, **kwds: Any) -> Dict[str, Any]: ... - -ClosureVars = NamedTuple('ClosureVars', [('nonlocals', Mapping[str, Any]), - ('globals', Mapping[str, Any]), - ('builtins', Mapping[str, Any]), - ('unbound', AbstractSet[str]), - ]) +class ClosureVars(NamedTuple): + nonlocals: Mapping[str, Any] + globals: Mapping[str, Any] + builtins: Mapping[str, Any] + unbound: AbstractSet[str] def getclosurevars(func: Callable[..., Any]) -> ClosureVars: ... def unwrap(func: Callable[..., Any], @@ -251,25 +249,20 @@ def unwrap(func: Callable[..., Any], # The interpreter stack # -Traceback = NamedTuple( - 'Traceback', - [ - ('filename', str), - ('lineno', int), - ('function', str), - ('code_context', Optional[List[str]]), - ('index', Optional[int]), - ] -) +class Traceback(NamedTuple): + filename: str + lineno: int + function: str + code_context: Optional[List[str]] + index: Optional[int] # type: ignore -# Python 3.5+ (functions returning it used to return regular tuples) -FrameInfo = NamedTuple('FrameInfo', [('frame', FrameType), - ('filename', str), - ('lineno', int), - ('function', str), - ('code_context', Optional[List[str]]), - ('index', Optional[int]), - ]) +class FrameInfo(NamedTuple): + frame: FrameType + filename: str + lineno: int + function: str + code_context: Optional[List[str]] + index: Optional[int] # type: ignore def getframeinfo(frame: Union[FrameType, TracebackType], context: int = ...) -> Traceback: ... def getouterframes(frame: Any, context: int = ...) -> List[FrameInfo]: ... @@ -311,10 +304,10 @@ def getgeneratorlocals(generator: Generator[Any, Any, Any]) -> Dict[str, Any]: . # TODO can we be more specific than "object"? def getcoroutinelocals(coroutine: object) -> Dict[str, Any]: ... -Attribute = NamedTuple('Attribute', [('name', str), - ('kind', str), - ('defining_class', type), - ('object', object), - ]) +class Attribute(NamedTuple): + name: str + kind: str + defining_class: type + object: object def classify_class_attrs(cls: type) -> List[Attribute]: ... diff --git a/stdlib/3/nntplib.pyi b/stdlib/3/nntplib.pyi index 00abdd994..78b078cb1 100644 --- a/stdlib/3/nntplib.pyi +++ b/stdlib/3/nntplib.pyi @@ -20,17 +20,15 @@ class NNTPDataError(NNTPError): ... NNTP_PORT: int NNTP_SSL_PORT: int -GroupInfo = NamedTuple('GroupInfo', [ - ('group', str), - ('last', str), - ('first', str), - ('flag', str), -]) -ArticleInfo = NamedTuple('ArticleInfo', [ - ('number', int), - ('message_id', str), - ('lines', List[bytes]), -]) +class GroupInfo(NamedTuple): + group: str + last: str + first: str + flag: str +class ArticleInfo(NamedTuple): + number: int + message_id: str + lines: List[bytes] def decode_header(header_str: str) -> str: ... diff --git a/stdlib/3/os/__init__.pyi b/stdlib/3/os/__init__.pyi index 6d7eef7eb..b17a55736 100644 --- a/stdlib/3/os/__init__.pyi +++ b/stdlib/3/os/__init__.pyi @@ -393,7 +393,9 @@ if sys.platform != 'win32': def readv(fd: int, buffers: Sequence[bytearray]) -> int: ... def writev(fd: int, buffers: Sequence[bytes]) -> int: ... -terminal_size = NamedTuple('terminal_size', [('columns', int), ('lines', int)]) +class terminal_size(NamedTuple): + columns: int + lines: int def get_terminal_size(fd: int = ...) -> terminal_size: ... def get_inheritable(fd: int) -> bool: ... diff --git a/stdlib/3/platform.pyi b/stdlib/3/platform.pyi index ca3a08165..858bb5930 100644 --- a/stdlib/3/platform.pyi +++ b/stdlib/3/platform.pyi @@ -12,7 +12,13 @@ def java_ver(release: str = ..., vendor: str = ..., vminfo: Tuple[str, str, str] def system_alias(system: str, release: str, version: str) -> Tuple[str, str, str]: ... def architecture(executable: str = ..., bits: str = ..., linkage: str = ...) -> Tuple[str, str]: ... -uname_result = NamedTuple('uname_result', [('system', str), ('node', str), ('release', str), ('version', str), ('machine', str), ('processor', str)]) +class uname_result(NamedTuple): + system: str + node: str + release: str + version: str + machine: str + processor: str def uname() -> uname_result: ... def system() -> str: ... diff --git a/stdlib/3/posix.pyi b/stdlib/3/posix.pyi index fe9782354..c6092ead4 100644 --- a/stdlib/3/posix.pyi +++ b/stdlib/3/posix.pyi @@ -10,34 +10,29 @@ from os import stat_result as stat_result if sys.version_info >= (3, 6): from builtins import _PathLike # See comment in builtins -uname_result = NamedTuple('uname_result', [ - ('sysname', str), - ('nodename', str), - ('release', str), - ('version', str), - ('machine', str), -]) +class uname_result(NamedTuple): + sysname: str + nodename: str + release: str + version: str + machine: str -times_result = NamedTuple('times_result', [ - ('user', float), - ('system', float), - ('children_user', float), - ('children_system', float), - ('elapsed', float), -]) +class times_result(NamedTuple): + user: float + system: float + children_user: float + children_system: float + elapsed: float -waitid_result = NamedTuple('waitid_result', [ - ('si_pid', int), - ('si_uid', int), - ('si_signo', int), - ('si_status', int), - ('si_code', int), -]) - -sched_param = NamedTuple('sched_param', [ - ('sched_priority', int), -]) +class waitid_result(NamedTuple): + si_pid: int + si_uid: int + si_signo: int + si_status: int + si_code: int +class sched_param(NamedTuple): + sched_priority: int EX_CANTCREAT: int EX_CONFIG: int diff --git a/stdlib/3/resource.pyi b/stdlib/3/resource.pyi index 0d2e30bb3..b4a6829f3 100644 --- a/stdlib/3/resource.pyi +++ b/stdlib/3/resource.pyi @@ -25,12 +25,23 @@ RUSAGE_CHILDREN: int RUSAGE_SELF: int RUSAGE_THREAD: int -_RUsage = NamedTuple('_RUsage', [('ru_utime', float), ('ru_stime', float), ('ru_maxrss', int), - ('ru_ixrss', int), ('ru_idrss', int), ('ru_isrss', int), - ('ru_minflt', int), ('ru_majflt', int), ('ru_nswap', int), - ('ru_inblock', int), ('ru_oublock', int), ('ru_msgsnd', int), - ('ru_msgrcv', int), ('ru_nsignals', int), ('ru_nvcsw', int), - ('ru_nivcsw', int)]) +class _RUsage(NamedTuple): + ru_utime: float + ru_stime: float + ru_maxrss: int + ru_ixrss: int + ru_idrss: int + ru_isrss: int + ru_minflt: int + ru_majflt: int + ru_nswap: int + ru_inblock: int + ru_oublock: int + ru_msgsnd: int + ru_msgrcv: int + ru_nsignals: int + ru_nvcsw: int + ru_nivcsw: int def getpagesize() -> int: ... def getrlimit(resource: int) -> Tuple[int, int]: ... diff --git a/stdlib/3/selectors.pyi b/stdlib/3/selectors.pyi index 6dd864795..99e311daa 100644 --- a/stdlib/3/selectors.pyi +++ b/stdlib/3/selectors.pyi @@ -12,18 +12,14 @@ _FileObject = Union[int, _HasFileno] _FileDescriptor = int _EventMask = int - EVENT_READ: _EventMask EVENT_WRITE: _EventMask - -SelectorKey = NamedTuple('SelectorKey', [ - ('fileobj', _FileObject), - ('fd', _FileDescriptor), - ('events', _EventMask), - ('data', Any) -]) - +class SelectorKey(NamedTuple): + fileobj: _FileObject + fd: _FileDescriptor + events: _EventMask + data: Any class BaseSelector(metaclass=ABCMeta): @abstractmethod diff --git a/stdlib/3/spwd.pyi b/stdlib/3/spwd.pyi index 0e55d7421..1fb972f6d 100644 --- a/stdlib/3/spwd.pyi +++ b/stdlib/3/spwd.pyi @@ -1,14 +1,15 @@ from typing import List, NamedTuple -struct_spwd = NamedTuple("struct_spwd", [("sp_namp", str), - ("sp_pwdp", str), - ("sp_lstchg", int), - ("sp_min", int), - ("sp_max", int), - ("sp_warn", int), - ("sp_inact", int), - ("sp_expire", int), - ("sp_flag", int)]) +class struct_spwd(NamedTuple): + sp_namp: str + sp_pwdp: str + sp_lstchg: int + sp_min: int + sp_max: int + sp_warn: int + sp_inact: int + sp_expire: int + sp_flag: int def getspall() -> List[struct_spwd]: ... def getspnam(name: str) -> struct_spwd: ... diff --git a/stdlib/3/tokenize.pyi b/stdlib/3/tokenize.pyi index 60b6ec97e..4a2959015 100644 --- a/stdlib/3/tokenize.pyi +++ b/stdlib/3/tokenize.pyi @@ -10,13 +10,12 @@ if sys.version_info < (3, 7): _Position = Tuple[int, int] -_TokenInfo = NamedTuple('TokenInfo', [ - ('type', int), - ('string', str), - ('start', _Position), - ('end', _Position), - ('line', str) -]) +class _TokenInfo(NamedTuple): + type: int + string: str + start: _Position + end: _Position + line: str class TokenInfo(_TokenInfo): @property diff --git a/stdlib/3/urllib/parse.pyi b/stdlib/3/urllib/parse.pyi index 7f8120ad4..fb303e5de 100644 --- a/stdlib/3/urllib/parse.pyi +++ b/stdlib/3/urllib/parse.pyi @@ -39,31 +39,33 @@ class _DefragResultBase(Tuple[Any, ...], Generic[AnyStr]): fragment: AnyStr -_SplitResultBase = NamedTuple( - '_SplitResultBase', - [ - ('scheme', str), ('netloc', str), ('path', str), ('query', str), ('fragment', str) - ] -) -_SplitResultBytesBase = NamedTuple( - '_SplitResultBytesBase', - [ - ('scheme', bytes), ('netloc', bytes), ('path', bytes), ('query', bytes), ('fragment', bytes) - ] -) +class _SplitResultBase(NamedTuple): + scheme: str + netloc: str + path: str + query: str + fragment: str +class _SplitResultBytesBase(NamedTuple): + scheme: bytes + netloc: bytes + path: bytes + query: bytes + fragment: bytes -_ParseResultBase = NamedTuple( - '_ParseResultBase', - [ - ('scheme', str), ('netloc', str), ('path', str), ('params', str), ('query', str), ('fragment', str) - ] -) -_ParseResultBytesBase = NamedTuple( - '_ParseResultBytesBase', - [ - ('scheme', bytes), ('netloc', bytes), ('path', bytes), ('params', bytes), ('query', bytes), ('fragment', bytes) - ] -) +class _ParseResultBase(NamedTuple): + scheme: str + netloc: str + path: str + params: str + query: str + fragment: str +class _ParseResultBytesBase(NamedTuple): + scheme: bytes + netloc: bytes + path: bytes + params: bytes + query: bytes + fragment: bytes # Structured result objects for string data class DefragResult(_DefragResultBase[str], _ResultMixinStr): ... diff --git a/stdlib/3/urllib/robotparser.pyi b/stdlib/3/urllib/robotparser.pyi index 36150ab01..2c5b657b1 100644 --- a/stdlib/3/urllib/robotparser.pyi +++ b/stdlib/3/urllib/robotparser.pyi @@ -3,7 +3,9 @@ from typing import Iterable, NamedTuple, Optional import sys -_RequestRate = NamedTuple('_RequestRate', [('requests', int), ('seconds', int)]) +class _RequestRate(NamedTuple): + requests: int + seconds: int class RobotFileParser: def __init__(self, url: str = ...) -> None: ... diff --git a/third_party/2/tornado/gen.pyi b/third_party/2/tornado/gen.pyi index ee3954b09..eac8278b3 100644 --- a/third_party/2/tornado/gen.pyi +++ b/third_party/2/tornado/gen.pyi @@ -1,5 +1,4 @@ -from typing import Any -from collections import namedtuple +from typing import Any, Dict, NamedTuple, Tuple singledispatch: Any @@ -104,6 +103,8 @@ class Runner: def result_callback(self, key): ... def handle_exception(self, typ, value, tb): ... -Arguments = namedtuple('Arguments', ['args', 'kwargs']) +class Arguments(NamedTuple): + args: Tuple[str, ...] + kwargs: Dict[str, Any] def convert_yielded(yielded): ... diff --git a/third_party/2/tornado/httputil.pyi b/third_party/2/tornado/httputil.pyi index 5f74cb57c..85a08ebcd 100644 --- a/third_party/2/tornado/httputil.pyi +++ b/third_party/2/tornado/httputil.pyi @@ -1,6 +1,6 @@ -from typing import Any, Dict +from typing import Any, Dict, NamedTuple + from tornado.util import ObjectDict -from collections import namedtuple class SSLError(Exception): ... @@ -79,11 +79,17 @@ def parse_body_arguments(content_type, body, arguments, files, headers=...): ... def parse_multipart_form_data(boundary, data, arguments, files): ... def format_timestamp(ts): ... -RequestStartLine = namedtuple('RequestStartLine', ['method', 'path', 'version']) +class RequestStartLine(NamedTuple): + method: str + path: str + version: str def parse_request_start_line(line): ... -ResponseStartLine = namedtuple('ResponseStartLine', ['version', 'code', 'reason']) +class ResponseStartLine(NamedTuple): + version: str + code: str + reason: str def parse_response_start_line(line): ... def doctests(): ... diff --git a/third_party/2and3/decorator.pyi b/third_party/2and3/decorator.pyi index 3443cd78b..adf5ebe7b 100644 --- a/third_party/2and3/decorator.pyi +++ b/third_party/2and3/decorator.pyi @@ -11,14 +11,14 @@ if sys.version_info >= (3,): from inspect import iscoroutinefunction as iscoroutinefunction from inspect import getfullargspec as getfullargspec else: - FullArgSpec = NamedTuple('FullArgSpec', [('args', List[str]), - ('varargs', Optional[str]), - ('varkw', Optional[str]), - ('defaults', Tuple[Any, ...]), - ('kwonlyargs', List[str]), - ('kwonlydefaults', Dict[str, Any]), - ('annotations', Dict[str, Any]), - ]) + class FullArgSpec(NamedTuple): + args: List[str] + varargs: Optional[str] + varkw: Optional[str] + defaults: Tuple[Any, ...] + kwonlyargs: List[str] + kwonlydefaults: Dict[str, Any] + annotations: Dict[str, Any] def iscoroutinefunction(f: Callable[..., Any]) -> bool: ... def getfullargspec(func: Any) -> FullArgSpec: ... diff --git a/third_party/2and3/jinja2/filters.pyi b/third_party/2and3/jinja2/filters.pyi index 44b7bb57e..8f0fb210a 100644 --- a/third_party/2and3/jinja2/filters.pyi +++ b/third_party/2and3/jinja2/filters.pyi @@ -37,7 +37,9 @@ def do_batch(value, linecount, fill_with: Optional[Any] = ...): ... def do_round(value, precision: int = ..., method: str = ...): ... def do_groupby(environment, value, attribute): ... -_GroupTuple = NamedTuple("_GroupTuple", [("grouper", Any), ("list", Any)]) +class _GroupTuple(NamedTuple): + grouper: Any + list: Any def do_sum(environment, iterable, attribute: Optional[Any] = ..., start: int = ...): ... def do_list(value): ... diff --git a/third_party/2and3/werkzeug/urls.pyi b/third_party/2and3/werkzeug/urls.pyi index e0b13069a..2853e46e1 100644 --- a/third_party/2and3/werkzeug/urls.pyi +++ b/third_party/2and3/werkzeug/urls.pyi @@ -1,12 +1,11 @@ -from collections import namedtuple -from typing import Any, Optional, Text - - -_URLTuple = namedtuple( - '_URLTuple', - ['scheme', 'netloc', 'path', 'query', 'fragment'] -) +from typing import Any, NamedTuple, Optional, Text +class _URLTuple(NamedTuple): + scheme: Any + netloc: Any + path: Any + query: Any + fragment: Any class BaseURL(_URLTuple): def replace(self, **kwargs): ...