Skip to content

Commit

Permalink
stdlib: Audit Callable[<parameters>, None] annotations (#8187)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Jun 27, 2022
1 parent 0178a0e commit 8b3b6bf
Show file tree
Hide file tree
Showing 21 changed files with 59 additions and 66 deletions.
12 changes: 4 additions & 8 deletions stdlib/_dummy_threading.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import sys
from _typeshed import ProfileFunction, TraceFunction
from collections.abc import Callable, Iterable, Mapping
from types import FrameType, TracebackType
from types import TracebackType
from typing import Any, TypeVar
from typing_extensions import TypeAlias

# TODO recursive type
_TF: TypeAlias = Callable[[FrameType, str, Any], Callable[..., Any] | None]

_PF: TypeAlias = Callable[[FrameType, str, Any], None]
_T = TypeVar("_T")

__all__ = [
Expand Down Expand Up @@ -43,8 +39,8 @@ def currentThread() -> Thread: ...
def get_ident() -> int: ...
def enumerate() -> list[Thread]: ...
def main_thread() -> Thread: ...
def settrace(func: _TF) -> None: ...
def setprofile(func: _PF | None) -> None: ...
def settrace(func: TraceFunction) -> None: ...
def setprofile(func: ProfileFunction | None) -> None: ...
def stack_size(size: int = ...) -> int: ...

TIMEOUT_MAX: float
Expand Down
11 changes: 9 additions & 2 deletions stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import ctypes
import mmap
import pickle
import sys
from collections.abc import Awaitable, Container, Iterable, Set as AbstractSet
from collections.abc import Awaitable, Callable, Container, Iterable, Set as AbstractSet
from os import PathLike
from types import TracebackType
from types import FrameType, TracebackType
from typing import Any, AnyStr, Generic, Protocol, TypeVar, Union
from typing_extensions import Final, Literal, LiteralString, TypeAlias, final

Expand Down Expand Up @@ -262,3 +262,10 @@ class structseq(Generic[_T_co]):

# Superset of typing.AnyStr that also inclues LiteralString
AnyOrLiteralStr = TypeVar("AnyOrLiteralStr", str, bytes, LiteralString) # noqa: Y001

# Objects suitable to be passed to sys.setprofile, threading.setprofile, and similar
ProfileFunction: TypeAlias = Callable[[FrameType, str, Any], object]

# Objects suitable to be passed to sys.settrace, threading.settrace, and similar
# TODO: Ideally this would be a recursive type alias
TraceFunction: TypeAlias = Callable[[FrameType, str, Any], Callable[[FrameType, str, Any], Any] | None]
2 changes: 1 addition & 1 deletion stdlib/asyncio/windows_utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if sys.platform == "win32":
@property
def handle(self) -> int: ...
def fileno(self) -> int: ...
def close(self, *, CloseHandle: Callable[[int], None] = ...) -> None: ...
def close(self, *, CloseHandle: Callable[[int], object] = ...) -> None: ...

class Popen(subprocess.Popen[AnyStr]):
stdin: PipeHandle | None # type: ignore[assignment]
Expand Down
8 changes: 4 additions & 4 deletions stdlib/concurrent/futures/process.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ if sys.version_info >= (3, 11):
def _process_worker(
call_queue: Queue[_CallItem],
result_queue: SimpleQueue[_ResultItem],
initializer: Callable[..., None] | None,
initializer: Callable[..., object] | None,
initargs: tuple[Any, ...],
max_tasks: int | None = ...,
) -> None: ...
Expand All @@ -120,7 +120,7 @@ elif sys.version_info >= (3, 7):
def _process_worker(
call_queue: Queue[_CallItem],
result_queue: SimpleQueue[_ResultItem],
initializer: Callable[..., None] | None,
initializer: Callable[..., object] | None,
initargs: tuple[Any, ...],
) -> None: ...

Expand Down Expand Up @@ -184,7 +184,7 @@ class ProcessPoolExecutor(Executor):
self,
max_workers: int | None = ...,
mp_context: BaseContext | None = ...,
initializer: Callable[..., None] | None = ...,
initializer: Callable[..., object] | None = ...,
initargs: tuple[Any, ...] = ...,
*,
max_tasks_per_child: int | None = ...,
Expand All @@ -194,7 +194,7 @@ class ProcessPoolExecutor(Executor):
self,
max_workers: int | None = ...,
mp_context: BaseContext | None = ...,
initializer: Callable[..., None] | None = ...,
initializer: Callable[..., object] | None = ...,
initargs: tuple[Any, ...] = ...,
) -> None: ...
else:
Expand Down
4 changes: 2 additions & 2 deletions stdlib/concurrent/futures/thread.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if sys.version_info >= (3, 7):
def _worker(
executor_reference: ref[Any],
work_queue: queue.SimpleQueue[Any],
initializer: Callable[..., None],
initializer: Callable[..., object],
initargs: tuple[Any, ...],
) -> None: ...

Expand Down Expand Up @@ -63,7 +63,7 @@ class ThreadPoolExecutor(Executor):
self,
max_workers: int | None = ...,
thread_name_prefix: str = ...,
initializer: Callable[..., None] | None = ...,
initializer: Callable[..., object] | None = ...,
initargs: tuple[Any, ...] = ...,
) -> None: ...
else:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/distutils/ccompiler.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class CCompiler:
def library_filename(self, libname: str, lib_type: str = ..., strip_dir: int = ..., output_dir: str = ...) -> str: ...
def object_filenames(self, source_filenames: list[str], strip_dir: int = ..., output_dir: str = ...) -> list[str]: ...
def shared_object_filename(self, basename: str, strip_dir: int = ..., output_dir: str = ...) -> str: ...
def execute(self, func: Callable[..., None], args: tuple[Any, ...], msg: str | None = ..., level: int = ...) -> None: ...
def execute(self, func: Callable[..., object], args: tuple[Any, ...], msg: str | None = ..., level: int = ...) -> None: ...
def spawn(self, cmd: list[str]) -> None: ...
def mkpath(self, name: str, mode: int = ...) -> None: ...
def move_file(self, src: str, dst: str) -> str: ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/distutils/util.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def check_environ() -> None: ...
def subst_vars(s: str, local_vars: Mapping[str, str]) -> None: ...
def split_quoted(s: str) -> list[str]: ...
def execute(
func: Callable[..., None], args: tuple[Any, ...], msg: str | None = ..., verbose: bool = ..., dry_run: bool = ...
func: Callable[..., object], args: tuple[Any, ...], msg: str | None = ..., verbose: bool = ..., dry_run: bool = ...
) -> None: ...
def strtobool(val: str) -> Literal[0, 1]: ...
def byte_compile(
Expand Down
2 changes: 1 addition & 1 deletion stdlib/email/mime/application.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MIMEApplication(MIMENonMultipart):
self,
_data: str | bytes,
_subtype: str = ...,
_encoder: Callable[[MIMEApplication], None] = ...,
_encoder: Callable[[MIMEApplication], object] = ...,
*,
policy: Policy | None = ...,
**_params: _ParamsType,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/email/mime/audio.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MIMEAudio(MIMENonMultipart):
self,
_audiodata: str | bytes,
_subtype: str | None = ...,
_encoder: Callable[[MIMEAudio], None] = ...,
_encoder: Callable[[MIMEAudio], object] = ...,
*,
policy: Policy | None = ...,
**_params: _ParamsType,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/email/mime/image.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MIMEImage(MIMENonMultipart):
self,
_imagedata: str | bytes,
_subtype: str | None = ...,
_encoder: Callable[[MIMEImage], None] = ...,
_encoder: Callable[[MIMEImage], object] = ...,
*,
policy: Policy | None = ...,
**_params: _ParamsType,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/lib2to3/pgen2/tokenize.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ if sys.version_info >= (3, 7):
__all__ += ["COLONEQUAL"]

_Coord: TypeAlias = tuple[int, int]
_TokenEater: TypeAlias = Callable[[int, str, _Coord, _Coord, str], None]
_TokenEater: TypeAlias = Callable[[int, str, _Coord, _Coord, str], object]
_TokenInfo: TypeAlias = tuple[int, str, _Coord, _Coord, str]

class TokenError(Exception): ...
Expand Down
8 changes: 4 additions & 4 deletions stdlib/os/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,17 @@ class _Environ(MutableMapping[AnyStr, AnyStr], Generic[AnyStr]):
decodevalue: _EnvironCodeFunc[AnyStr],
) -> None: ...
else:
putenv: Callable[[AnyStr, AnyStr], None]
unsetenv: Callable[[AnyStr, AnyStr], None]
putenv: Callable[[AnyStr, AnyStr], object]
unsetenv: Callable[[AnyStr, AnyStr], object]
def __init__(
self,
data: MutableMapping[AnyStr, AnyStr],
encodekey: _EnvironCodeFunc[AnyStr],
decodekey: _EnvironCodeFunc[AnyStr],
encodevalue: _EnvironCodeFunc[AnyStr],
decodevalue: _EnvironCodeFunc[AnyStr],
putenv: Callable[[AnyStr, AnyStr], None],
unsetenv: Callable[[AnyStr, AnyStr], None],
putenv: Callable[[AnyStr, AnyStr], object],
unsetenv: Callable[[AnyStr, AnyStr], object],
) -> None: ...

def setdefault(self, key: AnyStr, value: AnyStr) -> AnyStr: ... # type: ignore[override]
Expand Down
2 changes: 1 addition & 1 deletion stdlib/pkgutil.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def iter_importers(fullname: str = ...) -> Iterator[MetaPathFinder | PathEntryFi
def iter_modules(path: Iterable[str] | None = ..., prefix: str = ...) -> Iterator[ModuleInfo]: ...
def read_code(stream: SupportsRead[bytes]) -> Any: ... # undocumented
def walk_packages(
path: Iterable[str] | None = ..., prefix: str = ..., onerror: Callable[[str], None] | None = ...
path: Iterable[str] | None = ..., prefix: str = ..., onerror: Callable[[str], object] | None = ...
) -> Iterator[ModuleInfo]: ...
def get_data(package: str, resource: str) -> bytes | None: ...

Expand Down
6 changes: 3 additions & 3 deletions stdlib/pydoc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ class ModuleScanner:
quit: bool
def run(
self,
callback: Callable[[str | None, str, str], None],
callback: Callable[[str | None, str, str], object],
key: str | None = ...,
completer: Callable[[], None] | None = ...,
onerror: Callable[[str], None] | None = ...,
completer: Callable[[], object] | None = ...,
onerror: Callable[[str], object] | None = ...,
) -> None: ...

def apropos(key: str) -> None: ...
Expand Down
6 changes: 3 additions & 3 deletions stdlib/shutil.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if sys.version_info >= (3, 8):
dst: StrPath,
symlinks: bool = ...,
ignore: None | Callable[[str, list[str]], Iterable[str]] | Callable[[StrPath, list[str]], Iterable[str]] = ...,
copy_function: Callable[[str, str], None] = ...,
copy_function: Callable[[str, str], object] = ...,
ignore_dangling_symlinks: bool = ...,
dirs_exist_ok: bool = ...,
) -> _PathReturn: ...
Expand All @@ -78,7 +78,7 @@ else:
dst: StrPath,
symlinks: bool = ...,
ignore: None | Callable[[str, list[str]], Iterable[str]] | Callable[[StrPath, list[str]], Iterable[str]] = ...,
copy_function: Callable[[str, str], None] = ...,
copy_function: Callable[[str, str], object] = ...,
ignore_dangling_symlinks: bool = ...,
) -> _PathReturn: ...

Expand All @@ -94,7 +94,7 @@ if sys.version_info >= (3, 11):
else:
def rmtree(path: StrOrBytesPath, ignore_errors: bool = ..., onerror: Callable[[Any, Any, Any], Any] | None = ...) -> None: ...

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

# N.B. shutil.move appears to take bytes arguments, however,
# this does not work when dst is (or is within) an existing directory.
Expand Down
16 changes: 5 additions & 11 deletions stdlib/sys.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import OptExcInfo, structseq
from _typeshed import OptExcInfo, ProfileFunction, TraceFunction, structseq
from builtins import object as _object
from collections.abc import AsyncGenerator, Callable, Coroutine, Sequence
from importlib.abc import PathEntryFinder
Expand Down Expand Up @@ -241,16 +241,10 @@ def getsizeof(obj: object) -> int: ...
@overload
def getsizeof(obj: object, default: int) -> int: ...
def getswitchinterval() -> float: ...

_ProfileFunc: TypeAlias = Callable[[FrameType, str, Any], Any]

def getprofile() -> _ProfileFunc | None: ...
def setprofile(profilefunc: _ProfileFunc | None) -> None: ...

_TraceFunc: TypeAlias = Callable[[FrameType, str, Any], Callable[[FrameType, str, Any], Any] | None]

def gettrace() -> _TraceFunc | None: ...
def settrace(tracefunc: _TraceFunc | None) -> None: ...
def getprofile() -> ProfileFunction | None: ...
def setprofile(profilefunc: ProfileFunction | None) -> None: ...
def gettrace() -> TraceFunction | None: ...
def settrace(tracefunc: TraceFunction | None) -> None: ...

if sys.platform == "win32":
# A tuple of length 5, even though it has more than 5 attributes.
Expand Down
18 changes: 7 additions & 11 deletions stdlib/threading.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import sys
from _typeshed import ProfileFunction, TraceFunction
from collections.abc import Callable, Iterable, Mapping
from types import FrameType, TracebackType
from types import TracebackType
from typing import Any, TypeVar
from typing_extensions import TypeAlias

# TODO recursive type
_TF: TypeAlias = Callable[[FrameType, str, Any], Callable[..., Any] | None]

_PF: TypeAlias = Callable[[FrameType, str, Any], None]
_T = TypeVar("_T")

__all__ = [
Expand Down Expand Up @@ -40,7 +36,7 @@ if sys.version_info >= (3, 8):
if sys.version_info >= (3, 10):
__all__ += ["getprofile", "gettrace"]

_profile_hook: _PF | None
_profile_hook: ProfileFunction | None

def active_count() -> int: ...
def activeCount() -> int: ... # deprecated alias for active_count()
Expand All @@ -53,12 +49,12 @@ def main_thread() -> Thread: ...
if sys.version_info >= (3, 8):
from _thread import get_native_id as get_native_id

def settrace(func: _TF) -> None: ...
def setprofile(func: _PF | None) -> None: ...
def settrace(func: TraceFunction) -> None: ...
def setprofile(func: ProfileFunction | None) -> None: ...

if sys.version_info >= (3, 10):
def gettrace() -> _TF | None: ...
def getprofile() -> _PF | None: ...
def gettrace() -> TraceFunction | None: ...
def getprofile() -> ProfileFunction | None: ...

def stack_size(size: int = ...) -> int: ...

Expand Down
4 changes: 2 additions & 2 deletions stdlib/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ if sys.version_info >= (3, 7):
name: str,
bases: Iterable[object] = ...,
kwds: dict[str, Any] | None = ...,
exec_body: Callable[[dict[str, Any]], None] | None = ...,
exec_body: Callable[[dict[str, Any]], object] | None = ...,
) -> type: ...
def resolve_bases(bases: Iterable[object]) -> tuple[Any, ...]: ...

Expand All @@ -592,7 +592,7 @@ else:
name: str,
bases: tuple[type, ...] = ...,
kwds: dict[str, Any] | None = ...,
exec_body: Callable[[dict[str, Any]], None] | None = ...,
exec_body: Callable[[dict[str, Any]], object] | None = ...,
) -> type: ...

def prepare_class(
Expand Down
6 changes: 3 additions & 3 deletions stdlib/unittest/case.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ class TestCase:
class FunctionTestCase(TestCase):
def __init__(
self,
testFunc: Callable[[], None],
setUp: Callable[[], None] | None = ...,
tearDown: Callable[[], None] | None = ...,
testFunc: Callable[[], object],
setUp: Callable[[], object] | None = ...,
tearDown: Callable[[], object] | None = ...,
description: str | None = ...,
) -> None: ...
def runTest(self) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/urllib/request.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class HTTPErrorProcessor(BaseHandler):
def urlretrieve(
url: str,
filename: StrOrBytesPath | None = ...,
reporthook: Callable[[int, int, int], None] | None = ...,
reporthook: Callable[[int, int, int], object] | None = ...,
data: _DataType = ...,
) -> tuple[str, HTTPMessage]: ...
def urlcleanup() -> None: ...
Expand All @@ -299,7 +299,7 @@ class URLopener:
self,
url: str,
filename: str | None = ...,
reporthook: Callable[[int, int, int], None] | None = ...,
reporthook: Callable[[int, int, int], object] | None = ...,
data: bytes | None = ...,
) -> tuple[str, Message | None]: ...
def addheader(self, *args: tuple[str, str]) -> None: ... # undocumented
Expand Down
6 changes: 3 additions & 3 deletions stdlib/xdrlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Packer:
def pack_string(self, s: bytes) -> None: ...
def pack_opaque(self, s: bytes) -> None: ...
def pack_bytes(self, s: bytes) -> None: ...
def pack_list(self, list: Sequence[_T], pack_item: Callable[[_T], None]) -> None: ...
def pack_farray(self, n: int, list: Sequence[_T], pack_item: Callable[[_T], None]) -> None: ...
def pack_array(self, list: Sequence[_T], pack_item: Callable[[_T], None]) -> None: ...
def pack_list(self, list: Sequence[_T], pack_item: Callable[[_T], object]) -> None: ...
def pack_farray(self, n: int, list: Sequence[_T], pack_item: Callable[[_T], object]) -> None: ...
def pack_array(self, list: Sequence[_T], pack_item: Callable[[_T], object]) -> None: ...

class Unpacker:
def __init__(self, data: bytes) -> None: ...
Expand Down

0 comments on commit 8b3b6bf

Please sign in to comment.