Skip to content

Commit

Permalink
Capture python warnings and report them as matches
Browse files Browse the repository at this point in the history
This change introduces a new way to generate warnings in linter, one
that uses the warnings support in Python. The benefit is that this
allows use to generate warnings from anywhere inside the code without
having to pass them from function to function.

For start we will be using this for internal rules, like ability to
report outdated noqa comments.
  • Loading branch information
ssbarnea committed Apr 22, 2023
1 parent 93887cb commit 8bb3c05
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import multiprocessing
import multiprocessing.pool
import os
import warnings
from collections.abc import Generator
from dataclasses import dataclass
from fnmatch import fnmatch
Expand Down Expand Up @@ -113,6 +114,27 @@ def is_excluded(self, lintable: Lintable) -> bool:

def run(self) -> list[MatchError]: # noqa: C901
"""Execute the linting process."""
matches: list[MatchError] = []
with warnings.catch_warnings(record=True) as captured_warnings:
warnings.simplefilter("always")
matches = self._run()
for warn in captured_warnings:
# For the moment we are ignoring deprecation warnings as Ansible
# modules outside current content can generate them and user
# might not be able to do anything about them.
if warn.category is DeprecationWarning:
continue
_logger.warning(
"%s:%s %s %s",
warn.filename,
warn.lineno or 1,
warn.category.__name__,
warn.message,
)
return matches

def _run(self) -> list[MatchError]: # noqa: C901
"""Internal implementation of run."""
files: list[Lintable] = []
matches: list[MatchError] = []

Expand Down

0 comments on commit 8bb3c05

Please sign in to comment.