From fdcbf9b3d26162484a6c68db708dd359ea0992e3 Mon Sep 17 00:00:00 2001 From: Nikhil Marathe Date: Wed, 22 Mar 2017 22:41:06 -0700 Subject: [PATCH 1/2] Improved annotations for select.select() The TypeVars allow mypy to enforce that the returned lists are subsets of the arguments. --- stdlib/2/select.pyi | 8 ++++++-- stdlib/3/select.pyi | 16 +++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/stdlib/2/select.pyi b/stdlib/2/select.pyi index a88311e971e2..d5821b499e59 100644 --- a/stdlib/2/select.pyi +++ b/stdlib/2/select.pyi @@ -1,6 +1,6 @@ """Stubs for the 'select' module.""" -from typing import Any, Optional, Tuple, Iterable, List +from typing import Any, Optional, Tuple, Iterable, List, Sequence, TypeVar EPOLLERR = ... # type: int EPOLLET = ... # type: int @@ -66,7 +66,11 @@ POLLWRBAND = ... # type: int POLLWRNORM = ... # type: int def poll() -> epoll: ... -def select(rlist, wlist, xlist, timeout: float = None) -> Tuple[List, List, List]: ... + +_R = TypeVar("_R") +_W = TypeVar("_W") +_X = TypeVar("_X") +def select(rlist: Sequence[_R], wlist: Sequence[_W], xlist: Sequence[_X], timeout: float = None) -> Tuple[List[_R], List[_W], List[_X]]: ... class error(Exception): ... diff --git a/stdlib/3/select.pyi b/stdlib/3/select.pyi index 83446f03d9d0..17a4a67e380b 100644 --- a/stdlib/3/select.pyi +++ b/stdlib/3/select.pyi @@ -2,7 +2,7 @@ # NOTE: These are incomplete! -from typing import Any, Tuple, List, Sequence +from typing import Any, Tuple, List, Sequence, TypeVar class error(Exception): ... @@ -21,7 +21,13 @@ class poll: def unregister(self, fd: Any) -> None: ... def poll(self, timeout: int = ...) -> List[Tuple[int, int]]: ... -def select(rlist: Sequence, wlist: Sequence, xlist: Sequence, - timeout: float = ...) -> Tuple[List[Any], - List[Any], - List[Any]]: ... +# Not the canonical naming choices, but these map to the select arguments. We +# need 3 because nothing in select prevents the read set from being socket +# objects and the write set from being file descriptors. +_R = TypeVar("_R") +_W = TypeVar("_W") +_X = TypeVar("_X") +def select(rlist: Sequence[_R], wlist: Sequence[_W], xlist: Sequence[_X], + timeout: float = ...) -> Tuple[List[_R], + List[_W], + List[_X]]: ... From 1b36346196788da271ff7374289460eafc0e45cb Mon Sep 17 00:00:00 2001 From: Nikhil Marathe Date: Thu, 23 Mar 2017 10:08:19 -0700 Subject: [PATCH 2/2] inline declaration similar to py2 version --- stdlib/3/select.pyi | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/stdlib/3/select.pyi b/stdlib/3/select.pyi index 17a4a67e380b..2f445121017d 100644 --- a/stdlib/3/select.pyi +++ b/stdlib/3/select.pyi @@ -27,7 +27,4 @@ class poll: _R = TypeVar("_R") _W = TypeVar("_W") _X = TypeVar("_X") -def select(rlist: Sequence[_R], wlist: Sequence[_W], xlist: Sequence[_X], - timeout: float = ...) -> Tuple[List[_R], - List[_W], - List[_X]]: ... +def select(rlist: Sequence[_R], wlist: Sequence[_W], xlist: Sequence[_X], timeout: float = ...) -> Tuple[List[_R], List[_W], List[_X]]: ...