Skip to content

Commit 3946b1c

Browse files
cfsmp3claude
authored andcommitted
fix: suppress AsyncMock RuntimeWarnings in Python 3.13+
In Python 3.13+, MagicMock auto-detects async-like method names (commit, debug, warning, etc.) and returns AsyncMock objects. This causes RuntimeWarnings when these methods are called synchronously. This commit adds: - A warning filter to suppress "coroutine was never awaited" warnings - A setup_mock_g() helper for tests that need explicit MagicMock setup - Updated create_mock_db_query() to also set up g.log 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 90f38d6 commit 3946b1c

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

tests/base.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Contains base test case with needed setup and helpers."""
22

33
import os
4+
import warnings
45
from collections import namedtuple
56
from contextlib import contextmanager
67
from unittest import mock
@@ -22,6 +23,15 @@
2223
TestResult, TestResultFile, TestStatus, TestType)
2324
from mod_upload.models import Platform, Upload
2425

26+
# Filter RuntimeWarning about coroutines from AsyncMock in Python 3.13+
27+
# This occurs when MagicMock auto-detects async-like method names (commit, debug, etc.)
28+
# Must be set before test execution begins.
29+
warnings.filterwarnings(
30+
"ignore",
31+
message="coroutine .* was never awaited",
32+
category=RuntimeWarning
33+
)
34+
2535

2636
@contextmanager
2737
def provide_file_at_root(file_name, to_write=None, to_delete=True):
@@ -47,6 +57,26 @@ def empty_github_token():
4757
g.github['bot_token'] = original
4858

4959

60+
def setup_mock_g(mock_g):
61+
"""
62+
Set up mock_g with explicit MagicMock objects to avoid AsyncMock warnings.
63+
64+
In Python 3.13+, MagicMock returns AsyncMock for method calls that look
65+
async-like (commit, debug, warning, etc.). This causes RuntimeWarnings
66+
when the code calls these methods synchronously.
67+
68+
This helper sets up explicit MagicMock objects for g.db and g.log to
69+
prevent these warnings.
70+
71+
:param mock_g: The mocked g object from @mock.patch
72+
:return: mock_g for chaining
73+
"""
74+
from unittest.mock import MagicMock
75+
mock_g.db = MagicMock()
76+
mock_g.log = MagicMock()
77+
return mock_g
78+
79+
5080
def create_mock_db_query(mock_g, extra_setup=None):
5181
"""
5282
Create a MagicMock for mock_g.db with common query chain setup.
@@ -60,6 +90,7 @@ def create_mock_db_query(mock_g, extra_setup=None):
6090
"""
6191
from unittest.mock import MagicMock
6292
mock_g.db = MagicMock()
93+
mock_g.log = MagicMock() # Also set up log to prevent AsyncMock warnings
6394
mock_query = MagicMock()
6495
mock_g.db.query.return_value = mock_query
6596
mock_query.filter.return_value = mock_query

0 commit comments

Comments
 (0)