|
13 | 13 | Mapping, |
14 | 14 | TypeVar, |
15 | 15 | Callable, |
| 16 | + Iterator, |
16 | 17 | Optional, |
17 | 18 | Sequence, |
18 | 19 | ) |
19 | | -from typing_extensions import Set, Literal, Protocol, TypeAlias, TypedDict, override, runtime_checkable |
| 20 | +from typing_extensions import ( |
| 21 | + Set, |
| 22 | + Literal, |
| 23 | + Protocol, |
| 24 | + TypeAlias, |
| 25 | + TypedDict, |
| 26 | + SupportsIndex, |
| 27 | + overload, |
| 28 | + override, |
| 29 | + runtime_checkable, |
| 30 | +) |
20 | 31 |
|
21 | 32 | import httpx |
22 | 33 | import pydantic |
@@ -217,3 +228,26 @@ class _GenericAlias(Protocol): |
217 | 228 | class HttpxSendArgs(TypedDict, total=False): |
218 | 229 | auth: httpx.Auth |
219 | 230 | follow_redirects: bool |
| 231 | + |
| 232 | + |
| 233 | +_T_co = TypeVar("_T_co", covariant=True) |
| 234 | + |
| 235 | + |
| 236 | +if TYPE_CHECKING: |
| 237 | + # This works because str.__contains__ does not accept object (either in typeshed or at runtime) |
| 238 | + # https://github.com/hauntsaninja/useful_types/blob/5e9710f3875107d068e7679fd7fec9cfab0eff3b/useful_types/__init__.py#L285 |
| 239 | + class SequenceNotStr(Protocol[_T_co]): |
| 240 | + @overload |
| 241 | + def __getitem__(self, index: SupportsIndex, /) -> _T_co: ... |
| 242 | + @overload |
| 243 | + def __getitem__(self, index: slice, /) -> Sequence[_T_co]: ... |
| 244 | + def __contains__(self, value: object, /) -> bool: ... |
| 245 | + def __len__(self) -> int: ... |
| 246 | + def __iter__(self) -> Iterator[_T_co]: ... |
| 247 | + def index(self, value: Any, start: int = 0, stop: int = ..., /) -> int: ... |
| 248 | + def count(self, value: Any, /) -> int: ... |
| 249 | + def __reversed__(self) -> Iterator[_T_co]: ... |
| 250 | +else: |
| 251 | + # just point this to a normal `Sequence` at runtime to avoid having to special case |
| 252 | + # deserializing our custom sequence type |
| 253 | + SequenceNotStr = Sequence |
0 commit comments