From b4fab4aa3cd4ca5deb9d4e93499a9991859b7998 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 14 Jun 2022 15:39:13 +0100 Subject: [PATCH 1/4] Fix miscellaneous invalid `TypeVar` usages --- stdlib/ctypes/__init__.pyi | 7 +++++-- stdlib/difflib.pyi | 11 +++++++++- stdlib/mailbox.pyi | 7 ++++--- stdlib/multiprocessing/shared_memory.pyi | 7 +++++-- stdlib/tempfile.pyi | 20 ++++++++++--------- .../flake8_plugin_utils/plugin.pyi | 7 +++++-- 6 files changed, 40 insertions(+), 19 deletions(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 53a382ec0e71..53b1147897be 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -164,7 +164,7 @@ def POINTER(type: type[_CT]) -> type[pointer[_CT]]: ... class pointer(Generic[_CT], _PointerLike, _CData): _type_: type[_CT] contents: _CT - def __init__(self, arg: _CT = ...) -> None: ... + def __init__(self, arg: _CT) -> None: ... @overload def __getitem__(self, __i: int) -> _CT: ... @overload @@ -190,7 +190,10 @@ def wstring_at(address: _CVoidConstPLike, size: int = ...) -> str: ... class _SimpleCData(Generic[_T], _CData): value: _T - def __init__(self, value: _T = ...) -> None: ... + @overload + def __init__(self) -> None: ... + @overload + def __init__(self, value: _T) -> None: ... class c_byte(_SimpleCData[int]): ... diff --git a/stdlib/difflib.pyi b/stdlib/difflib.pyi index 87e3768034bf..854a53d433ae 100644 --- a/stdlib/difflib.pyi +++ b/stdlib/difflib.pyi @@ -28,8 +28,17 @@ class Match(NamedTuple): size: int class SequenceMatcher(Generic[_T]): + @overload + def __init__(self, isjunk: Callable[[_T], bool] | None, a: Sequence[_T], b: Sequence[_T], autojunk: bool = ...) -> None: ... + @overload + def __init__(self, *, a: Sequence[_T], b: Sequence[_T], autojunk: bool = ...) -> None: ... + @overload def __init__( - self, isjunk: Callable[[_T], bool] | None = ..., a: Sequence[_T] = ..., b: Sequence[_T] = ..., autojunk: bool = ... + self: SequenceMatcher[str], + isjunk: Callable[[str], bool] | None = ..., + a: Sequence[str] = ..., + b: Sequence[str] = ..., + autojunk: bool = ..., ) -> None: ... def set_seqs(self, a: Sequence[_T], b: Sequence[_T]) -> None: ... def set_seq1(self, a: Sequence[_T]) -> None: ... diff --git a/stdlib/mailbox.pyi b/stdlib/mailbox.pyi index 64183cd0b3a4..3169e8cfa689 100644 --- a/stdlib/mailbox.pyi +++ b/stdlib/mailbox.pyi @@ -46,9 +46,10 @@ class Mailbox(Generic[_MessageT]): _path: bytes | str # undocumented _factory: Callable[[IO[Any]], _MessageT] | None # undocumented - def __init__( - self, path: StrOrBytesPath, factory: Callable[[IO[Any]], _MessageT] | None = ..., create: bool = ... - ) -> None: ... + @overload + def __init__(self, path: StrOrBytesPath, factory: Callable[[IO[Any]], _MessageT], create: bool = ...) -> None: ... + @overload + def __init__(self, path: StrOrBytesPath, factory: None = ..., create: bool = ...) -> None: ... @abstractmethod def add(self, message: _MessageData) -> str: ... @abstractmethod diff --git a/stdlib/multiprocessing/shared_memory.pyi b/stdlib/multiprocessing/shared_memory.pyi index 76ccedaf478e..3ce0ca3863cc 100644 --- a/stdlib/multiprocessing/shared_memory.pyi +++ b/stdlib/multiprocessing/shared_memory.pyi @@ -1,7 +1,7 @@ import sys from _typeshed import Self from collections.abc import Iterable -from typing import Any, Generic, TypeVar +from typing import Any, Generic, TypeVar, overload if sys.version_info >= (3, 9): from types import GenericAlias @@ -23,7 +23,10 @@ class SharedMemory: class ShareableList(Generic[_SLT]): shm: SharedMemory - def __init__(self, sequence: Iterable[_SLT] | None = ..., *, name: str | None = ...) -> None: ... + @overload + def __init__(self, sequence: None = ..., *, name: str | None = ...) -> None: ... + @overload + def __init__(self, sequence: Iterable[_SLT], *, name: str | None = ...) -> None: ... def __getitem__(self, position: int) -> _SLT: ... def __setitem__(self, position: int, value: _SLT) -> None: ... def __reduce__(self: Self) -> tuple[Self, tuple[_SLT, ...]]: ... diff --git a/stdlib/tempfile.pyi b/stdlib/tempfile.pyi index b27b881d5b5b..2c096f0fb4de 100644 --- a/stdlib/tempfile.pyi +++ b/stdlib/tempfile.pyi @@ -355,25 +355,27 @@ class TemporaryDirectory(Generic[AnyStr]): @overload def __init__( self: TemporaryDirectory[str], - suffix: None = ..., - prefix: None = ..., - dir: None = ..., + suffix: str | None = ..., + prefix: str | None = ..., + dir: StrPath | None = ..., ignore_cleanup_errors: bool = ..., ) -> None: ... @overload def __init__( - self, - suffix: AnyStr | None = ..., - prefix: AnyStr | None = ..., - dir: GenericPath[AnyStr] | None = ..., + self: TemporaryDirectory[bytes], + suffix: bytes | None = ..., + prefix: bytes | None = ..., + dir: BytesPath | None = ..., ignore_cleanup_errors: bool = ..., ) -> None: ... else: @overload - def __init__(self: TemporaryDirectory[str], suffix: None = ..., prefix: None = ..., dir: None = ...) -> None: ... + def __init__( + self: TemporaryDirectory[str], suffix: str | None = ..., prefix: str | None = ..., dir: StrPath | None = ... + ) -> None: ... @overload def __init__( - self, suffix: AnyStr | None = ..., prefix: AnyStr | None = ..., dir: GenericPath[AnyStr] | None = ... + self: TemporaryDirectory[bytes], suffix: bytes | None = ..., prefix: bytes | None = ..., dir: BytesPath | None = ... ) -> None: ... def cleanup(self) -> None: ... diff --git a/stubs/flake8-plugin-utils/flake8_plugin_utils/plugin.pyi b/stubs/flake8-plugin-utils/flake8_plugin_utils/plugin.pyi index bba6537fffb8..4dd4b047206e 100644 --- a/stubs/flake8-plugin-utils/flake8_plugin_utils/plugin.pyi +++ b/stubs/flake8-plugin-utils/flake8_plugin_utils/plugin.pyi @@ -1,7 +1,7 @@ import argparse import ast from collections.abc import Iterable, Iterator -from typing import Any, Generic, TypeVar +from typing import Any, Generic, TypeVar, overload from typing_extensions import TypeAlias FLAKE8_ERROR: TypeAlias = tuple[int, int, str, type[Any]] @@ -18,7 +18,10 @@ class Error: class Visitor(ast.NodeVisitor, Generic[TConfig]): errors: list[Error] - def __init__(self, config: TConfig | None = ...) -> None: ... + @overload + def __init__(self, config: TConfig) -> None: ... + @overload + def __init__(self, config: None = ...) -> None: ... @property def config(self) -> TConfig: ... def error_from_node(self, error: type[Error], node: ast.AST, **kwargs: Any) -> None: ... From a76c2c5f5ef6a79d1cb803cbd16b9cbe5e612dcc Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 14 Jun 2022 15:41:35 +0100 Subject: [PATCH 2/4] Unused allowlist entry --- tests/stubtest_allowlists/py3_common.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index 95bcca043261..91d166821ff7 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -88,7 +88,6 @@ ctypes.memset # CFunctionType ctypes.pointer # imported C function ctypes.string_at # docstring argument name is wrong ctypes.wstring_at # docstring argument name is wrong -difflib.SequenceMatcher.__init__ # mypy default value for generic parameter issues. See https://github.com/python/mypy/issues/3737 distutils.command.bdist_packager # It exists in docs as package name but not in code except as a mention in a comment. distutils.version.Version._cmp # class should have declared this distutils.version.Version.parse # class should have declared this From 658ba6ea547f2531b3ae35d9f8dd6f9b33807e07 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 14 Jun 2022 15:53:10 +0100 Subject: [PATCH 3/4] Revert changes to `ctypes._SimpleCData` --- stdlib/ctypes/__init__.pyi | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 53b1147897be..ee26cbddefe4 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -190,10 +190,7 @@ def wstring_at(address: _CVoidConstPLike, size: int = ...) -> str: ... class _SimpleCData(Generic[_T], _CData): value: _T - @overload - def __init__(self) -> None: ... - @overload - def __init__(self, value: _T) -> None: ... + def __init__(self, value: _T = ...) -> None: ... class c_byte(_SimpleCData[int]): ... From d98b07b49b9262bad6f398769fccf9a9781a6fa3 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 14 Jun 2022 16:31:42 +0100 Subject: [PATCH 4/4] Update stubs/flake8-plugin-utils/flake8_plugin_utils/plugin.pyi --- stubs/flake8-plugin-utils/flake8_plugin_utils/plugin.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stubs/flake8-plugin-utils/flake8_plugin_utils/plugin.pyi b/stubs/flake8-plugin-utils/flake8_plugin_utils/plugin.pyi index 4dd4b047206e..6949ec275bbd 100644 --- a/stubs/flake8-plugin-utils/flake8_plugin_utils/plugin.pyi +++ b/stubs/flake8-plugin-utils/flake8_plugin_utils/plugin.pyi @@ -19,9 +19,9 @@ class Error: class Visitor(ast.NodeVisitor, Generic[TConfig]): errors: list[Error] @overload - def __init__(self, config: TConfig) -> None: ... - @overload def __init__(self, config: None = ...) -> None: ... + @overload + def __init__(self, config: TConfig) -> None: ... @property def config(self) -> TConfig: ... def error_from_node(self, error: type[Error], node: ast.AST, **kwargs: Any) -> None: ...