Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions stdlib/selectors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ import sys
from _typeshed import FileDescriptor, FileDescriptorLike, Unused
from abc import ABCMeta, abstractmethod
from collections.abc import Mapping
from typing import Any, Final, NamedTuple, TypeAlias
from typing import Any, Final, NamedTuple
from typing_extensions import Self

_EventMask: TypeAlias = int

EVENT_READ: Final = 1
EVENT_WRITE: Final = 2

class SelectorKey(NamedTuple):
fileobj: FileDescriptorLike
fd: FileDescriptor
events: _EventMask
events: int
data: Any

class BaseSelector(metaclass=ABCMeta):
@abstractmethod
def register(self, fileobj: FileDescriptorLike, events: _EventMask, data: Any = None) -> SelectorKey: ...
def register(self, fileobj: FileDescriptorLike, events: int, data: Any = None) -> SelectorKey: ...
@abstractmethod
def unregister(self, fileobj: FileDescriptorLike) -> SelectorKey: ...
def modify(self, fileobj: FileDescriptorLike, events: _EventMask, data: Any = None) -> SelectorKey: ...
def modify(self, fileobj: FileDescriptorLike, events: int, data: Any = None) -> SelectorKey: ...
@abstractmethod
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, _EventMask]]: ...
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, int]]: ...
def close(self) -> None: ...
def get_key(self, fileobj: FileDescriptorLike) -> SelectorKey: ...
@abstractmethod
Expand All @@ -32,16 +30,16 @@ class BaseSelector(metaclass=ABCMeta):
def __exit__(self, *args: Unused) -> None: ...

class _BaseSelectorImpl(BaseSelector, metaclass=ABCMeta):
def register(self, fileobj: FileDescriptorLike, events: _EventMask, data: Any = None) -> SelectorKey: ...
def register(self, fileobj: FileDescriptorLike, events: int, data: Any = None) -> SelectorKey: ...
def unregister(self, fileobj: FileDescriptorLike) -> SelectorKey: ...
def modify(self, fileobj: FileDescriptorLike, events: _EventMask, data: Any = None) -> SelectorKey: ...
def modify(self, fileobj: FileDescriptorLike, events: int, data: Any = None) -> SelectorKey: ...
def get_map(self) -> Mapping[FileDescriptorLike, SelectorKey]: ...

class SelectSelector(_BaseSelectorImpl):
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, _EventMask]]: ...
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, int]]: ...

class _PollLikeSelector(_BaseSelectorImpl):
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, _EventMask]]: ...
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, int]]: ...

if sys.platform != "win32":
class PollSelector(_PollLikeSelector): ...
Expand All @@ -58,12 +56,12 @@ if sys.platform != "linux" and sys.platform != "darwin" and sys.platform != "win
if sys.platform != "win32" and sys.platform != "linux":
class KqueueSelector(_BaseSelectorImpl):
def fileno(self) -> int: ...
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, _EventMask]]: ...
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, int]]: ...

# Not a real class at runtime, it is just a conditional alias to other real selectors.
# The runtime logic is more fine-grained than a `sys.platform` check;
# not really expressible in the stubs
class DefaultSelector(_BaseSelectorImpl):
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, _EventMask]]: ...
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, int]]: ...
if sys.platform != "win32":
def fileno(self) -> int: ...
8 changes: 3 additions & 5 deletions stubs/gevent/gevent/selectors.pyi
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
from _typeshed import FileDescriptorLike
from collections.abc import Mapping
from selectors import BaseSelector, SelectorKey
from typing import Any, TypeAlias
from typing import Any

from gevent._util import Lazy
from gevent.hub import Hub

__all__ = ["DefaultSelector", "GeventSelector"]

_EventMask: TypeAlias = int

# technically this derives from _BaseSelectorImpl, which does not have type annotations
# but in terms of type checking the only difference is, that we need to add get_map since
# GeventSelector does not override it
class GeventSelector(BaseSelector):
def __init__(self, hub: Hub | None = None) -> None: ...
@Lazy
def hub(self) -> Hub: ...
def register(self, fileobj: FileDescriptorLike, events: _EventMask, data: Any = None) -> SelectorKey: ...
def register(self, fileobj: FileDescriptorLike, events: int, data: Any = None) -> SelectorKey: ...
def unregister(self, fileobj: FileDescriptorLike) -> SelectorKey: ...
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, _EventMask]]: ...
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, int]]: ...
def close(self) -> None: ...
def get_map(self) -> Mapping[FileDescriptorLike, SelectorKey]: ...

Expand Down
6 changes: 3 additions & 3 deletions stubs/simple-websocket/simple_websocket/ws.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import threading
from _typeshed import FileDescriptorLike
from _typeshed.wsgi import WSGIEnvironment
from collections.abc import Callable
from selectors import SelectorKey, _EventMask
from selectors import SelectorKey
from ssl import SSLContext
from typing import Any, Protocol, type_check_only

Expand All @@ -27,9 +27,9 @@ class _EventClassProtocol(Protocol):
@type_check_only
class _SelectorClassProtocol(Protocol):
# the signature of `register` here is the same as `selectors._BaseSelectorImpl` from the stdlib
def register(self, fileobj: FileDescriptorLike, events: _EventMask, data: Any = None) -> SelectorKey: ...
def register(self, fileobj: FileDescriptorLike, events: int, data: Any = None) -> SelectorKey: ...
# the signature of `select` here is the same as `selectors.DefaultSelector` from the stdlib
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, _EventMask]]: ...
def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, int]]: ...
def close(self) -> None: ...

class Base:
Expand Down
Loading