Skip to content
Open
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
From 83cf9b2dcc569149510c3a9118b9a7dc44144232 Mon Sep 17 00:00:00 2001
From 2c74a0f55cf0314339a855f6a37297298d73e0e4 Mon Sep 17 00:00:00 2001
From: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Date: Mon, 26 Sep 2022 12:55:07 -0700
Subject: [PATCH] Remove use of LiteralString in builtins (#13743)

---
mypy/typeshed/stdlib/builtins.pyi | 127 +-----------------------------
1 file changed, 1 insertion(+), 126 deletions(-)
mypy/typeshed/stdlib/builtins.pyi | 130 +-----------------------------
1 file changed, 2 insertions(+), 128 deletions(-)

diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi
index 0fe6d9a69..1808e28e2 100644
index 6feba7ea2..8e25d8aea 100644
--- a/mypy/typeshed/stdlib/builtins.pyi
+++ b/mypy/typeshed/stdlib/builtins.pyi
@@ -65,7 +65,7 @@ from typing import ( # noqa: Y022,UP035
)
@@ -66,7 +66,7 @@ from typing import ( # noqa: Y022 # ruff: ignore[deprecated-import]

# we can't import `Literal` from typing or mypy crashes: see #11247
-from typing_extensions import Literal, LiteralString, Self, TypeIs, TypeVarTuple, deprecated, disjoint_base # noqa: Y023, UP035
+from typing_extensions import Literal, Self, TypeIs, TypeVarTuple, deprecated, disjoint_base # noqa: Y023, UP035
# ruff: ignore[deprecated-import]
-from typing_extensions import Literal, LiteralString, Self, TypeIs, TypeVarTuple, deprecated, disjoint_base # noqa: Y023
+from typing_extensions import Literal, Self, TypeIs, TypeVarTuple, deprecated, disjoint_base # noqa: Y023

if sys.version_info >= (3, 14):
from _typeshed import AnnotateFunc
@@ -489,20 +489,8 @@ class str(Sequence[str]):
@@ -484,20 +484,8 @@ class str(Sequence[str]):
def __new__(cls, object: object = "") -> Self: ...
@overload
def __new__(cls, object: ReadableBuffer, encoding: str = "utf-8", errors: str = "strict") -> Self: ...
Expand All @@ -41,7 +41,7 @@ index 0fe6d9a69..1808e28e2 100644
def center(self, width: SupportsIndex, fillchar: str = " ", /) -> str: ... # type: ignore[misc]

def count(self, sub: str, start: SupportsIndex | None = None, end: SupportsIndex | None = None, /) -> int: ...
@@ -510,17 +498,9 @@ class str(Sequence[str]):
@@ -505,17 +493,9 @@ class str(Sequence[str]):
def endswith(
self, suffix: str | tuple[str, ...], start: SupportsIndex | None = None, end: SupportsIndex | None = None, /
) -> bool: ...
Expand All @@ -59,7 +59,7 @@ index 0fe6d9a69..1808e28e2 100644
def format(self, *args: object, **kwargs: object) -> str: ...

def format_map(self, mapping: _FormatMapMapping, /) -> str: ...
@@ -537,119 +517,38 @@ class str(Sequence[str]):
@@ -532,119 +512,38 @@ class str(Sequence[str]):
def isspace(self) -> bool: ...
def istitle(self) -> bool: ...
def isupper(self) -> bool: ...
Expand Down Expand Up @@ -179,7 +179,7 @@ index 0fe6d9a69..1808e28e2 100644
def zfill(self, width: SupportsIndex, /) -> str: ... # type: ignore[misc]

if sys.version_info >= (3, 15):
@@ -677,49 +576,25 @@ class str(Sequence[str]):
@@ -672,51 +571,26 @@ class str(Sequence[str]):
@staticmethod
@overload
def maketrans(x: str, y: str, z: str, /) -> dict[int, int | None]: ...
Expand Down Expand Up @@ -214,7 +214,10 @@ index 0fe6d9a69..1808e28e2 100644
- @overload
- def __mod__(self: LiteralString, value: LiteralString | tuple[LiteralString, ...], /) -> LiteralString: ...
- @overload
def __mod__(self, value: Any, /) -> str: ...
- def __mod__(self, value: object, /) -> str: ...
-
+ def __mod__(self, value: Any, /) -> str: ...
def __rmod__(self, value: str, /) -> str: ...
-
- @overload
- def __mul__(self: LiteralString, value: SupportsIndex, /) -> LiteralString: ...
Expand All @@ -230,5 +233,5 @@ index 0fe6d9a69..1808e28e2 100644

def __getnewargs__(self) -> tuple[str]: ...
--
2.54.0
2.56.0

2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/_codecs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class _EncodingMap:

_DecodeCharMap: TypeAlias = SupportsGetItem[int, str | int | None]
_EncodeCharMap: TypeAlias = SupportsGetItem[int, bytes | int | None] | _EncodingMap
_Handler: TypeAlias = Callable[[UnicodeError], tuple[str | bytes, int]]
_Handler: TypeAlias = Callable[[UnicodeEncodeError | UnicodeDecodeError | UnicodeTranslateError], tuple[str | bytes, int]]
_SearchFunction: TypeAlias = Callable[[str], codecs.CodecInfo | None]

def register(search_function: _SearchFunction, /) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/_collections_abc.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from abc import abstractmethod
from types import MappingProxyType
from typing import ( # noqa: Y022,Y038,UP035,Y057
from typing import ( # noqa: Y022,Y038,Y057 # ruff: ignore[deprecated-import]
AbstractSet as Set,
AsyncGenerator as AsyncGenerator,
AsyncIterable as AsyncIterable,
Expand Down
72 changes: 65 additions & 7 deletions mypy/typeshed/stdlib/_hashlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,39 @@ import sys
from _typeshed import ReadableBuffer
from collections.abc import Callable
from types import ModuleType
from typing import AnyStr, Protocol, TypeAlias, final, overload, type_check_only
from typing import AnyStr, Literal, Protocol, TypeAlias, final, overload, type_check_only
from typing_extensions import Self, disjoint_base

_DigestMod: TypeAlias = str | Callable[[], _HashObject] | ModuleType | None
_FixedDigestName: TypeAlias = Literal[
"md5",
"MD5",
"sha1",
"SHA1",
"sha224",
"SHA224",
"sha256",
"SHA256",
"sha384",
"SHA384",
"sha512",
"SHA512",
"sha3_224",
"sha3-224",
"SHA3-224",
"sha3_256",
"sha3-256",
"SHA3-256",
"sha3_384",
"sha3-384",
"SHA3-384",
"sha3_512",
"sha3-512",
"SHA3-512",
]
_XofDigestName: TypeAlias = Literal[
"shake_128", "shake128", "SHAKE128", "shake-128", "SHAKE-128", "shake_256", "shake256", "SHAKE256", "shake-256", "SHAKE-256"
]

openssl_md_meth_names: frozenset[str]

Expand All @@ -22,17 +51,31 @@ class _HashObject(Protocol):
def hexdigest(self) -> str: ...
def update(self, obj: ReadableBuffer, /) -> None: ...

@type_check_only
class _HashObjectWithOptionalLength(Protocol):
@property
def digest_size(self) -> int: ...
@property
def block_size(self) -> int: ...
@property
def name(self) -> str: ...
def copy(self) -> Self: ...
# Depending on the algorithm, length is either required or not accepted.
def digest(self, length: int = ...) -> bytes: ...
def hexdigest(self, length: int = ...) -> str: ...
def update(self, obj: ReadableBuffer, /) -> None: ...

@disjoint_base
class HASH:
class HASH(_HashObjectWithOptionalLength):
@property
def digest_size(self) -> int: ...
@property
def block_size(self) -> int: ...
@property
def name(self) -> str: ...
def copy(self) -> Self: ...
def digest(self) -> bytes: ...
def hexdigest(self) -> str: ...
def digest(self) -> bytes: ... # type: ignore[override]
def hexdigest(self) -> str: ... # type: ignore[override]
def update(self, obj: ReadableBuffer, /) -> None: ...

class UnsupportedDigestmodError(ValueError): ...
Expand Down Expand Up @@ -63,9 +106,19 @@ def get_fips_mode() -> int: ...
def hmac_new(key: ReadableBuffer, msg: ReadableBuffer = b"", digestmod: _DigestMod = None) -> HMAC: ...

if sys.version_info >= (3, 13):
@overload
def new(
name: str, data: ReadableBuffer = b"", *, usedforsecurity: bool = True, string: ReadableBuffer | None = None
name: _XofDigestName, data: ReadableBuffer = b"", *, usedforsecurity: bool = True, string: ReadableBuffer | None = None
) -> HASHXOF: ...
@overload
def new(
name: _FixedDigestName, data: ReadableBuffer = b"", *, usedforsecurity: bool = True, string: ReadableBuffer | None = None
) -> HASH: ...
@overload
def new(
name: str, data: ReadableBuffer = b"", *, usedforsecurity: bool = True, string: ReadableBuffer | None = None
) -> _HashObjectWithOptionalLength: ...

def openssl_md5(
data: ReadableBuffer = b"", *, usedforsecurity: bool = True, string: ReadableBuffer | None = None
) -> HASH: ...
Expand Down Expand Up @@ -102,9 +155,14 @@ if sys.version_info >= (3, 13):
def openssl_shake_256(
data: ReadableBuffer = b"", *, usedforsecurity: bool = True, string: ReadableBuffer | None = None
) -> HASHXOF: ...

else:
def new(name: str, string: ReadableBuffer = b"", *, usedforsecurity: bool = True) -> HASH: ...
@overload
def new(name: _XofDigestName, string: ReadableBuffer = b"", *, usedforsecurity: bool = True) -> HASHXOF: ...
@overload
def new(name: _FixedDigestName, string: ReadableBuffer = b"", *, usedforsecurity: bool = True) -> HASH: ...
@overload
def new(name: str, string: ReadableBuffer = b"", *, usedforsecurity: bool = True) -> _HashObjectWithOptionalLength: ...

def openssl_md5(string: ReadableBuffer = b"", *, usedforsecurity: bool = True) -> HASH: ...
def openssl_sha1(string: ReadableBuffer = b"", *, usedforsecurity: bool = True) -> HASH: ...
def openssl_sha224(string: ReadableBuffer = b"", *, usedforsecurity: bool = True) -> HASH: ...
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/_interpreters.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_config(id: SupportsIndex, *, restrict: bool = False) -> types.SimpleName
def whence(id: SupportsIndex) -> _Whence: ...
def exec(
id: SupportsIndex, code: str | types.CodeType | Callable[[], object], shared: _SharedDict = {}, *, restrict: bool = False
) -> None | types.SimpleNamespace: ...
) -> types.SimpleNamespace | None: ...
def call(
id: SupportsIndex,
callable: Callable[..., _R],
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/_ssl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class _SSLContext:
post_handshake_auth: bool
protocol: int
security_level: int
sni_callback: Callable[[SSLObject, str, SSLContext], None | int] | None
sni_callback: Callable[[SSLObject, str, SSLContext], int | None] | None
verify_flags: int
verify_mode: int
def __new__(cls, protocol: int, /) -> Self: ...
Expand Down
14 changes: 13 additions & 1 deletion mypy/typeshed/stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class SupportsTrunc(Protocol):

# The second and third overload could technically be combined, but splitting
# them works better with some type checkers.
class SupportsGet(Protocol[_KT_contra, _VT_co]): # type: ignore[misc] # Covariant type as parameter
class SupportsGet(Protocol[_KT_contra, _VT_co]): # type: ignore[misc] # ty:ignore[invalid-protocol] # Covariant type as parameter
@overload
def get(self, key: _KT_contra, /) -> _VT_co | None: ...
@overload
Expand All @@ -189,6 +189,18 @@ class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]):
def keys(self) -> Iterable[_KT]: ...
def __getitem__(self, key: _KT, /) -> _VT_co: ...

class SupportsKeysAndGet(Protocol[_KT, _VT_co]): # type: ignore[misc] # ty:ignore[invalid-protocol] # Covariant type as parameter
def keys(self) -> Iterable[_KT]: ...

@overload
def get(self, key: _KT, /) -> _VT_co | None: ...
@overload
def get( # pyrefly: ignore[invalid-variance]
self, key: _KT, default: _VT_co, / # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues] # Covariant type as parameter
) -> _VT_co: ...
@overload
def get(self, key: _KT, default: _T, /) -> _VT_co | _T: ...

# stable
class SupportsGetItem(Protocol[_KT_contra, _VT_co]):
def __getitem__(self, key: _KT_contra, /) -> _VT_co: ...
Expand Down
Loading
Loading