Skip to content

Commit 2b323be

Browse files
authored
Add typeshed aliases to the types accepted by int and float constructors (#10707)
Ref #10630 (comment)
1 parent 381fc57 commit 2b323be

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

stdlib/_typeshed/__init__.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ from collections.abc import Awaitable, Callable, Iterable, Sequence, Set as Abst
77
from dataclasses import Field
88
from os import PathLike
99
from types import FrameType, TracebackType
10-
from typing import Any, AnyStr, ClassVar, Generic, Protocol, TypeVar, overload
11-
from typing_extensions import Buffer, Final, Literal, LiteralString, TypeAlias, final
10+
from typing import Any, AnyStr, ClassVar, Generic, Protocol, SupportsFloat, SupportsInt, TypeVar, overload
11+
from typing_extensions import Buffer, Final, Literal, LiteralString, SupportsIndex, TypeAlias, final
1212

1313
_KT = TypeVar("_KT")
1414
_KT_co = TypeVar("_KT_co", covariant=True)
@@ -312,3 +312,7 @@ TraceFunction: TypeAlias = Callable[[FrameType, str, Any], TraceFunction | None]
312312
# https://github.com/microsoft/pyright/issues/4339
313313
class DataclassInstance(Protocol):
314314
__dataclass_fields__: ClassVar[dict[str, Field[Any]]]
315+
316+
# Anything that can be passed to the int/float constructors
317+
ConvertibleToInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc
318+
ConvertibleToFloat: TypeAlias = str | ReadableBuffer | SupportsFloat | SupportsIndex

stdlib/builtins.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import types
55
from _collections_abc import dict_items, dict_keys, dict_values
66
from _typeshed import (
77
AnyStr_co,
8+
ConvertibleToFloat,
9+
ConvertibleToInt,
810
FileDescriptorOrPath,
911
OpenBinaryMode,
1012
OpenBinaryModeReading,
@@ -24,7 +26,6 @@ from _typeshed import (
2426
SupportsRDivMod,
2527
SupportsRichComparison,
2628
SupportsRichComparisonT,
27-
SupportsTrunc,
2829
SupportsWrite,
2930
)
3031
from collections.abc import Awaitable, Callable, Iterable, Iterator, MutableSet, Reversible, Set as AbstractSet, Sized
@@ -48,7 +49,6 @@ from typing import ( # noqa: Y022
4849
SupportsBytes,
4950
SupportsComplex,
5051
SupportsFloat,
51-
SupportsInt,
5252
TypeVar,
5353
overload,
5454
type_check_only,
@@ -221,7 +221,7 @@ _LiteralInteger = _PositiveInteger | _NegativeInteger | Literal[0] # noqa: Y026
221221

222222
class int:
223223
@overload
224-
def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self: ...
224+
def __new__(cls, __x: ConvertibleToInt = ...) -> Self: ...
225225
@overload
226226
def __new__(cls, __x: str | bytes | bytearray, base: SupportsIndex) -> Self: ...
227227
if sys.version_info >= (3, 8):
@@ -327,7 +327,7 @@ class int:
327327
def __index__(self) -> int: ...
328328

329329
class float:
330-
def __new__(cls, __x: SupportsFloat | SupportsIndex | str | ReadableBuffer = ...) -> Self: ...
330+
def __new__(cls, __x: ConvertibleToFloat = ...) -> Self: ...
331331
def as_integer_ratio(self) -> tuple[int, int]: ...
332332
def hex(self) -> str: ...
333333
def is_integer(self) -> bool: ...

stdlib/multiprocessing/util.pyi

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import threading
2-
from _typeshed import Incomplete, ReadableBuffer, SupportsTrunc, Unused
2+
from _typeshed import ConvertibleToInt, Incomplete, Unused
33
from collections.abc import Callable, Iterable, Mapping, MutableMapping, Sequence
44
from logging import Logger, _Level as _LoggingLevel
5-
from typing import Any, SupportsInt
6-
from typing_extensions import SupportsIndex
5+
from typing import Any
76

87
__all__ = [
98
"sub_debug",
@@ -77,9 +76,4 @@ class ForkAwareLocal(threading.local): ...
7776
MAXFD: int
7877

7978
def close_all_fds_except(fds: Iterable[int]) -> None: ...
80-
def spawnv_passfds(
81-
path: bytes,
82-
# args is anything that can be passed to the int constructor
83-
args: Sequence[str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc],
84-
passfds: Sequence[int],
85-
) -> int: ...
79+
def spawnv_passfds(path: bytes, args: Sequence[ConvertibleToInt], passfds: Sequence[int]) -> int: ...

0 commit comments

Comments
 (0)