Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdlib: audit more callback annotations #8209

Merged
merged 7 commits into from
Jul 19, 2022
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
2 changes: 1 addition & 1 deletion stdlib/_dummy_thread.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if sys.version_info >= (3, 7):
TIMEOUT_MAX: int
error = RuntimeError

def start_new_thread(function: Callable[..., Any], args: tuple[Any, ...], kwargs: dict[str, Any] = ...) -> None: ...
def start_new_thread(function: Callable[..., object], args: tuple[Any, ...], kwargs: dict[str, Any] = ...) -> None: ...
def exit() -> NoReturn: ...
def get_ident() -> int: ...
def allocate_lock() -> LockType: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/_dummy_threading.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Thread:
def __init__(
self,
group: None = ...,
target: Callable[..., Any] | None = ...,
target: Callable[..., object] | None = ...,
name: str | None = ...,
args: Iterable[Any] = ...,
kwargs: Mapping[str, Any] | None = ...,
Expand Down Expand Up @@ -151,7 +151,7 @@ class Timer(Thread):
def __init__(
self,
interval: float,
function: Callable[..., Any],
function: Callable[..., object],
args: Iterable[Any] | None = ...,
kwargs: Mapping[str, Any] | None = ...,
) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/_thread.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LockType:
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> None: ...

def start_new_thread(function: Callable[..., Any], args: tuple[Any, ...], kwargs: dict[str, Any] = ...) -> int: ...
def start_new_thread(function: Callable[..., object], args: tuple[Any, ...], kwargs: dict[str, Any] = ...) -> int: ...
def interrupt_main() -> None: ...
def exit() -> NoReturn: ...
def allocate_lock() -> LockType: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/atexit.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable
from typing import Any, TypeVar
from typing import TypeVar
from typing_extensions import ParamSpec

_T = TypeVar("_T")
Expand All @@ -9,4 +9,4 @@ def _clear() -> None: ...
def _ncallbacks() -> int: ...
def _run_exitfuncs() -> None: ...
def register(func: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs) -> Callable[_P, _T]: ...
def unregister(func: Callable[..., Any]) -> None: ...
def unregister(func: Callable[..., object]) -> None: ...
15 changes: 7 additions & 8 deletions stdlib/bdb.pyi
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import sys
from _typeshed import ExcInfo
from _typeshed import ExcInfo, TraceFunction
from collections.abc import Callable, Iterable, Mapping
from types import CodeType, FrameType, TracebackType
from typing import IO, Any, SupportsInt, TypeVar
from typing_extensions import Literal, ParamSpec, TypeAlias
from typing_extensions import Literal, ParamSpec

__all__ = ["BdbQuit", "Bdb", "Breakpoint"]

_T = TypeVar("_T")
_P = ParamSpec("_P")
_TraceDispatch: TypeAlias = Callable[[FrameType, str, Any], Any] # TODO: Recursive type

GENERATOR_AND_COROUTINE_FLAGS: Literal[672]

Expand All @@ -28,11 +27,11 @@ class Bdb:
def __init__(self, skip: Iterable[str] | None = ...) -> None: ...
def canonic(self, filename: str) -> str: ...
def reset(self) -> None: ...
def trace_dispatch(self, frame: FrameType, event: str, arg: Any) -> _TraceDispatch: ...
def dispatch_line(self, frame: FrameType) -> _TraceDispatch: ...
def dispatch_call(self, frame: FrameType, arg: None) -> _TraceDispatch: ...
def dispatch_return(self, frame: FrameType, arg: Any) -> _TraceDispatch: ...
def dispatch_exception(self, frame: FrameType, arg: ExcInfo) -> _TraceDispatch: ...
def trace_dispatch(self, frame: FrameType, event: str, arg: Any) -> TraceFunction: ...
def dispatch_line(self, frame: FrameType) -> TraceFunction: ...
def dispatch_call(self, frame: FrameType, arg: None) -> TraceFunction: ...
def dispatch_return(self, frame: FrameType, arg: Any) -> TraceFunction: ...
def dispatch_exception(self, frame: FrameType, arg: ExcInfo) -> TraceFunction: ...
def is_skipped_module(self, module_name: str) -> bool: ...
def stop_here(self, frame: FrameType) -> bool: ...
def break_here(self, frame: FrameType) -> bool: ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/concurrent/futures/_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Future(Generic[_T]):
def cancelled(self) -> bool: ...
def running(self) -> bool: ...
def done(self) -> bool: ...
def add_done_callback(self, fn: Callable[[Future[_T]], Any]) -> None: ...
def add_done_callback(self, fn: Callable[[Future[_T]], object]) -> None: ...
def result(self, timeout: float | None = ...) -> _T: ...
def set_running_or_notify_cancel(self) -> bool: ...
def set_result(self, result: _T) -> None: ...
Expand Down
14 changes: 7 additions & 7 deletions stdlib/concurrent/futures/process.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ from weakref import ref

from ._base import Executor, Future

_T = TypeVar("_T")

_threads_wakeups: MutableMapping[Any, Any]
_global_shutdown: bool

Expand Down Expand Up @@ -40,14 +42,12 @@ class _ExceptionWithTraceback:

def _rebuild_exc(exc: Exception, tb: str) -> Exception: ...

_S = TypeVar("_S")

class _WorkItem(Generic[_S]):
future: Future[_S]
fn: Callable[..., _S]
class _WorkItem(Generic[_T]):
future: Future[_T]
fn: Callable[..., _T]
args: Iterable[Any]
kwargs: Mapping[str, Any]
def __init__(self, future: Future[_S], fn: Callable[..., _S], args: Iterable[Any], kwargs: Mapping[str, Any]) -> None: ...
def __init__(self, future: Future[_T], fn: Callable[..., _T], args: Iterable[Any], kwargs: Mapping[str, Any]) -> None: ...

class _ResultItem:
work_id: int
Expand Down Expand Up @@ -91,7 +91,7 @@ if sys.version_info >= (3, 7):
def _on_queue_feeder_error(self, e: Exception, obj: _CallItem) -> None: ...

def _get_chunks(*iterables: Any, chunksize: int) -> Generator[tuple[Any, ...], None, None]: ...
def _process_chunk(fn: Callable[..., Any], chunk: tuple[Any, None, None]) -> Generator[Any, None, None]: ...
def _process_chunk(fn: Callable[..., _T], chunk: Iterable[tuple[Any, ...]]) -> list[_T]: ...

if sys.version_info >= (3, 11):
def _sendback_result(
Expand Down
4 changes: 2 additions & 2 deletions stdlib/distutils/cmd.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Command:
def run_command(self, command: str) -> None: ...
def get_sub_commands(self) -> list[str]: ...
def warn(self, msg: str) -> None: ...
def execute(self, func: Callable[..., Any], args: Iterable[Any], msg: str | None = ..., level: int = ...) -> None: ...
def execute(self, func: Callable[..., object], args: Iterable[Any], msg: str | None = ..., level: int = ...) -> None: ...
def mkpath(self, name: str, mode: int = ...) -> None: ...
def copy_file(
self,
Expand Down Expand Up @@ -60,7 +60,7 @@ class Command:
self,
infiles: str | list[str] | tuple[str, ...],
outfile: str,
func: Callable[..., Any],
func: Callable[..., object],
args: list[Any],
exec_msg: str | None = ...,
skip_msg: str | None = ...,
Expand Down
6 changes: 3 additions & 3 deletions stdlib/doctest.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class DocTestFinder:
extraglobs: dict[str, Any] | None = ...,
) -> list[DocTest]: ...

_Out: TypeAlias = Callable[[str], Any]
_Out: TypeAlias = Callable[[str], object]

class DocTestRunner:
DIVIDER: str
Expand Down Expand Up @@ -201,8 +201,8 @@ class DocTestCase(unittest.TestCase):
self,
test: DocTest,
optionflags: int = ...,
setUp: Callable[[DocTest], Any] | None = ...,
tearDown: Callable[[DocTest], Any] | None = ...,
setUp: Callable[[DocTest], object] | None = ...,
tearDown: Callable[[DocTest], object] | None = ...,
checker: OutputChecker | None = ...,
) -> None: ...
def setUp(self) -> None: ...
Expand Down
10 changes: 5 additions & 5 deletions stdlib/ftplib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ class FTP:
def ntransfercmd(self, cmd: str, rest: int | str | None = ...) -> tuple[socket, int]: ...
def transfercmd(self, cmd: str, rest: int | str | None = ...) -> socket: ...
def retrbinary(
self, cmd: str, callback: Callable[[bytes], Any], blocksize: int = ..., rest: int | str | None = ...
self, cmd: str, callback: Callable[[bytes], object], blocksize: int = ..., rest: int | str | None = ...
) -> str: ...
def storbinary(
self,
cmd: str,
fp: SupportsRead[bytes],
blocksize: int = ...,
callback: Callable[[bytes], Any] | None = ...,
callback: Callable[[bytes], object] | None = ...,
rest: int | str | None = ...,
) -> str: ...
def retrlines(self, cmd: str, callback: Callable[[str], Any] | None = ...) -> str: ...
def storlines(self, cmd: str, fp: SupportsReadline[bytes], callback: Callable[[bytes], Any] | None = ...) -> str: ...
def retrlines(self, cmd: str, callback: Callable[[str], object] | None = ...) -> str: ...
def storlines(self, cmd: str, fp: SupportsReadline[bytes], callback: Callable[[bytes], object] | None = ...) -> str: ...
def acct(self, password: str) -> str: ...
def nlst(self, *args: str) -> list[str]: ...
# Technically only the last arg can be a Callable but ...
def dir(self, *args: str | Callable[[str], None]) -> None: ...
def dir(self, *args: str | Callable[[str], object]) -> None: ...
def mlsd(self, path: str = ..., facts: Iterable[str] = ...) -> Iterator[tuple[str, dict[str, str]]]: ...
def rename(self, fromname: str, toname: str) -> str: ...
def delete(self, filename: str) -> str: ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/functools.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if sys.version_info >= (3, 8):
if sys.version_info >= (3, 9):
__all__ += ["cache"]

_AnyCallable: TypeAlias = Callable[..., Any]
_AnyCallable: TypeAlias = Callable[..., object]

_T = TypeVar("_T")
_S = TypeVar("_S")
Expand Down
4 changes: 3 additions & 1 deletion stdlib/heapq.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ _S = TypeVar("_S")

__about__: str

def merge(*iterables: Iterable[_S], key: Callable[[_S], Any] | None = ..., reverse: bool = ...) -> Iterable[_S]: ...
def merge(
*iterables: Iterable[_S], key: Callable[[_S], SupportsRichComparison] | None = ..., reverse: bool = ...
) -> Iterable[_S]: ...
def nlargest(n: int, iterable: Iterable[_S], key: Callable[[_S], SupportsRichComparison] | None = ...) -> list[_S]: ...
def nsmallest(n: int, iterable: Iterable[_S], key: Callable[[_S], SupportsRichComparison] | None = ...) -> list[_S]: ...
def _heapify_max(__x: list[Any]) -> None: ... # undocumented
2 changes: 2 additions & 0 deletions stdlib/lib2to3/pytree.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ _NL: TypeAlias = Node | Leaf
_Context: TypeAlias = tuple[str, int, int]
_Results: TypeAlias = dict[str, _NL]
_RawNode: TypeAlias = tuple[int, str, _Context, list[_NL] | None]
# This alias isn't used in this file,
# but is imported in other lib2to3 submodules
_Convert: TypeAlias = Callable[[Grammar, _RawNode], Any]

HUGE: int
Expand Down
4 changes: 2 additions & 2 deletions stdlib/os/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ def utime(
follow_symlinks: bool = ...,
) -> None: ...

_OnError: TypeAlias = Callable[[OSError], Any]
_OnError: TypeAlias = Callable[[OSError], object]

def walk(
top: GenericPath[AnyStr], topdown: bool = ..., onerror: _OnError | None = ..., followlinks: bool = ...
Expand Down Expand Up @@ -1011,7 +1011,7 @@ if sys.version_info >= (3, 8):
if sys.platform == "win32":
class _AddedDllDirectory:
path: str | None
def __init__(self, path: str | None, cookie: _T, remove_dll_directory: Callable[[_T], Any]) -> None: ...
def __init__(self, path: str | None, cookie: _T, remove_dll_directory: Callable[[_T], object]) -> None: ...
def close(self) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: object) -> None: ...
Expand Down
21 changes: 7 additions & 14 deletions stdlib/sched.pyi
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import sys
from collections.abc import Callable
from typing import Any, NamedTuple
from typing_extensions import TypeAlias

__all__ = ["scheduler"]

_ActionCallback: TypeAlias = Callable[..., Any]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ActionCallback: TypeAlias = Callable[..., Any]
_ActionCallback: TypeAlias = Callable[..., object]

Looks like the return value is ignored

Copy link
Member Author

@AlexWaygood AlexWaygood Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used as an attribute annotation as well, though, and I wanted to avoid unexpected false positives on code like

e: Event
x = e.action()
x.foo()

I'm not sure if it makes sense to do that, but I didn't want to accidentally cause a regression. Same for _WriterCallback in wsgiref.validate.


if sys.version_info >= (3, 10):
class Event(NamedTuple):
time: float
priority: Any
sequence: int
action: Callable[..., Any]
action: _ActionCallback
argument: tuple[Any, ...]
kwargs: dict[str, Any]

else:
class Event(NamedTuple):
time: float
priority: Any
action: Callable[..., Any]
action: _ActionCallback
argument: tuple[Any, ...]
kwargs: dict[str, Any]

Expand All @@ -27,20 +30,10 @@ class scheduler:

def __init__(self, timefunc: Callable[[], float] = ..., delayfunc: Callable[[float], object] = ...) -> None: ...
def enterabs(
self,
time: float,
priority: Any,
action: Callable[..., Any],
argument: tuple[Any, ...] = ...,
kwargs: dict[str, Any] = ...,
self, time: float, priority: Any, action: _ActionCallback, argument: tuple[Any, ...] = ..., kwargs: dict[str, Any] = ...
) -> Event: ...
def enter(
self,
delay: float,
priority: Any,
action: Callable[..., Any],
argument: tuple[Any, ...] = ...,
kwargs: dict[str, Any] = ...,
self, delay: float, priority: Any, action: _ActionCallback, argument: tuple[Any, ...] = ..., kwargs: dict[str, Any] = ...
) -> Event: ...
def run(self, blocking: bool = ...) -> float | None: ...
def cancel(self, event: Event) -> None: ...
Expand Down
10 changes: 4 additions & 6 deletions stdlib/shutil.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,15 @@ else:
ignore_dangling_symlinks: bool = ...,
) -> _PathReturn: ...

_OnErrorCallback: TypeAlias = Callable[[Callable[..., Any], Any, Any], object]

if sys.version_info >= (3, 11):
def rmtree(
path: StrOrBytesPath,
ignore_errors: bool = ...,
onerror: Callable[[Any, Any, Any], Any] | None = ...,
*,
dir_fd: int | None = ...,
path: StrOrBytesPath, ignore_errors: bool = ..., onerror: _OnErrorCallback | None = ..., *, dir_fd: int | None = ...
) -> None: ...

else:
def rmtree(path: StrOrBytesPath, ignore_errors: bool = ..., onerror: Callable[[Any, Any, Any], Any] | None = ...) -> None: ...
def rmtree(path: StrOrBytesPath, ignore_errors: bool = ..., onerror: _OnErrorCallback | None = ...) -> None: ...

_CopyFn: TypeAlias = Callable[[str, str], object] | Callable[[StrPath, StrPath], object]

Expand Down
2 changes: 1 addition & 1 deletion stdlib/telnetlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Telnet:
def read_lazy(self) -> bytes: ...
def read_very_lazy(self) -> bytes: ...
def read_sb_data(self) -> bytes: ...
def set_option_negotiation_callback(self, callback: Callable[[socket.socket, bytes, bytes], Any] | None) -> None: ...
def set_option_negotiation_callback(self, callback: Callable[[socket.socket, bytes, bytes], object] | None) -> None: ...
def process_rawq(self) -> None: ...
def rawq_getchar(self) -> bytes: ...
def fill_rawq(self) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/threading.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Thread:
def __init__(
self,
group: None = ...,
target: Callable[..., Any] | None = ...,
target: Callable[..., object] | None = ...,
name: str | None = ...,
args: Iterable[Any] = ...,
kwargs: Mapping[str, Any] | None = ...,
Expand Down Expand Up @@ -171,7 +171,7 @@ class Timer(Thread):
def __init__(
self,
interval: float,
function: Callable[..., Any],
function: Callable[..., object],
args: Iterable[Any] | None = ...,
kwargs: Mapping[str, Any] | None = ...,
) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/timeit.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from typing_extensions import TypeAlias
__all__ = ["Timer", "timeit", "repeat", "default_timer"]

_Timer: TypeAlias = Callable[[], float]
_Stmt: TypeAlias = str | Callable[[], Any]
_Stmt: TypeAlias = str | Callable[[], object]

default_timer: _Timer

Expand All @@ -16,7 +16,7 @@ class Timer:
def print_exc(self, file: IO[str] | None = ...) -> None: ...
def timeit(self, number: int = ...) -> float: ...
def repeat(self, repeat: int = ..., number: int = ...) -> list[float]: ...
def autorange(self, callback: Callable[[int, float], Any] | None = ...) -> tuple[int, float]: ...
def autorange(self, callback: Callable[[int, float], object] | None = ...) -> tuple[int, float]: ...

def timeit(
stmt: _Stmt = ..., setup: _Stmt = ..., timer: _Timer = ..., number: int = ..., globals: dict[str, Any] | None = ...
Expand Down
7 changes: 5 additions & 2 deletions stdlib/wsgiref/validate.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from _typeshed.wsgi import ErrorStream, InputStream, WSGIApplication
from collections.abc import Callable, Iterable, Iterator
from typing import Any, NoReturn
from typing_extensions import TypeAlias

__all__ = ["validator"]

Expand All @@ -25,9 +26,11 @@ class ErrorWrapper:
def writelines(self, seq: Iterable[str]) -> None: ...
def close(self) -> NoReturn: ...

_WriterCallback: TypeAlias = Callable[[bytes], Any]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_WriterCallback: TypeAlias = Callable[[bytes], Any]
_WriterCallback: TypeAlias = Callable[[bytes], object]

Return value is ignored


class WriteWrapper:
writer: Callable[[bytes], Any]
def __init__(self, wsgi_writer: Callable[[bytes], Any]) -> None: ...
writer: _WriterCallback
def __init__(self, wsgi_writer: _WriterCallback) -> None: ...
def __call__(self, s: bytes) -> None: ...

class PartialIteratorWrapper:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/xml/etree/ElementTree.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ if sys.version_info >= (3, 8):
class C14NWriterTarget:
def __init__(
self,
write: Callable[[str], Any],
write: Callable[[str], object],
*,
with_comments: bool = ...,
strip_text: bool = ...,
Expand Down