|
1 | 1 | import sys |
2 | | -from collections.abc import Callable, Iterable, Mapping, Sequence |
| 2 | +from collections.abc import Iterable, Mapping, Sequence |
3 | 3 | from types import GenericAlias |
4 | | -from typing import Any, AnyStr, Generic, Literal, NamedTuple, TypeVar, overload |
| 4 | +from typing import Any, AnyStr, Generic, Literal, NamedTuple, Protocol, overload, type_check_only |
5 | 5 | from typing_extensions import TypeAlias |
6 | 6 |
|
7 | 7 | __all__ = [ |
@@ -132,38 +132,32 @@ def urldefrag(url: str) -> DefragResult: ... |
132 | 132 | @overload |
133 | 133 | def urldefrag(url: bytes | bytearray | None) -> DefragResultBytes: ... |
134 | 134 |
|
135 | | -_Q = TypeVar("_Q", bound=str | Iterable[int]) |
| 135 | +# The values are passed through `str()` (unless they are bytes), so anything is valid. |
136 | 136 | _QueryType: TypeAlias = ( |
137 | | - Mapping[Any, Any] | Mapping[Any, Sequence[Any]] | Sequence[tuple[Any, Any]] | Sequence[tuple[Any, Sequence[Any]]] |
| 137 | + Mapping[str, object] |
| 138 | + | Mapping[bytes, object] |
| 139 | + | Mapping[str | bytes, object] |
| 140 | + | Mapping[str, Sequence[object]] |
| 141 | + | Mapping[bytes, Sequence[object]] |
| 142 | + | Mapping[str | bytes, Sequence[object]] |
| 143 | + | Sequence[tuple[str | bytes, object]] |
| 144 | + | Sequence[tuple[str | bytes, Sequence[object]]] |
138 | 145 | ) |
139 | 146 |
|
140 | | -@overload |
141 | | -def urlencode( |
142 | | - query: _QueryType, |
143 | | - doseq: bool = False, |
144 | | - safe: str = "", |
145 | | - encoding: str | None = None, |
146 | | - errors: str | None = None, |
147 | | - quote_via: Callable[[AnyStr, str, str, str], str] = ..., |
148 | | -) -> str: ... |
149 | | -@overload |
150 | | -def urlencode( |
151 | | - query: _QueryType, |
152 | | - doseq: bool, |
153 | | - safe: _Q, |
154 | | - encoding: str | None = None, |
155 | | - errors: str | None = None, |
156 | | - quote_via: Callable[[AnyStr, _Q, str, str], str] = ..., |
157 | | -) -> str: ... |
158 | | -@overload |
| 147 | +@type_check_only |
| 148 | +class _QuoteVia(Protocol): |
| 149 | + @overload |
| 150 | + def __call__(self, string: str, safe: str | bytes, encoding: str, errors: str, /) -> str: ... |
| 151 | + @overload |
| 152 | + def __call__(self, string: bytes, safe: str | bytes, /) -> str: ... |
| 153 | + |
159 | 154 | def urlencode( |
160 | 155 | query: _QueryType, |
161 | 156 | doseq: bool = False, |
162 | | - *, |
163 | | - safe: _Q, |
| 157 | + safe: str | bytes = "", |
164 | 158 | encoding: str | None = None, |
165 | 159 | errors: str | None = None, |
166 | | - quote_via: Callable[[AnyStr, _Q, str, str], str] = ..., |
| 160 | + quote_via: _QuoteVia = ..., |
167 | 161 | ) -> str: ... |
168 | 162 | def urljoin(base: AnyStr, url: AnyStr | None, allow_fragments: bool = True) -> AnyStr: ... |
169 | 163 | @overload |
|
0 commit comments