Skip to content

Add default filterwarning configuration #696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 11, 2025
Merged

Add default filterwarning configuration #696

merged 9 commits into from
Jun 11, 2025

Conversation

ionelmc
Copy link
Member

@ionelmc ionelmc commented Jun 6, 2025

No description provided.

@ionelmc ionelmc changed the title Add reproducer for #693. Add default filterwarning configuration Jun 11, 2025
@ionelmc ionelmc merged commit f69166a into master Jun 11, 2025
180 of 182 checks passed
@ionelmc ionelmc deleted the issue-693 branch June 11, 2025 21:53
from . import compat
from . import embed

if TYPE_CHECKING:
from .engine import CovController

COVERAGE_SQLITE_WARNING_RE = re.compile('unclosed database in <sqlite3.Connection object at', re.I)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; with conditions:

NIT - the pattern string could have be abstracted for use later
Suggested change
COVERAGE_SQLITE_WARNING_RE = re.compile('unclosed database in <sqlite3.Connection object at', re.I)
COVERAGE_SQLITE_WARNING_STUB = 'unclosed database in <sqlite3.Connection object at'
COVERAGE_SQLITE_WARNING_RE = re.compile(COVERAGE_SQLITE_WARNING_STUB, re.I)

and later used to simplify:

        for _, message, category, _, _ in warnings.filters:
            if category is ResourceWarning and message == COVERAGE_SQLITE_WARNING_RE:
                break
        else:
            warnings.filterwarnings('default', 'unclosed database in <sqlite3.Connection object at', ResourceWarning)

with the patch:

          for _, message, category, _, _ in warnings.filters:
              if category is ResourceWarning and message == COVERAGE_SQLITE_WARNING_RE:
                  break
          else:
-             warnings.filterwarnings('default', 'unclosed database in <sqlite3.Connection object at', ResourceWarning)
+             warnings.filterwarnings('default', COVERAGE_SQLITE_WARNING_STUB, ResourceWarning)

yielding:

        for _, message, category, _, _ in warnings.filters:
            if category is ResourceWarning and message == COVERAGE_SQLITE_WARNING_RE:
                break
        else:
            warnings.filterwarnings('default', COVERAGE_SQLITE_WARNING_STUB, ResourceWarning)

if category is ResourceWarning and message == COVERAGE_SQLITE_WARNING_RE:
break
else:
warnings.filterwarnings('default', 'unclosed database in <sqlite3.Connection object at', ResourceWarning)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; with continued conditions:

NIT - Could be more DRY

As mentioned already, this string could be set with the RE variable so that only the base string need be ever updated 🤷

e.g.,

COVERAGE_SQLITE_WARNING_STUB = 'unclosed database in <sqlite3.Connection object at'
COVERAGE_SQLITE_WARNING_RE = re.compile(COVERAGE_SQLITE_WARNING_STUB, re.I)

and refactor this line to:

Suggested change
warnings.filterwarnings('default', 'unclosed database in <sqlite3.Connection object at', ResourceWarning)
warnings.filterwarnings('default', COVERAGE_SQLITE_WARNING_STUB, ResourceWarning)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants