From 5a8b9dafb37e4b351838914060dea39149a2f3bc Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 16 Feb 2022 19:47:49 +0000 Subject: [PATCH] Fix various py310 stubtest errors (#7239) --- stdlib/VERSIONS | 1 + stdlib/asyncio/proactor_events.pyi | 30 ++++++++++++++++++++--------- stdlib/py_compile.pyi | 6 +++++- stdlib/sched.pyi | 23 ++++++++++++++++------ stdlib/signal.pyi | 2 +- stdlib/unicodedata.pyi | 4 +++- stdlib/unittest/mock.pyi | 19 +++++++++++++++--- tests/stubtest_allowlists/py310.txt | 15 +++++---------- 8 files changed, 69 insertions(+), 31 deletions(-) diff --git a/stdlib/VERSIONS b/stdlib/VERSIONS index 2e2f593b9..eceb75385 100644 --- a/stdlib/VERSIONS +++ b/stdlib/VERSIONS @@ -112,6 +112,7 @@ difflib: 2.7- dis: 2.7- distutils: 2.7- distutils.command.bdist_msi: 2.7-3.10 +distutils.command.bdist_wininst: 2.7-3.9 doctest: 2.7- dummy_threading: 2.7-3.8 email: 2.7- diff --git a/stdlib/asyncio/proactor_events.pyi b/stdlib/asyncio/proactor_events.pyi index eba158267..3171cf604 100644 --- a/stdlib/asyncio/proactor_events.pyi +++ b/stdlib/asyncio/proactor_events.pyi @@ -29,15 +29,27 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin, transports.BaseTr def get_write_buffer_size(self) -> int: ... class _ProactorReadPipeTransport(_ProactorBasePipeTransport, transports.ReadTransport): - def __init__( - self, - loop: events.AbstractEventLoop, - sock: socket, - protocol: streams.StreamReaderProtocol, - waiter: futures.Future[Any] | None = ..., - extra: Mapping[Any, Any] | None = ..., - server: events.AbstractServer | None = ..., - ) -> None: ... + if sys.version_info >= (3, 10): + def __init__( + self, + loop: events.AbstractEventLoop, + sock: socket, + protocol: streams.StreamReaderProtocol, + waiter: futures.Future[Any] | None = ..., + extra: Mapping[Any, Any] | None = ..., + server: events.AbstractServer | None = ..., + buffer_size: int = ..., + ) -> None: ... + else: + def __init__( + self, + loop: events.AbstractEventLoop, + sock: socket, + protocol: streams.StreamReaderProtocol, + waiter: futures.Future[Any] | None = ..., + extra: Mapping[Any, Any] | None = ..., + server: events.AbstractServer | None = ..., + ) -> None: ... class _ProactorBaseWritePipeTransport(_ProactorBasePipeTransport, transports.WriteTransport): def __init__( diff --git a/stdlib/py_compile.pyi b/stdlib/py_compile.pyi index d9dca05c7..2e8285400 100644 --- a/stdlib/py_compile.pyi +++ b/stdlib/py_compile.pyi @@ -43,4 +43,8 @@ else: file: AnyStr, cfile: AnyStr | None = ..., dfile: AnyStr | None = ..., doraise: bool = ..., optimize: int = ... ) -> AnyStr | None: ... -def main(args: list[str] | None = ...) -> int: ... +if sys.version_info >= (3, 10): + def main() -> None: ... + +else: + def main(args: list[str] | None = ...) -> int: ... diff --git a/stdlib/sched.pyi b/stdlib/sched.pyi index ac58e525a..dff781b0c 100644 --- a/stdlib/sched.pyi +++ b/stdlib/sched.pyi @@ -1,13 +1,24 @@ +import sys from typing import Any, Callable, NamedTuple __all__ = ["scheduler"] -class Event(NamedTuple): - time: float - priority: Any - action: Callable[..., Any] - argument: tuple[Any, ...] - kwargs: dict[str, Any] +if sys.version_info >= (3, 10): + class Event(NamedTuple): + time: float + priority: Any + sequence: int + action: Callable[..., Any] + argument: tuple[Any, ...] + kwargs: dict[str, Any] + +else: + class Event(NamedTuple): + time: float + priority: Any + action: Callable[..., Any] + argument: tuple[Any, ...] + kwargs: dict[str, Any] class scheduler: timefunc: Callable[[], float] diff --git a/stdlib/signal.pyi b/stdlib/signal.pyi index f1495c92e..a6bc2daad 100644 --- a/stdlib/signal.pyi +++ b/stdlib/signal.pyi @@ -63,7 +63,7 @@ SIG_IGN: Handlers _SIGNUM = Union[int, Signals] _HANDLER = Union[Callable[[int, Optional[FrameType]], Any], int, Handlers, None] -def default_int_handler(signum: int, frame: FrameType | None) -> None: ... +def default_int_handler(__signalnum: int, __frame: FrameType | None) -> None: ... if sys.version_info >= (3, 10): # arguments changed in 3.10.2 def getsignal(signalnum: _SIGNUM) -> _HANDLER: ... diff --git a/stdlib/unicodedata.pyi b/stdlib/unicodedata.pyi index dca88caa7..aec8867df 100644 --- a/stdlib/unicodedata.pyi +++ b/stdlib/unicodedata.pyi @@ -3,9 +3,11 @@ from typing import Any, TypeVar from typing_extensions import final ucd_3_2_0: UCD -ucnhash_CAPI: Any unidata_version: str +if sys.version_info < (3, 10): + ucnhash_CAPI: Any + _T = TypeVar("_T") def bidirectional(__chr: str) -> str: ... diff --git a/stdlib/unittest/mock.pyi b/stdlib/unittest/mock.pyi index 664da1a34..2a2631a70 100644 --- a/stdlib/unittest/mock.pyi +++ b/stdlib/unittest/mock.pyi @@ -442,9 +442,22 @@ class _ANY: ANY: Any -def create_autospec( - spec: Any, spec_set: Any = ..., instance: Any = ..., _parent: Any | None = ..., _name: Any | None = ..., **kwargs: Any -) -> Any: ... +if sys.version_info >= (3, 10): + def create_autospec( + spec: Any, + spec_set: Any = ..., + instance: Any = ..., + _parent: Any | None = ..., + _name: Any | None = ..., + *, + unsafe: bool = ..., + **kwargs: Any, + ) -> Any: ... + +else: + def create_autospec( + spec: Any, spec_set: Any = ..., instance: Any = ..., _parent: Any | None = ..., _name: Any | None = ..., **kwargs: Any + ) -> Any: ... class _SpecState: spec: Any diff --git a/tests/stubtest_allowlists/py310.txt b/tests/stubtest_allowlists/py310.txt index 6f32aa60c..826119db2 100644 --- a/tests/stubtest_allowlists/py310.txt +++ b/tests/stubtest_allowlists/py310.txt @@ -129,17 +129,13 @@ os.path.join pstats.SortKey.__new__ tkinter.EventType.__new__ -# Rest of these errors are new in Python 3.10: -asyncio.proactor_events._ProactorReadPipeTransport.__init__ -distutils.command.bdist_wininst -py_compile.main -sched.Event.__new__ -sched.Event._fields -signal.default_int_handler +distutils.command.bdist_wininst # removed in 3.10 + +# stubtest complains that in 3.10 the default argument is inconsistent with the annotation, +# but in 3.10+ calling the function without the default argument is in fact deprecated, +# so it's better to ignore stubtest ssl.SSLContext.__new__ ssl._create_unverified_context -unicodedata.ucnhash_CAPI -unittest.mock.create_autospec # Exists at runtime, but missing from stubs _ast.Tuple.dims @@ -169,7 +165,6 @@ multiprocessing.managers.SharedMemoryServer.shutdown multiprocessing.managers.SharedMemoryServer.track_segment pyexpat.XMLParserType.SkippedEntityHandler pyexpat.XMLParserType.intern -sched.Event.sequence types.CoroutineType.cr_origin typing._SpecialForm.__call__ unicodedata.UCD.is_normalized