Skip to content

Commit b3c37be

Browse files
authored
[http.cookies] Modernize (#15266)
* Inline `_DataType` type alias. * Replace `Mapping` with protocols. * Replace `list` in argument positions with protocols.
1 parent ad18156 commit b3c37be

2 files changed

Lines changed: 15 additions & 19 deletions

File tree

stdlib/http/cookies.pyi

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
from _typeshed import MaybeNone
2-
from collections.abc import Iterable, Mapping
1+
from _typeshed import MaybeNone, SupportsItems, SupportsKeysAndGetItem
2+
from collections.abc import Container, Iterable
33
from types import GenericAlias
44
from typing import Any, Generic, TypeVar, overload
5-
from typing_extensions import TypeAlias
65

76
__all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
87

9-
_DataType: TypeAlias = str | Mapping[str, str | Morsel[Any]]
108
_T = TypeVar("_T")
119

1210
@overload
@@ -31,27 +29,24 @@ class Morsel(dict[str, Any], Generic[_T]):
3129
def set(self, key: str, val: str, coded_val: _T) -> None: ...
3230
def setdefault(self, key: str, val: str | None = None) -> str: ...
3331
# The dict update can also get a keywords argument so this is incompatible
34-
@overload # type: ignore[override]
35-
def update(self, values: Mapping[str, str]) -> None: ...
36-
@overload
37-
def update(self, values: Iterable[tuple[str, str]]) -> None: ...
32+
def update(self, values: Iterable[tuple[str, str]] | SupportsKeysAndGetItem[str, str]) -> None: ... # type: ignore[override]
3833
def isReservedKey(self, K: str) -> bool: ...
39-
def output(self, attrs: list[str] | None = None, header: str = "Set-Cookie:") -> str: ...
34+
def output(self, attrs: Container[str] | None = None, header: str = "Set-Cookie:") -> str: ...
4035
__str__ = output
41-
def js_output(self, attrs: list[str] | None = None) -> str: ...
42-
def OutputString(self, attrs: list[str] | None = None) -> str: ...
36+
def js_output(self, attrs: Container[str] | None = None) -> str: ...
37+
def OutputString(self, attrs: Container[str] | None = None) -> str: ...
4338
def __eq__(self, morsel: object) -> bool: ...
4439
def __setitem__(self, K: str, V: Any) -> None: ...
4540
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
4641

4742
class BaseCookie(dict[str, Morsel[_T]], Generic[_T]):
48-
def __init__(self, input: _DataType | None = None) -> None: ...
43+
def __init__(self, input: str | SupportsItems[str, str | Morsel[Any]] | None = None) -> None: ...
4944
def value_decode(self, val: str) -> tuple[_T, str]: ...
5045
def value_encode(self, val: _T) -> tuple[str, str]: ...
51-
def output(self, attrs: list[str] | None = None, header: str = "Set-Cookie:", sep: str = "\r\n") -> str: ...
46+
def output(self, attrs: Container[str] | None = None, header: str = "Set-Cookie:", sep: str = "\r\n") -> str: ...
5247
__str__ = output
53-
def js_output(self, attrs: list[str] | None = None) -> str: ...
54-
def load(self, rawdata: _DataType) -> None: ...
48+
def js_output(self, attrs: Container[str] | None = None) -> str: ...
49+
def load(self, rawdata: str | SupportsItems[str, str | Morsel[Any]]) -> None: ...
5550
def __setitem__(self, key: str, value: str | Morsel[_T]) -> None: ...
5651

5752
class SimpleCookie(BaseCookie[str]): ...

stubs/yt-dlp/yt_dlp/cookies.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from collections.abc import Collection, Iterator, KeysView
1+
from _typeshed import SupportsItems
2+
from collections.abc import Iterator, KeysView
23
from enum import Enum
34
from http.cookiejar import Cookie, CookiePolicy, MozillaCookieJar
4-
from http.cookies import SimpleCookie
5-
from typing import Final, TextIO, TypeVar
5+
from http.cookies import Morsel, SimpleCookie
6+
from typing import Any, Final, TextIO, TypeVar
67

78
from . import _LoggerProtocol
89
from .minicurses import MultilinePrinter
@@ -101,4 +102,4 @@ class DataParser:
101102
def pbkdf2_sha1(password: bytes, salt: bytes, iterations: int, key_length: int) -> bytes: ...
102103

103104
class LenientSimpleCookie(SimpleCookie):
104-
def load(self, data: str | Collection[str]) -> None: ...
105+
def load(self, data: str | SupportsItems[str, str | Morsel[Any]]) -> None: ...

0 commit comments

Comments
 (0)