Skip to content

Commit 21f1420

Browse files
committed
refactor: remove redundant casts
1 parent 1a3d078 commit 21f1420

6 files changed

Lines changed: 14 additions & 43 deletions

File tree

src/faceit/api/pagination.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,9 @@ def _deduplicate_collection(
490490
collection: typing.Union[ItemPage[_T], typing.List[RawAPIItem]],
491491
/,
492492
) -> typing.Union[ItemPage[_T], typing.List[RawAPIItem]]:
493-
unique_items = deduplicate_unhashable(collection)
494-
if isinstance(collection, ItemPage):
495-
return collection.with_items(typing.cast("typing.List[_T]", unique_items))
496-
return typing.cast("typing.List[RawAPIItem]", unique_items)
493+
if not isinstance(collection, ItemPage):
494+
return deduplicate_unhashable(collection)
495+
return collection.with_items(deduplicate_unhashable(collection))
497496

498497
@classmethod
499498
def _create_unix_timestamp_iterator(

src/faceit/models/item_page.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def _construct_without_metadata(
158158
cls, items: typing.Optional[typing.Iterable[_R]] = None, /
159159
) -> ItemPage[_R]:
160160
# fmt: off
161-
return ItemPage[_R](
161+
return cls.model_construct( # type: ignore[return-value]
162162
items=tuple(items or ()),
163163
offset=None, limit=None,
164164
time_from=None, time_to=None,

src/faceit/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def create_uuid_validator(
195195
error_message: typing.Optional[str] = None,
196196
) -> typing.Callable[[typing.Any], str]:
197197
if error_message is None:
198-
error_message = f"Invalid {arg_name}: {{value}}. Expected a valid UUID."
198+
error_message = "Invalid {arg_name}: {value}. Expected a valid UUID."
199199

200200
def validator(value: typing.Any, /) -> str:
201201
if is_valid_uuid(value):

tests/test_pagination.py

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from faceit.models.custom_types import TimestampMs
2323

2424
if typing.TYPE_CHECKING:
25-
from faceit.types import RawAPIItem, RawAPIPageResponse
25+
from faceit.types import RawAPIPageResponse
2626

2727

2828
class _DummyResource(
@@ -41,9 +41,7 @@ def raw_method(
4141
offset: int = Field(0, ge=0),
4242
limit: int = Field(2, ge=1, le=2),
4343
) -> RawAPIPageResponse:
44-
items = typing.cast(
45-
"typing.List[RawAPIItem]", self._items[offset : offset + limit]
46-
)
44+
items = self._items[offset : offset + limit]
4745
return {"items": items, "start": offset, "end": offset + limit}
4846

4947
async def async_raw_method(
@@ -67,9 +65,7 @@ def raw_method_with_unix(
6765
if to is None
6866
else [item for item in self._items if item["finished_at"] < to]
6967
)
70-
items = typing.cast(
71-
"typing.List[RawAPIItem]", filtered[offset : offset + limit]
72-
)
68+
items = filtered[offset : offset + limit]
7369
return {"items": items, "start": offset, "end": offset + limit}
7470

7571
async def async_raw_method_with_unix(
@@ -108,22 +104,8 @@ def dummy_resource(
108104
@pytest.fixture
109105
def raw_pages() -> typing.Tuple[RawAPIPageResponse, RawAPIPageResponse]:
110106
return (
111-
{
112-
"items": [
113-
typing.cast("RawAPIItem", {"id": "a"}),
114-
typing.cast("RawAPIItem", {"id": "b"}),
115-
],
116-
"start": 0,
117-
"end": 2,
118-
},
119-
{
120-
"items": [
121-
typing.cast("RawAPIItem", {"id": "b"}),
122-
typing.cast("RawAPIItem", {"id": "c"}),
123-
],
124-
"start": 2,
125-
"end": 4,
126-
},
107+
{"items": [{"id": "a"}, {"id": "b"}], "start": 0, "end": 2},
108+
{"items": [{"id": "b"}, {"id": "c"}], "start": 2, "end": 4},
127109
)
128110

129111

@@ -177,10 +159,8 @@ def test_extract_unix_timestamp_from_raw_page() -> None:
177159
second_item_timestamp = 200
178160
page: RawAPIPageResponse = {
179161
"items": [
180-
typing.cast("RawAPIItem", {"stats": {"Match Finished At": 100}}),
181-
typing.cast(
182-
"RawAPIItem", {"stats": {"Match Finished At": second_item_timestamp}}
183-
),
162+
{"stats": {"Match Finished At": 100}},
163+
{"stats": {"Match Finished At": second_item_timestamp}},
184164
],
185165
"start": 0,
186166
"end": 2,
@@ -280,16 +260,8 @@ def test_sync_iterator_collect_respects_safe_max_items(
280260

281261
async def test_async_gather_from_iterator_raw() -> None:
282262
async def source() -> typing.AsyncIterator[RawAPIPageResponse]: # noqa: RUF029
283-
yield {
284-
"items": [typing.cast("RawAPIItem", {"id": 1})],
285-
"start": 0,
286-
"end": 1,
287-
}
288-
yield {
289-
"items": [typing.cast("RawAPIItem", {"id": 2})],
290-
"start": 1,
291-
"end": 2,
292-
}
263+
yield {"items": [{"id": 1}], "start": 0, "end": 1}
264+
yield {"items": [{"id": 2}], "start": 1, "end": 2}
293265

294266
result = await AsyncPageIterator.gather_from_iterator(
295267
source(),

0 commit comments

Comments
 (0)