From 630b49a291ef28e9cc558a43534a314d581d22f8 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 1 Apr 2024 16:20:01 +0200 Subject: [PATCH] Replace various Incompletes in stdlib (#11673) Co-authored-by: Alex Waygood --- stdlib/importlib/resources/simple.pyi | 15 +++++++--- stdlib/io.pyi | 10 +++---- stdlib/multiprocessing/resource_tracker.pyi | 4 +-- stdlib/multiprocessing/util.pyi | 31 +++++++++++++++++---- stdlib/typing.pyi | 8 +++--- stdlib/typing_extensions.pyi | 2 +- stdlib/xml/dom/xmlbuilder.pyi | 2 +- 7 files changed, 49 insertions(+), 23 deletions(-) diff --git a/stdlib/importlib/resources/simple.pyi b/stdlib/importlib/resources/simple.pyi index c360da96d..c4c758111 100644 --- a/stdlib/importlib/resources/simple.pyi +++ b/stdlib/importlib/resources/simple.pyi @@ -1,6 +1,5 @@ import abc import sys -from _typeshed import Incomplete, OpenBinaryMode, OpenTextMode, Unused from collections.abc import Iterator from io import TextIOWrapper from typing import IO, Any, BinaryIO, Literal, NoReturn, overload @@ -28,11 +27,19 @@ if sys.version_info >= (3, 11): def is_file(self) -> Literal[True]: ... def is_dir(self) -> Literal[False]: ... @overload - def open(self, mode: OpenTextMode = "r", *args, **kwargs) -> TextIOWrapper: ... + def open( + self, + mode: Literal["r"] = "r", + encoding: str | None = None, + errors: str | None = None, + newline: str | None = None, + line_buffering: bool = False, + write_through: bool = False, + ) -> TextIOWrapper: ... @overload - def open(self, mode: OpenBinaryMode, *args: Unused, **kwargs: Unused) -> BinaryIO: ... + def open(self, mode: Literal["rb"]) -> BinaryIO: ... @overload - def open(self, mode: str, *args: Incomplete, **kwargs) -> IO[Any]: ... + def open(self, mode: str) -> IO[Any]: ... def joinpath(self, name: Never) -> NoReturn: ... # type: ignore[override] class ResourceContainer(Traversable, metaclass=abc.ABCMeta): diff --git a/stdlib/io.pyi b/stdlib/io.pyi index e7ed1b0b5..fdbbc8ddd 100644 --- a/stdlib/io.pyi +++ b/stdlib/io.pyi @@ -179,11 +179,11 @@ class TextIOWrapper(TextIOBase, TextIO): # type: ignore[misc] # incompatible d def __init__( self, buffer: _WrappedBuffer, - encoding: str | None = ..., - errors: str | None = ..., - newline: str | None = ..., - line_buffering: bool = ..., - write_through: bool = ..., + encoding: str | None = None, + errors: str | None = None, + newline: str | None = None, + line_buffering: bool = False, + write_through: bool = False, ) -> None: ... # Equals the "buffer" argument passed in to the constructor. @property diff --git a/stdlib/multiprocessing/resource_tracker.pyi b/stdlib/multiprocessing/resource_tracker.pyi index 78ad79cf9..61da7fdf1 100644 --- a/stdlib/multiprocessing/resource_tracker.pyi +++ b/stdlib/multiprocessing/resource_tracker.pyi @@ -6,8 +6,8 @@ __all__ = ["ensure_running", "register", "unregister"] class ResourceTracker: def getfd(self) -> int | None: ... def ensure_running(self) -> None: ... - def register(self, name: Sized, rtype) -> None: ... - def unregister(self, name: Sized, rtype) -> None: ... + def register(self, name: Sized, rtype: str) -> None: ... + def unregister(self, name: Sized, rtype: str) -> None: ... _resource_tracker: ResourceTracker ensure_running = _resource_tracker.ensure_running diff --git a/stdlib/multiprocessing/util.pyi b/stdlib/multiprocessing/util.pyi index 8b900996f..790d6c746 100644 --- a/stdlib/multiprocessing/util.pyi +++ b/stdlib/multiprocessing/util.pyi @@ -2,7 +2,7 @@ import threading from _typeshed import ConvertibleToInt, Incomplete, Unused from collections.abc import Callable, Iterable, Mapping, MutableMapping, Sequence from logging import Logger, _Level as _LoggingLevel -from typing import Any +from typing import Any, Generic, TypeVar, overload __all__ = [ "sub_debug", @@ -22,6 +22,9 @@ __all__ = [ "SUBWARNING", ] +_T = TypeVar("_T") +_R_co = TypeVar("_R_co", default=Any, covariant=True) + NOTSET: int SUBDEBUG: int DEBUG: int @@ -42,13 +45,29 @@ def is_abstract_socket_namespace(address: str | bytes | None) -> bool: ... abstract_sockets_supported: bool def get_temp_dir() -> str: ... -def register_after_fork(obj, func: Callable[[Incomplete], object]) -> None: ... +def register_after_fork(obj: _T, func: Callable[[_T], object]) -> None: ... -class Finalize: +class Finalize(Generic[_R_co]): + # "args" and "kwargs" are passed as arguments to "callback". + @overload def __init__( self, - obj: Incomplete | None, - callback: Callable[..., Incomplete], + obj: None, + callback: Callable[..., _R_co], + *, + args: Sequence[Any] = (), + kwargs: Mapping[str, Any] | None = None, + exitpriority: int, + ) -> None: ... + @overload + def __init__( + self, obj: None, callback: Callable[..., _R_co], args: Sequence[Any], kwargs: Mapping[str, Any] | None, exitpriority: int + ) -> None: ... + @overload + def __init__( + self, + obj: Any, + callback: Callable[..., _R_co], args: Sequence[Any] = (), kwargs: Mapping[str, Any] | None = None, exitpriority: int | None = None, @@ -59,7 +78,7 @@ class Finalize: _finalizer_registry: MutableMapping[Incomplete, Incomplete] = {}, sub_debug: Callable[..., object] = ..., getpid: Callable[[], int] = ..., - ): ... + ) -> _R_co: ... def cancel(self) -> None: ... def still_active(self) -> bool: ... diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index a2294f2f5..901e00efb 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -170,7 +170,7 @@ class TypeVar: def __or__(self, right: Any) -> _SpecialForm: ... def __ror__(self, left: Any) -> _SpecialForm: ... if sys.version_info >= (3, 11): - def __typing_subst__(self, arg): ... + def __typing_subst__(self, arg: Any) -> Any: ... # Used for an undocumented mypy feature. Does not exist at runtime. _promote = object() @@ -221,7 +221,7 @@ if sys.version_info >= (3, 11): def __init__(self, name: str) -> None: ... def __iter__(self) -> Any: ... def __typing_subst__(self, arg: Never) -> Never: ... - def __typing_prepare_subst__(self, alias, args): ... + def __typing_prepare_subst__(self, alias: Any, args: Any) -> tuple[Any, ...]: ... if sys.version_info >= (3, 10): @final @@ -270,8 +270,8 @@ if sys.version_info >= (3, 10): @property def kwargs(self) -> ParamSpecKwargs: ... if sys.version_info >= (3, 11): - def __typing_subst__(self, arg): ... - def __typing_prepare_subst__(self, alias, args): ... + def __typing_subst__(self, arg: Any) -> Any: ... + def __typing_prepare_subst__(self, alias: Any, args: Any) -> tuple[Any, ...]: ... def __or__(self, right: Any) -> _SpecialForm: ... def __ror__(self, left: Any) -> _SpecialForm: ... diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index cb67eb612..dc04f08c5 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -413,7 +413,7 @@ class TypeVar: def __or__(self, right: Any) -> _SpecialForm: ... def __ror__(self, left: Any) -> _SpecialForm: ... if sys.version_info >= (3, 11): - def __typing_subst__(self, arg): ... + def __typing_subst__(self, arg: Any) -> Any: ... @final class ParamSpec: diff --git a/stdlib/xml/dom/xmlbuilder.pyi b/stdlib/xml/dom/xmlbuilder.pyi index 62ca7dd9f..ab76d362e 100644 --- a/stdlib/xml/dom/xmlbuilder.pyi +++ b/stdlib/xml/dom/xmlbuilder.pyi @@ -60,7 +60,7 @@ class DOMBuilder: def supportsFeature(self, name: str) -> bool: ... def canSetFeature(self, name: str, state: int) -> bool: ... # getFeature could return any attribute from an instance of `Options` - def getFeature(self, name: str): ... + def getFeature(self, name: str) -> Any: ... def parseURI(self, uri: str) -> ExpatBuilder | ExpatBuilderNS: ... def parse(self, input: DOMInputSource) -> ExpatBuilder | ExpatBuilderNS: ... # `input` and `cnode` argtypes for `parseWithContext` are unknowable