Skip to content

Commit 2032a76

Browse files
chore(internal): add Sequence related utils
1 parent da8cdc0 commit 2032a76

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

src/knockapi/_types.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@
1313
Mapping,
1414
TypeVar,
1515
Callable,
16+
Iterator,
1617
Optional,
1718
Sequence,
1819
)
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+
)
2031

2132
import httpx
2233
import pydantic
@@ -217,3 +228,26 @@ class _GenericAlias(Protocol):
217228
class HttpxSendArgs(TypedDict, total=False):
218229
auth: httpx.Auth
219230
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

src/knockapi/_utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
extract_type_arg as extract_type_arg,
3939
is_iterable_type as is_iterable_type,
4040
is_required_type as is_required_type,
41+
is_sequence_type as is_sequence_type,
4142
is_annotated_type as is_annotated_type,
4243
is_type_alias_type as is_type_alias_type,
4344
strip_annotated_type as strip_annotated_type,

src/knockapi/_utils/_typing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ def is_list_type(typ: type) -> bool:
2626
return (get_origin(typ) or typ) == list
2727

2828

29+
def is_sequence_type(typ: type) -> bool:
30+
origin = get_origin(typ) or typ
31+
return origin == typing_extensions.Sequence or origin == typing.Sequence or origin == _c_abc.Sequence
32+
33+
2934
def is_iterable_type(typ: type) -> bool:
3035
"""If the given type is `typing.Iterable[T]`"""
3136
origin = get_origin(typ) or typ

tests/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import inspect
55
import traceback
66
import contextlib
7-
from typing import Any, TypeVar, Iterator, cast
7+
from typing import Any, TypeVar, Iterator, Sequence, cast
88
from datetime import date, datetime
99
from typing_extensions import Literal, get_args, get_origin, assert_type
1010

@@ -15,6 +15,7 @@
1515
is_list_type,
1616
is_union_type,
1717
extract_type_arg,
18+
is_sequence_type,
1819
is_annotated_type,
1920
is_type_alias_type,
2021
)
@@ -71,6 +72,13 @@ def assert_matches_type(
7172
if is_list_type(type_):
7273
return _assert_list_type(type_, value)
7374

75+
if is_sequence_type(type_):
76+
assert isinstance(value, Sequence)
77+
inner_type = get_args(type_)[0]
78+
for entry in value: # type: ignore
79+
assert_type(inner_type, entry) # type: ignore
80+
return
81+
7482
if origin == str:
7583
assert isinstance(value, str)
7684
elif origin == int:

0 commit comments

Comments
 (0)