Add typeshed aliases to the types accepted by int and float constructors (#10707)

Ref https://github.com/python/typeshed/pull/10630#discussion_r1321646168
This commit is contained in:
Ali Hamdan
2023-09-23 04:50:43 +02:00
committed by GitHub
parent 381fc572a8
commit 2b323bed50
3 changed files with 13 additions and 15 deletions

View File

@@ -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, SupportsFloat, 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,7 @@ 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]]]
# Anything that can be passed to the int/float constructors
ConvertibleToInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc
ConvertibleToFloat: TypeAlias = str | ReadableBuffer | SupportsFloat | SupportsIndex

View File

@@ -5,6 +5,8 @@ import types
from _collections_abc import dict_items, dict_keys, dict_values
from _typeshed import (
AnyStr_co,
ConvertibleToFloat,
ConvertibleToInt,
FileDescriptorOrPath,
OpenBinaryMode,
OpenBinaryModeReading,
@@ -24,7 +26,6 @@ from _typeshed import (
SupportsRDivMod,
SupportsRichComparison,
SupportsRichComparisonT,
SupportsTrunc,
SupportsWrite,
)
from collections.abc import Awaitable, Callable, Iterable, Iterator, MutableSet, Reversible, Set as AbstractSet, Sized
@@ -48,7 +49,6 @@ from typing import ( # noqa: Y022
SupportsBytes,
SupportsComplex,
SupportsFloat,
SupportsInt,
TypeVar,
overload,
type_check_only,
@@ -221,7 +221,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: ConvertibleToInt = ...) -> Self: ...
@overload
def __new__(cls, __x: str | bytes | bytearray, base: SupportsIndex) -> Self: ...
if sys.version_info >= (3, 8):
@@ -327,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: ...

View File

@@ -1,9 +1,8 @@
import threading
from _typeshed import Incomplete, ReadableBuffer, SupportsTrunc, 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, 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[ConvertibleToInt], passfds: Sequence[int]) -> int: ...