forked from PyCQA/pylint-pytest
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
the current implementation provokes recursion errors because of the VariablesChecker.add_message patch not working properly. This rework fix the issue by replacing the original variable checker by a subclass, without patch.
- Loading branch information
Anis Da Silva Campos
committed
Dec 23, 2023
1 parent
b243816
commit 128e502
Showing
7 changed files
with
142 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,22 @@ | ||
import glob | ||
import importlib | ||
import inspect | ||
import os | ||
from pylint.checkers.variables import VariablesChecker | ||
from pylint.lint import PyLinter | ||
|
||
from .checkers import BasePytestChecker | ||
from .checkers.class_attr_loader import ClassAttrLoader | ||
from .checkers.fixture import FixtureChecker | ||
from .checkers.variables import CustomVariablesChecker | ||
|
||
|
||
def register(linter): | ||
"""auto discover pylint checker classes""" | ||
dirname = os.path.dirname(__file__) | ||
for module in glob.glob(os.path.join(dirname, "checkers", "*.py")): | ||
# trim file extension | ||
module = os.path.splitext(module)[0] | ||
def register(linter: PyLinter) -> None: | ||
"""Register the checker classes""" | ||
remove_original_variables_checker(linter) | ||
linter.register_checker(CustomVariablesChecker(linter)) | ||
linter.register_checker(FixtureChecker(linter)) | ||
linter.register_checker(ClassAttrLoader(linter)) | ||
|
||
# use relative path only | ||
module = module.replace(dirname, "", 1) | ||
|
||
# translate file path into module import path | ||
module = module.replace(os.sep, ".") | ||
|
||
checker = importlib.import_module(module, package=os.path.basename(dirname)) | ||
for attr_name in dir(checker): | ||
attr_val = getattr(checker, attr_name) | ||
if ( | ||
attr_val != BasePytestChecker | ||
and inspect.isclass(attr_val) | ||
and issubclass(attr_val, BasePytestChecker) | ||
): | ||
linter.register_checker(attr_val(linter)) | ||
def remove_original_variables_checker(linter: PyLinter) -> None: | ||
"""We need to remove VariablesChecker before registering CustomVariablesChecker""" | ||
variable_checkers = linter._checkers[VariablesChecker.name] # pylint: disable=protected-access | ||
for checker in variable_checkers: | ||
if isinstance(checker, VariablesChecker): | ||
variable_checkers.remove(checker) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from typing import Any, Optional | ||
|
||
from astroid import Arguments, Module | ||
from astroid.nodes.node_ng import NodeNG | ||
from pylint.checkers.variables import VariablesChecker | ||
from pylint.interfaces import Confidence | ||
|
||
from pylint_pytest.utils import _can_use_fixture, _is_same_module | ||
|
||
from .fixture import FixtureChecker | ||
|
||
|
||
class CustomVariablesChecker(VariablesChecker): | ||
"""Overrides the default VariablesChecker of pylint to discard unwanted warning messages""" | ||
|
||
def add_message( | ||
self, | ||
msgid: str, | ||
line: Optional[int] = None, | ||
node: Optional[NodeNG] = None, | ||
args: Any = None, | ||
confidence: Confidence = None, | ||
col_offset: Optional[int] = None, | ||
end_lineno: Optional[int] = None, | ||
end_col_offset: Optional[int] = None, | ||
) -> None: | ||
""" | ||
- intercept and discard unwanted warning messages | ||
""" | ||
# check W0611 unused-import | ||
if msgid == "unused-import": | ||
# actual attribute name is not passed as arg so...dirty hack | ||
# message is usually in the form of '%s imported from %s (as %)' | ||
message_tokens = args.split() | ||
fixture_name = message_tokens[0] | ||
|
||
# ignoring 'import %s' message | ||
if message_tokens[0] == "import" and len(message_tokens) == 2: | ||
pass | ||
|
||
# fixture is defined in other modules and being imported to | ||
# conftest for pytest magic | ||
elif ( | ||
node | ||
and isinstance(node.parent, Module) | ||
and node.parent.name.split(".")[-1] == "conftest" | ||
and fixture_name in FixtureChecker.pytest_fixtures | ||
): | ||
return | ||
|
||
# imported fixture is referenced in test/fixture func | ||
elif ( | ||
fixture_name in FixtureChecker.invoked_with_func_args | ||
and fixture_name in FixtureChecker.pytest_fixtures | ||
): | ||
if _is_same_module( | ||
fixtures=FixtureChecker.pytest_fixtures, | ||
import_node=node, | ||
fixture_name=fixture_name, | ||
): | ||
return | ||
|
||
# fixture is referenced in @pytest.mark.usefixtures | ||
elif ( | ||
fixture_name in FixtureChecker.invoked_with_usefixtures | ||
and fixture_name in FixtureChecker.pytest_fixtures | ||
): | ||
if _is_same_module( | ||
fixtures=FixtureChecker.pytest_fixtures, | ||
import_node=node, | ||
fixture_name=fixture_name, | ||
): | ||
return | ||
|
||
# check W0613 unused-argument | ||
if ( | ||
msgid == "unused-argument" | ||
and node | ||
and _can_use_fixture(node.parent.parent) | ||
and isinstance(node.parent, Arguments) | ||
): | ||
if node.name in FixtureChecker.pytest_fixtures: | ||
# argument is used as a fixture | ||
return | ||
|
||
fixnames = ( | ||
arg.name for arg in node.parent.args if arg.name in FixtureChecker.pytest_fixtures | ||
) | ||
for fixname in fixnames: | ||
if node.name in FixtureChecker.pytest_fixtures[fixname][0].argnames: | ||
# argument is used by a fixture | ||
return | ||
|
||
# check W0621 redefined-outer-name | ||
if ( | ||
msgid == "redefined-outer-name" | ||
and node | ||
and _can_use_fixture(node.parent.parent) | ||
and isinstance(node.parent, Arguments) | ||
and node.name in FixtureChecker.pytest_fixtures | ||
): | ||
return | ||
|
||
super().add_message(msgid, line, node, args, confidence, col_offset) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters