Unify file descriptor definitions (#3584)

The _types module can house any common type defintions used throughout
the rest of typeshed to keep defintions in sync.

First candidate is file descriptors where anything with `fileno()`
method is accepted. There were several different implementations in
various files that can be unified.
This commit is contained in:
Daniel Farley
2020-01-08 17:25:36 -08:00
committed by Rebecca Chen
parent 1651348a08
commit 955e9c7da4
10 changed files with 84 additions and 86 deletions

10
stdlib/2and3/_types.pyi Normal file
View File

@@ -0,0 +1,10 @@
# These types are for typeshed-only objects that don't exist at runtime
from typing import type_check_only, Protocol, Union
@type_check_only
class HasFileno(Protocol):
def fileno(self) -> int: ...
FileDescriptor = int
FileDescriptorLike = Union[int, HasFileno]

View File

@@ -7,6 +7,7 @@ import time
import warnings
from socket import SocketType
from typing import Optional
from _types import FileDescriptorLike
from errno import (EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL,
ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED,
@@ -141,5 +142,5 @@ class file_wrapper:
def fileno(self) -> int: ...
class file_dispatcher(dispatcher):
def __init__(self, fd: int, map: _maptype = ...) -> None: ...
def __init__(self, fd: FileDescriptorLike, map: _maptype = ...) -> None: ...
def set_file(self, fd: int) -> None: ...

View File

@@ -1,10 +1,7 @@
import sys
from typing import Any, Iterable, List, Optional, Protocol, Tuple, Union
from _types import FileDescriptorLike
class _HasFileno(Protocol):
def fileno(self) -> int: ...
_FileDescriptor = Union[int, _HasFileno]
EPOLLERR: int
EPOLLET: int
@@ -71,9 +68,9 @@ POLLWRNORM: int
class poll:
def __init__(self) -> None: ...
def register(self, fd: _FileDescriptor, eventmask: int = ...) -> None: ...
def modify(self, fd: _FileDescriptor, eventmask: int) -> None: ...
def unregister(self, fd: _FileDescriptor) -> None: ...
def register(self, fd: FileDescriptorLike, eventmask: int = ...) -> None: ...
def modify(self, fd: FileDescriptorLike, eventmask: int) -> None: ...
def unregister(self, fd: FileDescriptorLike) -> None: ...
def poll(self, timeout: Optional[float] = ...) -> List[Tuple[int, int]]: ...
def select(rlist: Iterable[Any], wlist: Iterable[Any], xlist: Iterable[Any],
@@ -94,7 +91,7 @@ class kevent(object):
flags: int
ident: int
udata: Any
def __init__(self, ident: _FileDescriptor, filter: int = ..., flags: int = ..., fflags: int = ..., data: Any = ..., udata: Any = ...) -> None: ...
def __init__(self, ident: FileDescriptorLike, filter: int = ..., flags: int = ..., fflags: int = ..., data: Any = ..., udata: Any = ...) -> None: ...
# BSD only
class kqueue(object):
@@ -104,7 +101,7 @@ class kqueue(object):
def control(self, changelist: Optional[Iterable[kevent]], max_events: int, timeout: float = ...) -> List[kevent]: ...
def fileno(self) -> int: ...
@classmethod
def fromfd(cls, fd: _FileDescriptor) -> kqueue: ...
def fromfd(cls, fd: FileDescriptorLike) -> kqueue: ...
# Linux only
class epoll(object):
@@ -118,12 +115,12 @@ class epoll(object):
def close(self) -> None: ...
closed: bool
def fileno(self) -> int: ...
def register(self, fd: _FileDescriptor, eventmask: int = ...) -> None: ...
def modify(self, fd: _FileDescriptor, eventmask: int) -> None: ...
def unregister(self, fd: _FileDescriptor) -> None: ...
def register(self, fd: FileDescriptorLike, eventmask: int = ...) -> None: ...
def modify(self, fd: FileDescriptorLike, eventmask: int) -> None: ...
def unregister(self, fd: FileDescriptorLike) -> None: ...
def poll(self, timeout: float = ..., maxevents: int = ...) -> List[Tuple[int, int]]: ...
@classmethod
def fromfd(cls, fd: _FileDescriptor) -> epoll: ...
def fromfd(cls, fd: FileDescriptorLike) -> epoll: ...
if sys.version_info >= (3, 3):
# Solaris only
@@ -132,7 +129,7 @@ if sys.version_info >= (3, 3):
def close(self) -> None: ...
closed: bool
def fileno(self) -> int: ...
def register(self, fd: _FileDescriptor, eventmask: int = ...) -> None: ...
def modify(self, fd: _FileDescriptor, eventmask: int = ...) -> None: ...
def unregister(self, fd: _FileDescriptor) -> None: ...
def register(self, fd: FileDescriptorLike, eventmask: int = ...) -> None: ...
def modify(self, fd: FileDescriptorLike, eventmask: int = ...) -> None: ...
def unregister(self, fd: FileDescriptorLike) -> None: ...
def poll(self, timeout: Optional[float] = ...) -> List[Tuple[int, int]]: ...

View File

@@ -1,8 +1,8 @@
# Stubs for termios
from typing import IO, List, Union
from _types import FileDescriptorLike
_FD = Union[int, IO[str]]
_Attr = List[Union[int, List[bytes]]]
# TODO constants not really documented
@@ -238,11 +238,11 @@ VWERASE: int
XCASE: int
XTABS: int
def tcgetattr(fd: _FD) -> _Attr: ...
def tcsetattr(fd: _FD, when: int, attributes: _Attr) -> None: ...
def tcsendbreak(fd: _FD, duration: int) -> None: ...
def tcdrain(fd: _FD) -> None: ...
def tcflush(fd: _FD, queue: int) -> None: ...
def tcflow(fd: _FD, action: int) -> None: ...
def tcgetattr(fd: FileDescriptorLike) -> _Attr: ...
def tcsetattr(fd: FileDescriptorLike, when: int, attributes: _Attr) -> None: ...
def tcsendbreak(fd: FileDescriptorLike, duration: int) -> None: ...
def tcdrain(fd: FileDescriptorLike) -> None: ...
def tcflush(fd: FileDescriptorLike, queue: int) -> None: ...
def tcflow(fd: FileDescriptorLike, action: int) -> None: ...
class error(Exception): ...