From 14cfa62a3a4a92743640429cf71f3dc1314ba850 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 30 Mar 2024 14:05:57 +0100 Subject: [PATCH 01/12] Replace various Incompletes in stdlib --- stdlib/importlib/resources/simple.pyi | 30 +++++++++++++++++---- stdlib/multiprocessing/resource_tracker.pyi | 6 ++--- stdlib/multiprocessing/util.pyi | 28 +++++++++++++++---- stdlib/typing.pyi | 10 +++---- stdlib/typing_extensions.pyi | 4 +-- stdlib/xml/dom/xmlbuilder.pyi | 2 +- 6 files changed, 59 insertions(+), 21 deletions(-) diff --git a/stdlib/importlib/resources/simple.pyi b/stdlib/importlib/resources/simple.pyi index 9ff415156365..1f633eff5e13 100644 --- a/stdlib/importlib/resources/simple.pyi +++ b/stdlib/importlib/resources/simple.pyi @@ -1,8 +1,8 @@ import abc import sys -from _typeshed import Incomplete, OpenBinaryMode, OpenTextMode, Unused +from _typeshed import OpenBinaryMode, OpenTextMode from collections.abc import Iterator -from io import TextIOWrapper +from io import TextIOWrapper, _WrappedBuffer from typing import IO, Any, BinaryIO, Literal, NoReturn, overload from typing_extensions import Never @@ -28,11 +28,31 @@ 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: Incomplete, **kwargs: Incomplete) -> TextIOWrapper: ... + def open( + self, + mode: OpenTextMode = "r", + *, + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., + line_buffering: bool = ..., + write_through: bool = ..., + ) -> TextIOWrapper: ... @overload - def open(self, mode: OpenBinaryMode, *args: Unused, **kwargs: Unused) -> BinaryIO: ... + def open( + self, + mode: OpenTextMode, + buffer: _WrappedBuffer, + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., + line_buffering: bool = ..., + write_through: bool = ..., + ) -> TextIOWrapper: ... @overload - def open(self, mode: str, *args: Incomplete, **kwargs: Incomplete) -> IO[Any]: ... + def open(self, mode: OpenBinaryMode) -> BinaryIO: ... + @overload + def open(self, mode: str, *args: Any, **kwargs: Any) -> IO[Any]: ... def joinpath(self, name: Never) -> NoReturn: ... # type: ignore[override] class ResourceContainer(Traversable, metaclass=abc.ABCMeta): diff --git a/stdlib/multiprocessing/resource_tracker.pyi b/stdlib/multiprocessing/resource_tracker.pyi index 7f726a00d73a..61da7fdf1ceb 100644 --- a/stdlib/multiprocessing/resource_tracker.pyi +++ b/stdlib/multiprocessing/resource_tracker.pyi @@ -1,4 +1,4 @@ -from _typeshed import FileDescriptorOrPath, Incomplete +from _typeshed import FileDescriptorOrPath from collections.abc import Sized __all__ = ["ensure_running", "register", "unregister"] @@ -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: Incomplete) -> None: ... - def unregister(self, name: Sized, rtype: Incomplete) -> 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 aeb46f85a327..fb8e87f174e9 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, TypeVar, overload __all__ = [ "sub_debug", @@ -22,6 +22,9 @@ __all__ = [ "SUBWARNING", ] +_T = TypeVar("_T") +_R = TypeVar("_R", default=Any) + NOTSET: int SUBDEBUG: int DEBUG: int @@ -42,13 +45,28 @@ def is_abstract_socket_namespace(address: str | bytes | None) -> bool: ... abstract_sockets_supported: bool def get_temp_dir() -> str: ... -def register_after_fork(obj: Incomplete, func: Callable[[Incomplete], object]) -> None: ... +def register_after_fork(obj: _T, func: Callable[[_T], object]) -> None: ... class Finalize: + @overload + def __init__( + self, + obj: None, + callback: Callable[..., _R], + *, + args: Sequence[Any] = (), + kwargs: Mapping[str, Any] | None = None, + exitpriority: int, + ) -> None: ... + @overload + def __init__( + self, obj: None, callback: Callable[..., _R], args: Sequence[Any], kwargs: Mapping[str, Any] | None, exitpriority: int + ) -> None: ... + @overload def __init__( self, - obj: Incomplete | None, - callback: Callable[..., Incomplete], + obj: Any, + callback: Callable[..., _R], args: Sequence[Any] = (), kwargs: Mapping[str, Any] | None = None, exitpriority: int | None = None, @@ -59,7 +77,7 @@ class Finalize: _finalizer_registry: MutableMapping[Incomplete, Incomplete] = {}, sub_debug: Callable[..., object] = ..., getpid: Callable[[], int] = ..., - ) -> Incomplete: ... + ) -> _R: ... def cancel(self) -> None: ... def still_active(self) -> bool: ... diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index be0c29c89f8d..901e00efba00 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -6,7 +6,7 @@ import collections # noqa: F401 # pyright: ignore import sys import typing_extensions from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import IdentityFunction, Incomplete, ReadableBuffer, SupportsKeysAndGetItem +from _typeshed import IdentityFunction, ReadableBuffer, SupportsKeysAndGetItem from abc import ABCMeta, abstractmethod from contextlib import AbstractAsyncContextManager, AbstractContextManager from re import Match as Match, Pattern as Pattern @@ -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: Incomplete) -> Incomplete: ... + 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: Incomplete, args: Incomplete) -> Incomplete: ... + 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: Incomplete) -> Incomplete: ... - def __typing_prepare_subst__(self, alias: Incomplete, args: Incomplete) -> Incomplete: ... + 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 f9e94ca683d6..dc04f08c50d9 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -2,7 +2,7 @@ import abc import sys import typing from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import IdentityFunction, Incomplete +from _typeshed import IdentityFunction from typing import ( # noqa: Y022,Y037,Y038,Y039 IO as IO, TYPE_CHECKING as TYPE_CHECKING, @@ -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: Incomplete) -> Incomplete: ... + 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 480dd7ce732c..ab76d362e23f 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) -> Incomplete: ... + 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 From c921c5553c369c94383d116ff9393c13eb15df2e Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 30 Mar 2024 14:11:39 +0100 Subject: [PATCH 02/12] Add missing Generic marker --- stdlib/multiprocessing/util.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/multiprocessing/util.pyi b/stdlib/multiprocessing/util.pyi index fb8e87f174e9..0618393e5b0d 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, TypeVar, overload +from typing import Any, Generic, TypeVar, overload __all__ = [ "sub_debug", @@ -47,7 +47,7 @@ abstract_sockets_supported: bool def get_temp_dir() -> str: ... def register_after_fork(obj: _T, func: Callable[[_T], object]) -> None: ... -class Finalize: +class Finalize(Generic[_R]): @overload def __init__( self, From b5523503e2436b2ebcf474d8de373f17c9799439 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 30 Mar 2024 14:13:20 +0100 Subject: [PATCH 03/12] Add an explanatory comment --- stdlib/multiprocessing/util.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/multiprocessing/util.pyi b/stdlib/multiprocessing/util.pyi index 0618393e5b0d..0f1fec3b290d 100644 --- a/stdlib/multiprocessing/util.pyi +++ b/stdlib/multiprocessing/util.pyi @@ -48,6 +48,7 @@ def get_temp_dir() -> str: ... def register_after_fork(obj: _T, func: Callable[[_T], object]) -> None: ... class Finalize(Generic[_R]): + # "args" and "kwargs" are passed as arguments to "callback". @overload def __init__( self, From d51b3de5f9f9bb4c0945c4f54e8f67ad04cf6651 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 30 Mar 2024 14:17:05 +0100 Subject: [PATCH 04/12] ResourceHandle.open() had a bug upstream --- stdlib/importlib/resources/simple.pyi | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/stdlib/importlib/resources/simple.pyi b/stdlib/importlib/resources/simple.pyi index 1f633eff5e13..c12dfc729a02 100644 --- a/stdlib/importlib/resources/simple.pyi +++ b/stdlib/importlib/resources/simple.pyi @@ -28,21 +28,9 @@ 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", - *, - encoding: str | None = ..., - errors: str | None = ..., - newline: str | None = ..., - line_buffering: bool = ..., - write_through: bool = ..., - ) -> TextIOWrapper: ... - @overload def open( self, mode: OpenTextMode, - buffer: _WrappedBuffer, encoding: str | None = ..., errors: str | None = ..., newline: str | None = ..., From eefbc2dc7d598e1d931c0bf8d62cb36366c2d927 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 30 Mar 2024 13:18:38 +0000 Subject: [PATCH 05/12] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/importlib/resources/simple.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/importlib/resources/simple.pyi b/stdlib/importlib/resources/simple.pyi index c12dfc729a02..7537cdc0f94b 100644 --- a/stdlib/importlib/resources/simple.pyi +++ b/stdlib/importlib/resources/simple.pyi @@ -2,7 +2,7 @@ import abc import sys from _typeshed import OpenBinaryMode, OpenTextMode from collections.abc import Iterator -from io import TextIOWrapper, _WrappedBuffer +from io import TextIOWrapper from typing import IO, Any, BinaryIO, Literal, NoReturn, overload from typing_extensions import Never From 2c7684583b18028fe6a0719880c2da2f275190a1 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 30 Mar 2024 14:26:20 +0100 Subject: [PATCH 06/12] Add defaults to TextIOWrapper --- stdlib/io.pyi | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stdlib/io.pyi b/stdlib/io.pyi index e7ed1b0b5ee5..fdbbc8dddce9 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 From 3e0bb77506369a60be62ad8c025e0a92a0a27e7f Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 30 Mar 2024 14:26:45 +0100 Subject: [PATCH 07/12] Match ResourceHandle.open() to ABC --- stdlib/importlib/resources/simple.pyi | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/stdlib/importlib/resources/simple.pyi b/stdlib/importlib/resources/simple.pyi index 7537cdc0f94b..24252963ca6d 100644 --- a/stdlib/importlib/resources/simple.pyi +++ b/stdlib/importlib/resources/simple.pyi @@ -1,9 +1,8 @@ import abc import sys -from _typeshed import OpenBinaryMode, OpenTextMode from collections.abc import Iterator from io import TextIOWrapper -from typing import IO, Any, BinaryIO, Literal, NoReturn, overload +from typing import BinaryIO, Literal, NoReturn, overload from typing_extensions import Never if sys.version_info >= (3, 11): @@ -30,17 +29,15 @@ if sys.version_info >= (3, 11): @overload def open( self, - mode: OpenTextMode, - encoding: str | None = ..., - errors: str | None = ..., - newline: str | None = ..., - line_buffering: bool = ..., - write_through: bool = ..., + 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) -> BinaryIO: ... - @overload - def open(self, mode: str, *args: Any, **kwargs: Any) -> IO[Any]: ... + def open(self, mode: Literal["rb"]) -> BinaryIO: ... def joinpath(self, name: Never) -> NoReturn: ... # type: ignore[override] class ResourceContainer(Traversable, metaclass=abc.ABCMeta): From 48d2464fd4eac275767f2de15b10a050f629b5f6 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 1 Apr 2024 14:29:41 +0200 Subject: [PATCH 08/12] Update stdlib/importlib/resources/simple.pyi Co-authored-by: Alex Waygood --- stdlib/importlib/resources/simple.pyi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stdlib/importlib/resources/simple.pyi b/stdlib/importlib/resources/simple.pyi index 24252963ca6d..1eb1c5cb401f 100644 --- a/stdlib/importlib/resources/simple.pyi +++ b/stdlib/importlib/resources/simple.pyi @@ -38,6 +38,8 @@ if sys.version_info >= (3, 11): ) -> TextIOWrapper: ... @overload def open(self, mode: Literal["rb"]) -> BinaryIO: ... + @overload + def open(self, mode: str) -> IO[Any]: ... def joinpath(self, name: Never) -> NoReturn: ... # type: ignore[override] class ResourceContainer(Traversable, metaclass=abc.ABCMeta): From 35439f390b2a19784c097f9e4949e2261e6ac3db Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Apr 2024 12:33:14 +0000 Subject: [PATCH 09/12] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/importlib/resources/simple.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/importlib/resources/simple.pyi b/stdlib/importlib/resources/simple.pyi index 1eb1c5cb401f..2d4bd1ca057f 100644 --- a/stdlib/importlib/resources/simple.pyi +++ b/stdlib/importlib/resources/simple.pyi @@ -39,7 +39,7 @@ if sys.version_info >= (3, 11): @overload def open(self, mode: Literal["rb"]) -> BinaryIO: ... @overload - def open(self, mode: str) -> IO[Any]: ... + def open(self, mode: str) -> IO[Any]: ... def joinpath(self, name: Never) -> NoReturn: ... # type: ignore[override] class ResourceContainer(Traversable, metaclass=abc.ABCMeta): From d60e8c9f78a90f223f5cc6053ca7291195eda6ed Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 1 Apr 2024 13:43:22 +0100 Subject: [PATCH 10/12] Update simple.pyi --- stdlib/importlib/resources/simple.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/importlib/resources/simple.pyi b/stdlib/importlib/resources/simple.pyi index 2d4bd1ca057f..c4c758111c2d 100644 --- a/stdlib/importlib/resources/simple.pyi +++ b/stdlib/importlib/resources/simple.pyi @@ -2,7 +2,7 @@ import abc import sys from collections.abc import Iterator from io import TextIOWrapper -from typing import BinaryIO, Literal, NoReturn, overload +from typing import IO, Any, BinaryIO, Literal, NoReturn, overload from typing_extensions import Never if sys.version_info >= (3, 11): From a0580f3f508e8c727a10233f12276549653bde2d Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 1 Apr 2024 16:08:02 +0200 Subject: [PATCH 11/12] Make _R covariant Co-authored-by: Alex Waygood --- stdlib/multiprocessing/util.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/multiprocessing/util.pyi b/stdlib/multiprocessing/util.pyi index 0f1fec3b290d..796ccaff2858 100644 --- a/stdlib/multiprocessing/util.pyi +++ b/stdlib/multiprocessing/util.pyi @@ -23,7 +23,7 @@ __all__ = [ ] _T = TypeVar("_T") -_R = TypeVar("_R", default=Any) +_R = TypeVar("_R_co", default=Any, covariant=True) NOTSET: int SUBDEBUG: int From b77a5cbcd4c1718ad7514d56dd37fed9439bb409 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 1 Apr 2024 16:10:41 +0200 Subject: [PATCH 12/12] Fix typevar name --- stdlib/multiprocessing/util.pyi | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/multiprocessing/util.pyi b/stdlib/multiprocessing/util.pyi index 796ccaff2858..790d6c7467f0 100644 --- a/stdlib/multiprocessing/util.pyi +++ b/stdlib/multiprocessing/util.pyi @@ -23,7 +23,7 @@ __all__ = [ ] _T = TypeVar("_T") -_R = TypeVar("_R_co", default=Any, covariant=True) +_R_co = TypeVar("_R_co", default=Any, covariant=True) NOTSET: int SUBDEBUG: int @@ -47,13 +47,13 @@ abstract_sockets_supported: bool def get_temp_dir() -> str: ... def register_after_fork(obj: _T, func: Callable[[_T], object]) -> None: ... -class Finalize(Generic[_R]): +class Finalize(Generic[_R_co]): # "args" and "kwargs" are passed as arguments to "callback". @overload def __init__( self, obj: None, - callback: Callable[..., _R], + callback: Callable[..., _R_co], *, args: Sequence[Any] = (), kwargs: Mapping[str, Any] | None = None, @@ -61,13 +61,13 @@ class Finalize(Generic[_R]): ) -> None: ... @overload def __init__( - self, obj: None, callback: Callable[..., _R], args: Sequence[Any], kwargs: Mapping[str, Any] | None, exitpriority: int + 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], + callback: Callable[..., _R_co], args: Sequence[Any] = (), kwargs: Mapping[str, Any] | None = None, exitpriority: int | None = None, @@ -78,7 +78,7 @@ class Finalize(Generic[_R]): _finalizer_registry: MutableMapping[Incomplete, Incomplete] = {}, sub_debug: Callable[..., object] = ..., getpid: Callable[[], int] = ..., - ) -> _R: ... + ) -> _R_co: ... def cancel(self) -> None: ... def still_active(self) -> bool: ...