From cdf34f86d66c7fd9a55a7d0a5aca625d154c375e Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Tue, 12 Sep 2023 21:30:16 +0200 Subject: [PATCH 1/4] Add a typeshed alias to the type accepted by `int` constructor Ref https://github.com/python/typeshed/pull/10630#discussion_r1321646168 --- stdlib/_typeshed/__init__.pyi | 7 +++++-- stdlib/builtins.pyi | 5 ++--- stdlib/multiprocessing/util.pyi | 12 +++--------- stubs/openpyxl/openpyxl/descriptors/base.pyi | 1 + stubs/pyasn1/pyasn1/type/univ.pyi | 1 + stubs/python-xlib/Xlib/protocol/rq.pyi | 1 + 6 files changed, 13 insertions(+), 14 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 7ae67292e8cd..7171857c7154 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -7,8 +7,8 @@ from collections.abc import Awaitable, Callable, Iterable, Sequence, Set as Abst from dataclasses import Field from os import PathLike from types import FrameType, TracebackType -from typing import Any, AnyStr, ClassVar, Generic, Protocol, TypeVar, overload -from typing_extensions import Buffer, Final, Literal, LiteralString, TypeAlias, final +from typing import Any, AnyStr, ClassVar, Generic, Protocol, SupportsInt, TypeVar, overload +from typing_extensions import Buffer, Final, Literal, LiteralString, SupportsIndex, TypeAlias, final _KT = TypeVar("_KT") _KT_co = TypeVar("_KT_co", covariant=True) @@ -312,3 +312,6 @@ TraceFunction: TypeAlias = Callable[[FrameType, str, Any], TraceFunction | None] # https://github.com/microsoft/pyright/issues/4339 class DataclassInstance(Protocol): __dataclass_fields__: ClassVar[dict[str, Field[Any]]] + +# Any thing that can be passed to the int constructor +_AcceptedByInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc # noqa: Y047 diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index cf4f857c5524..6e73d852edaf 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -24,8 +24,8 @@ from _typeshed import ( SupportsRDivMod, SupportsRichComparison, SupportsRichComparisonT, - SupportsTrunc, SupportsWrite, + _AcceptedByInt, ) from collections.abc import Awaitable, Callable, Iterable, Iterator, MutableSet, Reversible, Set as AbstractSet, Sized from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper @@ -48,7 +48,6 @@ from typing import ( # noqa: Y022 SupportsBytes, SupportsComplex, SupportsFloat, - SupportsInt, TypeVar, overload, type_check_only, @@ -221,7 +220,7 @@ _LiteralInteger = _PositiveInteger | _NegativeInteger | Literal[0] # noqa: Y026 class int: @overload - def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self: ... + def __new__(cls, __x: _AcceptedByInt = ...) -> Self: ... @overload def __new__(cls, __x: str | bytes | bytearray, base: SupportsIndex) -> Self: ... if sys.version_info >= (3, 8): diff --git a/stdlib/multiprocessing/util.pyi b/stdlib/multiprocessing/util.pyi index 7ca650511e51..a9b553b5e049 100644 --- a/stdlib/multiprocessing/util.pyi +++ b/stdlib/multiprocessing/util.pyi @@ -1,9 +1,8 @@ import threading -from _typeshed import Incomplete, ReadableBuffer, SupportsTrunc, Unused +from _typeshed import Incomplete, Unused, _AcceptedByInt from collections.abc import Callable, Iterable, Mapping, MutableMapping, Sequence from logging import Logger, _Level as _LoggingLevel -from typing import Any, SupportsInt -from typing_extensions import SupportsIndex +from typing import Any __all__ = [ "sub_debug", @@ -77,9 +76,4 @@ class ForkAwareLocal(threading.local): ... MAXFD: int def close_all_fds_except(fds: Iterable[int]) -> None: ... -def spawnv_passfds( - path: bytes, - # args is anything that can be passed to the int constructor - args: Sequence[str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc], - passfds: Sequence[int], -) -> int: ... +def spawnv_passfds(path: bytes, args: Sequence[_AcceptedByInt], passfds: Sequence[int]) -> int: ... diff --git a/stubs/openpyxl/openpyxl/descriptors/base.pyi b/stubs/openpyxl/openpyxl/descriptors/base.pyi index 6e62f1e88f28..7d97e03dbd56 100644 --- a/stubs/openpyxl/openpyxl/descriptors/base.pyi +++ b/stubs/openpyxl/openpyxl/descriptors/base.pyi @@ -18,6 +18,7 @@ _M = TypeVar("_M", int, float) _ExpectedTypeParam: TypeAlias = type[_T] | tuple[type[_T], ...] _ConvertibleToMultiCellRange: TypeAlias = MultiCellRange | str | Iterable[CellRange] +# TODO: replace by `_typeshed._AcceptedByInt` when it is included in type checkers _ConvertibleToInt: TypeAlias = int | str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc _ConvertibleToFloat: TypeAlias = float | SupportsFloat | SupportsIndex | str | ReadableBuffer # Since everything is convertible to a bool, this restricts to only intended expected types diff --git a/stubs/pyasn1/pyasn1/type/univ.pyi b/stubs/pyasn1/pyasn1/type/univ.pyi index 6330ba5d441b..e49c8306f159 100644 --- a/stubs/pyasn1/pyasn1/type/univ.pyi +++ b/stubs/pyasn1/pyasn1/type/univ.pyi @@ -6,6 +6,7 @@ from typing_extensions import Self, SupportsIndex, TypeAlias from pyasn1.type import base, constraint, namedtype, namedval from pyasn1.type.tag import TagSet +# TODO: replace by `_typeshed._AcceptedByInt` when it is included in type checkers _SizedIntegerable: TypeAlias = ReadableBuffer | str | SupportsInt | SupportsIndex | SupportsTrunc NoValue = base.NoValue diff --git a/stubs/python-xlib/Xlib/protocol/rq.pyi b/stubs/python-xlib/Xlib/protocol/rq.pyi index a3ff00307969..43a1888348c9 100644 --- a/stubs/python-xlib/Xlib/protocol/rq.pyi +++ b/stubs/python-xlib/Xlib/protocol/rq.pyi @@ -14,6 +14,7 @@ from Xlib.ext.xinput import ClassInfoClass from Xlib.protocol import display _T = TypeVar("_T") +# TODO: replace by `_typeshed._AcceptedByInt` when it is included in type checkers _IntNew: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc _ModifierMappingList8Elements: TypeAlias = Sequence[Sequence[int]] From 734d46e720f91a5e60bc93c52392b176293c6119 Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Tue, 12 Sep 2023 23:24:31 +0200 Subject: [PATCH 2/4] CR --- stdlib/_typeshed/__init__.pyi | 4 ++-- stdlib/builtins.pyi | 4 ++-- stdlib/multiprocessing/util.pyi | 4 ++-- stubs/openpyxl/openpyxl/descriptors/base.pyi | 2 +- stubs/pyasn1/pyasn1/type/univ.pyi | 2 +- stubs/python-xlib/Xlib/protocol/rq.pyi | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 7171857c7154..aa4861dfe0ec 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -313,5 +313,5 @@ TraceFunction: TypeAlias = Callable[[FrameType, str, Any], TraceFunction | None] class DataclassInstance(Protocol): __dataclass_fields__: ClassVar[dict[str, Field[Any]]] -# Any thing that can be passed to the int constructor -_AcceptedByInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc # noqa: Y047 +# Anything that can be passed to the int constructor +AcceptedByInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 6e73d852edaf..bdd3db88340f 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -4,6 +4,7 @@ import sys import types from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import ( + AcceptedByInt, AnyStr_co, FileDescriptorOrPath, OpenBinaryMode, @@ -25,7 +26,6 @@ from _typeshed import ( SupportsRichComparison, SupportsRichComparisonT, SupportsWrite, - _AcceptedByInt, ) from collections.abc import Awaitable, Callable, Iterable, Iterator, MutableSet, Reversible, Set as AbstractSet, Sized from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper @@ -220,7 +220,7 @@ _LiteralInteger = _PositiveInteger | _NegativeInteger | Literal[0] # noqa: Y026 class int: @overload - def __new__(cls, __x: _AcceptedByInt = ...) -> Self: ... + def __new__(cls, __x: AcceptedByInt = ...) -> Self: ... @overload def __new__(cls, __x: str | bytes | bytearray, base: SupportsIndex) -> Self: ... if sys.version_info >= (3, 8): diff --git a/stdlib/multiprocessing/util.pyi b/stdlib/multiprocessing/util.pyi index a9b553b5e049..d9e1d71ef445 100644 --- a/stdlib/multiprocessing/util.pyi +++ b/stdlib/multiprocessing/util.pyi @@ -1,5 +1,5 @@ import threading -from _typeshed import Incomplete, Unused, _AcceptedByInt +from _typeshed import AcceptedByInt, Incomplete, Unused from collections.abc import Callable, Iterable, Mapping, MutableMapping, Sequence from logging import Logger, _Level as _LoggingLevel from typing import Any @@ -76,4 +76,4 @@ class ForkAwareLocal(threading.local): ... MAXFD: int def close_all_fds_except(fds: Iterable[int]) -> None: ... -def spawnv_passfds(path: bytes, args: Sequence[_AcceptedByInt], passfds: Sequence[int]) -> int: ... +def spawnv_passfds(path: bytes, args: Sequence[AcceptedByInt], passfds: Sequence[int]) -> int: ... diff --git a/stubs/openpyxl/openpyxl/descriptors/base.pyi b/stubs/openpyxl/openpyxl/descriptors/base.pyi index 7d97e03dbd56..3d599e60dce2 100644 --- a/stubs/openpyxl/openpyxl/descriptors/base.pyi +++ b/stubs/openpyxl/openpyxl/descriptors/base.pyi @@ -18,7 +18,7 @@ _M = TypeVar("_M", int, float) _ExpectedTypeParam: TypeAlias = type[_T] | tuple[type[_T], ...] _ConvertibleToMultiCellRange: TypeAlias = MultiCellRange | str | Iterable[CellRange] -# TODO: replace by `_typeshed._AcceptedByInt` when it is included in type checkers +# TODO: replace with `_typeshed.AcceptedByInt` when it is included in type checkers _ConvertibleToInt: TypeAlias = int | str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc _ConvertibleToFloat: TypeAlias = float | SupportsFloat | SupportsIndex | str | ReadableBuffer # Since everything is convertible to a bool, this restricts to only intended expected types diff --git a/stubs/pyasn1/pyasn1/type/univ.pyi b/stubs/pyasn1/pyasn1/type/univ.pyi index e49c8306f159..ec8aa15046e1 100644 --- a/stubs/pyasn1/pyasn1/type/univ.pyi +++ b/stubs/pyasn1/pyasn1/type/univ.pyi @@ -6,7 +6,7 @@ from typing_extensions import Self, SupportsIndex, TypeAlias from pyasn1.type import base, constraint, namedtype, namedval from pyasn1.type.tag import TagSet -# TODO: replace by `_typeshed._AcceptedByInt` when it is included in type checkers +# TODO: replace with `_typeshed.AcceptedByInt` when it is included in type checkers _SizedIntegerable: TypeAlias = ReadableBuffer | str | SupportsInt | SupportsIndex | SupportsTrunc NoValue = base.NoValue diff --git a/stubs/python-xlib/Xlib/protocol/rq.pyi b/stubs/python-xlib/Xlib/protocol/rq.pyi index 43a1888348c9..2bba2ecef1db 100644 --- a/stubs/python-xlib/Xlib/protocol/rq.pyi +++ b/stubs/python-xlib/Xlib/protocol/rq.pyi @@ -14,7 +14,7 @@ from Xlib.ext.xinput import ClassInfoClass from Xlib.protocol import display _T = TypeVar("_T") -# TODO: replace by `_typeshed._AcceptedByInt` when it is included in type checkers +# TODO: replace with `_typeshed.AcceptedByInt` when it is included in type checkers _IntNew: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc _ModifierMappingList8Elements: TypeAlias = Sequence[Sequence[int]] From a3b7d80d3b5729fed672f28507c78a1e23d96eb9 Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Wed, 20 Sep 2023 21:29:05 +0200 Subject: [PATCH 3/4] Use ConvertibleToInt name, delete todos --- stdlib/_typeshed/__init__.pyi | 2 +- stdlib/builtins.pyi | 4 ++-- stdlib/multiprocessing/util.pyi | 4 ++-- stubs/openpyxl/openpyxl/descriptors/base.pyi | 1 - stubs/pyasn1/pyasn1/type/univ.pyi | 1 - stubs/python-xlib/Xlib/protocol/rq.pyi | 1 - 6 files changed, 5 insertions(+), 8 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index aa4861dfe0ec..93ab024ebee5 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -314,4 +314,4 @@ class DataclassInstance(Protocol): __dataclass_fields__: ClassVar[dict[str, Field[Any]]] # Anything that can be passed to the int constructor -AcceptedByInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc +ConvertibleToInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index bdd3db88340f..0a340e503eb1 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -4,8 +4,8 @@ import sys import types from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import ( - AcceptedByInt, AnyStr_co, + ConvertibleToInt, FileDescriptorOrPath, OpenBinaryMode, OpenBinaryModeReading, @@ -220,7 +220,7 @@ _LiteralInteger = _PositiveInteger | _NegativeInteger | Literal[0] # noqa: Y026 class int: @overload - def __new__(cls, __x: AcceptedByInt = ...) -> Self: ... + def __new__(cls, __x: ConvertibleToInt = ...) -> Self: ... @overload def __new__(cls, __x: str | bytes | bytearray, base: SupportsIndex) -> Self: ... if sys.version_info >= (3, 8): diff --git a/stdlib/multiprocessing/util.pyi b/stdlib/multiprocessing/util.pyi index d9e1d71ef445..aeb46f85a327 100644 --- a/stdlib/multiprocessing/util.pyi +++ b/stdlib/multiprocessing/util.pyi @@ -1,5 +1,5 @@ import threading -from _typeshed import AcceptedByInt, Incomplete, Unused +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 @@ -76,4 +76,4 @@ class ForkAwareLocal(threading.local): ... MAXFD: int def close_all_fds_except(fds: Iterable[int]) -> None: ... -def spawnv_passfds(path: bytes, args: Sequence[AcceptedByInt], passfds: Sequence[int]) -> int: ... +def spawnv_passfds(path: bytes, args: Sequence[ConvertibleToInt], passfds: Sequence[int]) -> int: ... diff --git a/stubs/openpyxl/openpyxl/descriptors/base.pyi b/stubs/openpyxl/openpyxl/descriptors/base.pyi index 3d599e60dce2..6e62f1e88f28 100644 --- a/stubs/openpyxl/openpyxl/descriptors/base.pyi +++ b/stubs/openpyxl/openpyxl/descriptors/base.pyi @@ -18,7 +18,6 @@ _M = TypeVar("_M", int, float) _ExpectedTypeParam: TypeAlias = type[_T] | tuple[type[_T], ...] _ConvertibleToMultiCellRange: TypeAlias = MultiCellRange | str | Iterable[CellRange] -# TODO: replace with `_typeshed.AcceptedByInt` when it is included in type checkers _ConvertibleToInt: TypeAlias = int | str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc _ConvertibleToFloat: TypeAlias = float | SupportsFloat | SupportsIndex | str | ReadableBuffer # Since everything is convertible to a bool, this restricts to only intended expected types diff --git a/stubs/pyasn1/pyasn1/type/univ.pyi b/stubs/pyasn1/pyasn1/type/univ.pyi index ec8aa15046e1..6330ba5d441b 100644 --- a/stubs/pyasn1/pyasn1/type/univ.pyi +++ b/stubs/pyasn1/pyasn1/type/univ.pyi @@ -6,7 +6,6 @@ from typing_extensions import Self, SupportsIndex, TypeAlias from pyasn1.type import base, constraint, namedtype, namedval from pyasn1.type.tag import TagSet -# TODO: replace with `_typeshed.AcceptedByInt` when it is included in type checkers _SizedIntegerable: TypeAlias = ReadableBuffer | str | SupportsInt | SupportsIndex | SupportsTrunc NoValue = base.NoValue diff --git a/stubs/python-xlib/Xlib/protocol/rq.pyi b/stubs/python-xlib/Xlib/protocol/rq.pyi index 2bba2ecef1db..a3ff00307969 100644 --- a/stubs/python-xlib/Xlib/protocol/rq.pyi +++ b/stubs/python-xlib/Xlib/protocol/rq.pyi @@ -14,7 +14,6 @@ from Xlib.ext.xinput import ClassInfoClass from Xlib.protocol import display _T = TypeVar("_T") -# TODO: replace with `_typeshed.AcceptedByInt` when it is included in type checkers _IntNew: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc _ModifierMappingList8Elements: TypeAlias = Sequence[Sequence[int]] From 5f0c9836c3bae19b94420b1f8206284424820b52 Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Wed, 20 Sep 2023 21:33:01 +0200 Subject: [PATCH 4/4] Add ConvertibleToFloat --- stdlib/_typeshed/__init__.pyi | 5 +++-- stdlib/builtins.pyi | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 93ab024ebee5..8e92138c748a 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -7,7 +7,7 @@ from collections.abc import Awaitable, Callable, Iterable, Sequence, Set as Abst from dataclasses import Field from os import PathLike from types import FrameType, TracebackType -from typing import Any, AnyStr, ClassVar, Generic, Protocol, SupportsInt, TypeVar, overload +from typing import Any, AnyStr, ClassVar, Generic, Protocol, SupportsFloat, SupportsInt, TypeVar, overload from typing_extensions import Buffer, Final, Literal, LiteralString, SupportsIndex, TypeAlias, final _KT = TypeVar("_KT") @@ -313,5 +313,6 @@ TraceFunction: TypeAlias = Callable[[FrameType, str, Any], TraceFunction | None] class DataclassInstance(Protocol): __dataclass_fields__: ClassVar[dict[str, Field[Any]]] -# Anything that can be passed to the int constructor +# Anything that can be passed to the int/float constructors ConvertibleToInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc +ConvertibleToFloat: TypeAlias = str | ReadableBuffer | SupportsFloat | SupportsIndex diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 0a340e503eb1..4fc75dfb17c6 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -5,6 +5,7 @@ import types from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import ( AnyStr_co, + ConvertibleToFloat, ConvertibleToInt, FileDescriptorOrPath, OpenBinaryMode, @@ -326,7 +327,7 @@ class int: def __index__(self) -> int: ... class float: - def __new__(cls, __x: SupportsFloat | SupportsIndex | str | ReadableBuffer = ...) -> Self: ... + def __new__(cls, __x: ConvertibleToFloat = ...) -> Self: ... def as_integer_ratio(self) -> tuple[int, int]: ... def hex(self) -> str: ... def is_integer(self) -> bool: ...