Skip to content

Commit a944954

Browse files
authored
chore: update Ruff to v0.10 (#1087)
There is a new rule [TC006](https://docs.astral.sh/ruff/rules/runtime-cast-value/).
1 parent 98c963d commit a944954

20 files changed

+67
-67
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ dev = [
107107
"pytest-xdist~=3.6.0",
108108
"pytest~=8.3.0",
109109
"respx~=0.22.0",
110-
"ruff~=0.9.0",
110+
"ruff~=0.10.0",
111111
"setuptools~=76.0.0", # setuptools are used by pytest, but not explicitly required
112112
"sortedcontainers-stubs~=2.4.0",
113113
"types-beautifulsoup4~=4.12.0.20240229",

src/crawlee/_autoscaling/snapshotter.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def get_memory_sample(self, duration: timedelta | None = None) -> list[Snapshot]
186186
Returns:
187187
A sample of memory snapshots.
188188
"""
189-
snapshots = cast(list[Snapshot], self._memory_snapshots)
189+
snapshots = cast('list[Snapshot]', self._memory_snapshots)
190190
return self._get_sample(snapshots, duration)
191191

192192
@ensure_context
@@ -199,7 +199,7 @@ def get_event_loop_sample(self, duration: timedelta | None = None) -> list[Snaps
199199
Returns:
200200
A sample of event loop snapshots.
201201
"""
202-
snapshots = cast(list[Snapshot], self._event_loop_snapshots)
202+
snapshots = cast('list[Snapshot]', self._event_loop_snapshots)
203203
return self._get_sample(snapshots, duration)
204204

205205
@ensure_context
@@ -212,7 +212,7 @@ def get_cpu_sample(self, duration: timedelta | None = None) -> list[Snapshot]:
212212
Returns:
213213
A sample of CPU snapshots.
214214
"""
215-
snapshots = cast(list[Snapshot], self._cpu_snapshots)
215+
snapshots = cast('list[Snapshot]', self._cpu_snapshots)
216216
return self._get_sample(snapshots, duration)
217217

218218
@ensure_context
@@ -225,7 +225,7 @@ def get_client_sample(self, duration: timedelta | None = None) -> list[Snapshot]
225225
Returns:
226226
A sample of client snapshots.
227227
"""
228-
snapshots = cast(list[Snapshot], self._client_snapshots)
228+
snapshots = cast('list[Snapshot]', self._client_snapshots)
229229
return self._get_sample(snapshots, duration)
230230

231231
@staticmethod
@@ -255,7 +255,7 @@ def _snapshot_cpu(self, event_data: EventSystemInfoData) -> None:
255255
created_at=event_data.cpu_info.created_at,
256256
)
257257

258-
snapshots = cast(list[Snapshot], self._cpu_snapshots)
258+
snapshots = cast('list[Snapshot]', self._cpu_snapshots)
259259
self._prune_snapshots(snapshots, event_data.cpu_info.created_at)
260260
self._cpu_snapshots.add(snapshot)
261261

@@ -275,7 +275,7 @@ def _snapshot_memory(self, event_data: EventSystemInfoData) -> None:
275275
created_at=event_data.memory_info.created_at,
276276
)
277277

278-
snapshots = cast(list[Snapshot], self._memory_snapshots)
278+
snapshots = cast('list[Snapshot]', self._memory_snapshots)
279279
self._prune_snapshots(snapshots, snapshot.created_at)
280280
self._memory_snapshots.add(snapshot)
281281
self._evaluate_memory_load(event_data.memory_info.current_size, event_data.memory_info.created_at)
@@ -295,7 +295,7 @@ def _snapshot_event_loop(self) -> None:
295295
event_loop_delay = snapshot.created_at - previous_snapshot.created_at - self._EVENT_LOOP_SNAPSHOT_INTERVAL
296296
snapshot.delay = event_loop_delay
297297

298-
snapshots = cast(list[Snapshot], self._event_loop_snapshots)
298+
snapshots = cast('list[Snapshot]', self._event_loop_snapshots)
299299
self._prune_snapshots(snapshots, snapshot.created_at)
300300
self._event_loop_snapshots.add(snapshot)
301301

@@ -312,7 +312,7 @@ def _snapshot_client(self) -> None:
312312
error_count = rate_limit_errors.get(self._CLIENT_RATE_LIMIT_ERROR_RETRY_COUNT, 0)
313313
snapshot = ClientSnapshot(error_count=error_count, max_error_count=self._max_client_errors)
314314

315-
snapshots = cast(list[Snapshot], self._client_snapshots)
315+
snapshots = cast('list[Snapshot]', self._client_snapshots)
316316
self._prune_snapshots(snapshots, snapshot.created_at)
317317
self._client_snapshots.add(snapshot)
318318

src/crawlee/_cli.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _prompt_for_project_name(initial_project_name: str | None) -> str:
7878

7979
def _prompt_text(message: str, default: str) -> str:
8080
return cast(
81-
str,
81+
'str',
8282
ConsoleRender().render(
8383
inquirer.Text(
8484
name='text',
@@ -93,7 +93,7 @@ def _prompt_text(message: str, default: str) -> str:
9393
def _prompt_choice(message: str, choices: list[str]) -> str:
9494
"""Prompt the user to pick one from a list of choices."""
9595
return cast(
96-
str,
96+
'str',
9797
ConsoleRender().render(
9898
inquirer.List(
9999
name='choice',
@@ -106,7 +106,7 @@ def _prompt_choice(message: str, choices: list[str]) -> str:
106106

107107
def _prompt_bool(message: str, *, default: bool) -> bool:
108108
return cast(
109-
bool,
109+
'bool',
110110
ConsoleRender().render(
111111
inquirer.Confirm(
112112
name='confirm',

src/crawlee/_request.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,12 @@ def get_query_param_from_url(self, param: str, *, default: str | None = None) ->
306306
@property
307307
def label(self) -> str | None:
308308
"""A string used to differentiate between arbitrary request types."""
309-
return cast(UserData, self.user_data).label
309+
return cast('UserData', self.user_data).label
310310

311311
@property
312312
def crawlee_data(self) -> CrawleeRequestData:
313313
"""Crawlee-specific configuration stored in the `user_data`."""
314-
user_data = cast(UserData, self.user_data)
314+
user_data = cast('UserData', self.user_data)
315315
if user_data.crawlee_data is None:
316316
user_data.crawlee_data = CrawleeRequestData()
317317

src/crawlee/_types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ async def get_value(self, key: str, default_value: T | None = None) -> T | None:
244244

245245
async def get_value(self, key: str, default_value: T | None = None) -> T | None:
246246
if key in self.updates:
247-
return cast(T, self.updates[key].content)
247+
return cast('T', self.updates[key].content)
248248

249249
return await self._actual_key_value_store.get_value(key, default_value)
250250

src/crawlee/_utils/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def _timedelta_to_ms(td: timedelta | None) -> float | None:
1414
return float('inf')
1515
if td is None:
1616
return td
17-
return int(round(td.total_seconds() * 1000))
17+
return round(td.total_seconds() * 1000)
1818

1919

2020
def _timedelta_to_secs(td: timedelta | None) -> float | None:

src/crawlee/browsers/_playwright_browser_controller.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
from crawlee._utils.docs import docs_group
1313
from crawlee.browsers._browser_controller import BrowserController
14-
from crawlee.browsers._types import BrowserType
1514
from crawlee.fingerprint_suite import HeaderGenerator
1615

1716
if TYPE_CHECKING:
1817
from collections.abc import Mapping
1918

2019
from crawlee.browsers._playwright_browser import PlaywrightPersistentBrowser
20+
from crawlee.browsers._types import BrowserType
2121
from crawlee.fingerprint_suite import FingerprintGenerator
2222
from crawlee.proxy_configuration import ProxyInfo
2323

@@ -107,7 +107,7 @@ def is_browser_connected(self) -> bool:
107107
@property
108108
@override
109109
def browser_type(self) -> BrowserType:
110-
return cast(BrowserType, self._browser.browser_type.name)
110+
return cast('BrowserType', self._browser.browser_type.name)
111111

112112
@override
113113
async def new_page(

src/crawlee/crawlers/_basic/_basic_crawler.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ def __init__(
303303

304304
# Request router setup
305305
self._router: Router[TCrawlingContext] | None = None
306-
if isinstance(cast(Router, request_handler), Router):
307-
self._router = cast(Router[TCrawlingContext], request_handler)
306+
if isinstance(cast('Router', request_handler), Router):
307+
self._router = cast('Router[TCrawlingContext]', request_handler)
308308
elif request_handler is not None:
309309
self._router = None
310310
self.router.default_handler(request_handler)
@@ -349,7 +349,7 @@ def __init__(
349349

350350
# Statistics
351351
self._statistics = statistics or cast(
352-
Statistics[TStatisticsState],
352+
'Statistics[TStatisticsState]',
353353
Statistics.with_default_state(
354354
periodic_message_logger=self._logger,
355355
log_message='Current request statistics:',
@@ -1090,7 +1090,7 @@ async def __run_task_function(self) -> None:
10901090

10911091
except RequestHandlerError as primary_error:
10921092
primary_error = cast(
1093-
RequestHandlerError[TCrawlingContext], primary_error
1093+
'RequestHandlerError[TCrawlingContext]', primary_error
10941094
) # valid thanks to ContextPipeline
10951095

10961096
self._logger.debug(

src/crawlee/crawlers/_basic/_context_pipeline.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async def __call__(
7676
cleanup_stack.append(middleware_instance)
7777

7878
try:
79-
await final_context_consumer(cast(TCrawlingContext, crawling_context))
79+
await final_context_consumer(cast('TCrawlingContext', crawling_context))
8080
except SessionError: # Session errors get special treatment
8181
raise
8282
except Exception as e:
@@ -111,7 +111,7 @@ def compose(
111111
"""
112112
return ContextPipeline[TMiddlewareCrawlingContext](
113113
_middleware=cast(
114-
Callable[[BasicCrawlingContext], AsyncGenerator[TMiddlewareCrawlingContext, None]], middleware
114+
'Callable[[BasicCrawlingContext], AsyncGenerator[TMiddlewareCrawlingContext, None]]', middleware
115115
),
116-
_parent=cast(ContextPipeline[BasicCrawlingContext], self),
116+
_parent=cast('ContextPipeline[BasicCrawlingContext]', self),
117117
)

src/crawlee/events/_event_manager.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import asyncio
66
import inspect
77
from collections import defaultdict
8-
from collections.abc import Awaitable, Callable
98
from datetime import timedelta
109
from functools import wraps
1110
from logging import getLogger
@@ -28,6 +27,7 @@
2827
)
2928

3029
if TYPE_CHECKING:
30+
from collections.abc import Awaitable, Callable
3131
from types import TracebackType
3232

3333
from typing_extensions import NotRequired
@@ -158,7 +158,7 @@ def on(self, *, event: Event, listener: EventListener[Any]) -> None:
158158
"""
159159
signature = inspect.signature(listener)
160160

161-
@wraps(cast(Callable[..., Union[None, Awaitable[None]]], listener))
161+
@wraps(cast('Callable[..., Union[None, Awaitable[None]]]', listener))
162162
async def listener_wrapper(event_data: EventData) -> None:
163163
try:
164164
bound_args = signature.bind(event_data)
@@ -170,7 +170,7 @@ async def listener_wrapper(event_data: EventData) -> None:
170170
coro = (
171171
listener(*bound_args.args, **bound_args.kwargs)
172172
if asyncio.iscoroutinefunction(listener)
173-
else asyncio.to_thread(cast(Callable[..., None], listener), *bound_args.args, **bound_args.kwargs)
173+
else asyncio.to_thread(cast('Callable[..., None]', listener), *bound_args.args, **bound_args.kwargs)
174174
)
175175
# Note: use `asyncio.iscoroutinefunction` rather then `inspect.iscoroutinefunction` since it works with
176176
# unittests.mock.AsyncMock. See https://github.com/python/cpython/issues/84753.

src/crawlee/http_clients/_httpx.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
from crawlee.errors import ProxyError
1313
from crawlee.fingerprint_suite import HeaderGenerator
1414
from crawlee.http_clients import HttpClient, HttpCrawlingResult, HttpResponse
15-
from crawlee.sessions import Session
1615

1716
if TYPE_CHECKING:
1817
from ssl import SSLContext
1918

2019
from crawlee import Request
2120
from crawlee._types import HttpMethod, HttpPayload
2221
from crawlee.proxy_configuration import ProxyInfo
22+
from crawlee.sessions import Session
2323
from crawlee.statistics import Statistics
2424

2525
logger = getLogger(__name__)
@@ -60,7 +60,7 @@ async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
6060
response = await super().handle_async_request(request)
6161
response.request = request
6262

63-
if session := cast(Session, request.extensions.get('crawlee_session')):
63+
if session := cast('Session', request.extensions.get('crawlee_session')):
6464
session.cookies.store_cookies(list(response.cookies.jar))
6565

6666
if 'Set-Cookie' in response.headers:

src/crawlee/statistics/_statistics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ async def _maybe_load_statistics(self) -> None:
290290
if not self._key_value_store:
291291
return
292292

293-
stored_state = await self._key_value_store.get_value(self._persist_state_key, cast(Any, {}))
293+
stored_state = await self._key_value_store.get_value(self._persist_state_key, cast('Any', {}))
294294

295295
saved_state = self.state.__class__.model_validate(stored_state)
296296
self.state = saved_state

src/crawlee/storages/_dataset.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ async def export_to(self, **kwargs: Unpack[ExportToKwargs]) -> None:
367367
Args:
368368
kwargs: Keyword arguments for the storage client method.
369369
"""
370-
key = cast(str, kwargs.get('key'))
370+
key = cast('str', kwargs.get('key'))
371371
content_type = kwargs.get('content_type', 'json')
372372
to_key_value_store_id = kwargs.get('to_key_value_store_id')
373373
to_key_value_store_name = kwargs.get('to_key_value_store_name')

tests/unit/_autoscaling/test_autoscaled_pool.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def get_historical_system_info() -> SystemInfo:
170170

171171
return result
172172

173-
cast(Mock, system_status.get_historical_system_info).side_effect = get_historical_system_info
173+
cast('Mock', system_status.get_historical_system_info).side_effect = get_historical_system_info
174174

175175
# Override AP class attributes using monkeypatch.
176176
monkeypatch.setattr(AutoscaledPool, '_AUTOSCALE_INTERVAL', timedelta(seconds=0.1))
@@ -244,7 +244,7 @@ def get_historical_system_info() -> SystemInfo:
244244
client_info=LoadRatioInfo(limit_ratio=0.9, actual_ratio=0.3),
245245
)
246246

247-
cast(Mock, system_status.get_historical_system_info).side_effect = get_historical_system_info
247+
cast('Mock', system_status.get_historical_system_info).side_effect = get_historical_system_info
248248

249249
# Override AP class attributes using monkeypatch.
250250
monkeypatch.setattr(AutoscaledPool, '_AUTOSCALE_INTERVAL', timedelta(seconds=0.1))

tests/unit/_autoscaling/test_snapshotter.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def test_snapshot_pruning_removes_outdated_records(snapshotter: Snapshotter) ->
148148
snapshotter._cpu_snapshots = snapshots
149149

150150
# Prune snapshots older than 2 hours
151-
snapshots_casted = cast(list[Snapshot], snapshotter._cpu_snapshots)
151+
snapshots_casted = cast('list[Snapshot]', snapshotter._cpu_snapshots)
152152
snapshotter._prune_snapshots(snapshots_casted, now)
153153

154154
# Check that only the last two snapshots remain
@@ -160,7 +160,7 @@ def test_snapshot_pruning_removes_outdated_records(snapshotter: Snapshotter) ->
160160
def test_pruning_empty_snapshot_list_remains_empty(snapshotter: Snapshotter) -> None:
161161
now = datetime.now(timezone.utc)
162162
snapshotter._cpu_snapshots = Snapshotter._get_sorted_list_by_created_at(list[CpuSnapshot]())
163-
snapshots_casted = cast(list[Snapshot], snapshotter._cpu_snapshots)
163+
snapshots_casted = cast('list[Snapshot]', snapshotter._cpu_snapshots)
164164
snapshotter._prune_snapshots(snapshots_casted, now)
165165
assert snapshotter._cpu_snapshots == []
166166

@@ -184,7 +184,7 @@ def test_snapshot_pruning_keeps_recent_records_unaffected(snapshotter: Snapshott
184184
snapshotter._cpu_snapshots = snapshots
185185

186186
# Prune snapshots older than 2 hours
187-
snapshots_casted = cast(list[Snapshot], snapshotter._cpu_snapshots)
187+
snapshots_casted = cast('list[Snapshot]', snapshotter._cpu_snapshots)
188188
snapshotter._prune_snapshots(snapshots_casted, now)
189189

190190
# Check that only the last two snapshots remain

tests/unit/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _isolate_test_environment(prepare_test_env: Callable[[], None]) -> None:
9191
def _set_crawler_log_level(pytestconfig: pytest.Config, monkeypatch: pytest.MonkeyPatch) -> None:
9292
from crawlee import _log_config
9393

94-
loglevel = cast(Optional[str], pytestconfig.getoption('--log-level'))
94+
loglevel = cast('Optional[str]', pytestconfig.getoption('--log-level'))
9595
if loglevel is not None:
9696
monkeypatch.setattr(_log_config, 'get_configured_log_level', lambda: getattr(logging, loglevel.upper()))
9797

@@ -128,7 +128,7 @@ def __truediv__(self, other: Any) -> URLWrapper:
128128
def __str__(self) -> str:
129129
return str(self.url)
130130

131-
return cast(URL, URLWrapper(URL(os.environ.get('HTTPBIN_URL', 'https://httpbin.org'))))
131+
return cast('URL', URLWrapper(URL(os.environ.get('HTTPBIN_URL', 'https://httpbin.org'))))
132132

133133

134134
@pytest.fixture

tests/unit/crawlers/_adaptive_playwright/test_adaptive_playwright_crawler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ async def test_adaptive_crawling_result_use_state_isolation(
410410
@crawler.router.default_handler
411411
async def request_handler(context: AdaptivePlaywrightCrawlingContext) -> None:
412412
nonlocal request_handler_calls
413-
state = cast(dict[str, int], await context.use_state())
413+
state = cast('dict[str, int]', await context.use_state())
414414
request_handler_calls += 1
415415
state['counter'] += 1
416416

0 commit comments

Comments
 (0)