Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
Changelog
=========

* Improve handling of ResourceWarning from sqlite3.

The plugin adds warning filter for sqlite3 ``ResourceWarning`` unclosed database (since 6.2.0).
It checks if there is already existing plugin for this message by comparing filter regular expression.
When filter is specified on command line the message is escaped and does not match an expected message.
A check for an escaped regular expression is added to handle this case.

With this fix one can suppress ``ResourceWarning`` from sqlite3 from command line::

pytest -W "ignore:unclosed database in <sqlite3.Connection object at:ResourceWarning" ...

7.0.0 (2025-09-09)
------------------

Expand Down
5 changes: 4 additions & 1 deletion src/pytest_cov/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
if TYPE_CHECKING:
from .engine import CovController

# The message is unescaped if it comes from configuration file, and escaped if
# it comes from command line option (-W) or PYTHONWARNINGS envvar.
COVERAGE_SQLITE_WARNING_RE = re.compile('unclosed database in <sqlite3.Connection object at', re.I)
COVERAGE_SQLITE_WARNING_RE2 = re.compile('unclosed\\ database\\ in\\ <sqlite3\\.Connection\\ object\\ at', re.I)


def validate_report(arg):
Expand Down Expand Up @@ -325,7 +328,7 @@ def pytest_runtestloop(self, session):

# we add default warning configuration to prevent certain warnings to bubble up as errors due to rigid filterwarnings configuration
for _, message, category, _, _ in warnings.filters:
if category is ResourceWarning and message == COVERAGE_SQLITE_WARNING_RE:
if category is ResourceWarning and message in (COVERAGE_SQLITE_WARNING_RE, COVERAGE_SQLITE_WARNING_RE2):
break
else:
warnings.filterwarnings('default', 'unclosed database in <sqlite3.Connection object at', ResourceWarning)
Expand Down
Loading