Skip to content

Commit

Permalink
Implement a checker to validate that q uses SearchFIlter
Browse files Browse the repository at this point in the history
  • Loading branch information
smk4664 committed Dec 31, 2024
1 parent f96de5c commit d7a3033
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pylint_nautobot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .deprecated_status_model import NautobotDeprecatedStatusModelChecker
from .incorrect_base_class import NautobotIncorrectBaseClassChecker
from .model_label import NautobotModelLabelChecker
from .q_search_filter import NautobotUseSearchFilterChecker
from .replaced_models import NautobotReplacedModelsImportChecker
from .string_field_blank_null import NautobotStringFieldBlankNull
from .sub_class_name import NautobotSubClassNameChecker
Expand All @@ -22,6 +23,7 @@
NautobotIncorrectBaseClassChecker,
NautobotModelLabelChecker,
NautobotReplacedModelsImportChecker,
NautobotUseSearchFilterChecker,
NautobotStringFieldBlankNull,
NautobotSubClassNameChecker,
NautobotUseFieldsAllChecker,
Expand Down
48 changes: 48 additions & 0 deletions pylint_nautobot/q_search_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Check for use of SearchFilter on NautobotFilterSets instead of custom `q` search functions."""

from astroid import Assign, AssignName, ClassDef
from pylint.checkers import BaseChecker

from .utils import is_version_compatible

_META_CLASSES = {
"nautobot.extras.filters.NautobotFilterSet": ">1",
"nautobot.apps.filters.NautobotFilterSet": ">1",
}


class NautobotUseSearchFilterChecker(BaseChecker):
"""Visit NautobotFilterSet subclasses and check for use of `q = SearchFilter`, instead of `q = django_filters.CharField`."""

version_specifier = ">=2,<3"

name = "nautobot-use-search-filter"
msgs = {
"C4272": (
"Use `q = SearchFilter` instead of django_filters.CharField and the custom `search` function.",
"nb-use-search-filter",
"Nautobot provides a `SearchFilter` class that uses MappedPredicates to provide a more flexible search experience. "
"This should be used in place of `django_filters.CharFilter` and no longer requires the custom `search` function.",
),
}

def __init__(self, *args, **kwargs):
"""Initialize the checker."""
super().__init__(*args, **kwargs)

self.meta_classes = [
key for key, specifier_set in _META_CLASSES.items() if is_version_compatible(specifier_set)
]

def visit_classdef(self, node: ClassDef):
"""Visit class definitions."""
if not any(ancestor.qname() in self.meta_classes for ancestor in node.ancestors()):
return

for child_node in node.get_children():
if isinstance(child_node, Assign):
if any(isinstance(target, AssignName) and target.name == "q" for target in child_node.targets):
# because they can be ast Name or Attribute nodes
child_node_name = child_node.value.func.as_string().split(".")[-1]
if child_node_name != "SearchFilter":
self.add_message("nb-use-search-filter", node=child_node)

0 comments on commit d7a3033

Please sign in to comment.