Skip to content

Commit 60e39a3

Browse files
authored
chore: Reduce the number of warnings in tests (#1594)
### Description - Reduce the number of warnings in tests
1 parent 33d0db3 commit 60e39a3

File tree

7 files changed

+30
-16
lines changed

7 files changed

+30
-16
lines changed

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@ timeout = 300
222222
markers = [
223223
"run_alone: marks tests that must run in isolation",
224224
]
225+
# Ignore DeprecationWarnings coming from Uvicorn's internal imports. Uvicorn relies on deprecated
226+
# modules from `websockets`, which triggers warnings during tests. These are safe to ignore until
227+
# Uvicorn updates its internals.
228+
filterwarnings = [
229+
"ignore:websockets.legacy is deprecated:DeprecationWarning",
230+
"ignore:websockets.server.WebSocketServerProtocol is deprecated:DeprecationWarning",
231+
]
225232

226233
[tool.mypy]
227234
python_version = "3.10"

src/crawlee/_utils/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
import asyncio
3+
import inspect
44
from collections.abc import Callable
55
from functools import wraps
66
from typing import Any, TypeVar
@@ -44,4 +44,4 @@ async def async_wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
4444

4545
return await method(self, *args, **kwargs)
4646

47-
return async_wrapper if asyncio.iscoroutinefunction(method) else sync_wrapper # type: ignore[return-value]
47+
return async_wrapper if inspect.iscoroutinefunction(method) else sync_wrapper # type: ignore[return-value]

src/crawlee/_utils/recurring_task.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import inspect
45
from logging import getLogger
56
from typing import TYPE_CHECKING
67

@@ -49,7 +50,7 @@ async def _wrapper(self) -> None:
4950
"""
5051
sleep_time_secs = self.delay.total_seconds()
5152
while True:
52-
await self.func() if asyncio.iscoroutinefunction(self.func) else self.func()
53+
await self.func() if inspect.iscoroutinefunction(self.func) else self.func()
5354
await asyncio.sleep(sleep_time_secs)
5455

5556
def start(self) -> None:

src/crawlee/events/_event_manager.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,9 @@ async def listener_wrapper(event_data: EventData) -> None:
174174
# to avoid blocking the event loop
175175
coro = (
176176
listener(*bound_args.args, **bound_args.kwargs)
177-
if asyncio.iscoroutinefunction(listener)
177+
if inspect.iscoroutinefunction(listener)
178178
else asyncio.to_thread(cast('Callable[..., None]', listener), *bound_args.args, **bound_args.kwargs)
179179
)
180-
# Note: use `asyncio.iscoroutinefunction` rather then `inspect.iscoroutinefunction` since it works with
181-
# unittests.mock.AsyncMock. See https://github.com/python/cpython/issues/84753.
182180

183181
listener_task = asyncio.create_task(coro, name=f'Task-{event.value}-{listener.__name__}')
184182
self._listener_tasks.add(listener_task)

tests/unit/browsers/test_playwright_browser_controller.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from unittest.mock import AsyncMock
77

88
import pytest
9-
from playwright.async_api import Browser, Playwright, async_playwright
9+
from playwright.async_api import Browser, BrowserContext, Page, Playwright, async_playwright
1010

1111
from crawlee.browsers import PlaywrightBrowserController, PlaywrightPersistentBrowser
1212

@@ -115,6 +115,10 @@ async def test_memory_leak_on_concurrent_context_creation() -> None:
115115
# Prepare mocked browser with relevant methods and attributes
116116
mocked_browser = AsyncMock()
117117
mocked_context_launcher = AsyncMock()
118+
mocked_context = AsyncMock(spec=BrowserContext)
119+
120+
mocked_context_launcher.return_value = mocked_context
121+
mocked_context.new_page.return_value = AsyncMock(spec=Page)
118122

119123
async def delayed_launch_persistent_context(*args: Any, **kwargs: Any) -> Any:
120124
"""Ensure that both calls to create context overlap in time."""

tests/unit/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def redirect_http_server(unused_tcp_port_factory: Callable[[], int]) -> Iterator
185185
timeout_graceful_shutdown=10,
186186
log_level='error',
187187
access_log=False,
188+
ws='websockets-sansio',
188189
)
189190
server = TestServer(config=config)
190191
yield from serve_in_thread(server)

tests/unit/storages/test_key_value_store.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,25 +1095,28 @@ async def test_validate_name(storage_client: StorageClient, name: str, *, is_val
10951095

10961096

10971097
@pytest.mark.parametrize(
1098-
'tested_storage_client',
1098+
'tested_storage_client_class',
10991099
[
1100-
pytest.param(MemoryStorageClient(), id='tested=MemoryStorageClient'),
1101-
pytest.param(FileSystemStorageClient(), id='tested=FileSystemStorageClient'),
1102-
pytest.param(SqlStorageClient(), id='tested=SqlStorageClient'),
1100+
pytest.param(MemoryStorageClient, id='tested=MemoryStorageClient'),
1101+
pytest.param(FileSystemStorageClient, id='tested=FileSystemStorageClient'),
1102+
pytest.param(SqlStorageClient, id='tested=SqlStorageClient'),
11031103
],
11041104
)
11051105
@pytest.mark.parametrize(
1106-
'global_storage_client',
1106+
'global_storage_client_class',
11071107
[
1108-
pytest.param(MemoryStorageClient(), id='global=MemoryStorageClient'),
1109-
pytest.param(FileSystemStorageClient(), id='global=FileSystemStorageClient'),
1110-
pytest.param(SqlStorageClient(), id='global=SqlStorageClient'),
1108+
pytest.param(MemoryStorageClient, id='global=MemoryStorageClient'),
1109+
pytest.param(FileSystemStorageClient, id='global=FileSystemStorageClient'),
1110+
pytest.param(SqlStorageClient, id='global=SqlStorageClient'),
11111111
],
11121112
)
11131113
async def test_get_auto_saved_value_various_global_clients(
1114-
tmp_path: Path, tested_storage_client: StorageClient, global_storage_client: StorageClient
1114+
tmp_path: Path, tested_storage_client_class: type[StorageClient], global_storage_client_class: type[StorageClient]
11151115
) -> None:
11161116
"""Ensure that persistence is working for all clients regardless of what is set in service locator."""
1117+
tested_storage_client = tested_storage_client_class()
1118+
global_storage_client = global_storage_client_class()
1119+
11171120
service_locator.set_configuration(
11181121
Configuration(
11191122
storage_dir=str(tmp_path),

0 commit comments

Comments
 (0)