Skip to content

Commit

Permalink
Remove Tables from fields __all__ check (#83)
Browse files Browse the repository at this point in the history
* fix: Remove `Tables` from being checked to use `__all__`.

* fix: Use fields all checker

* fix: Serializers version
  • Loading branch information
snaselj authored Mar 29, 2024
1 parent ffcea25 commit 27355d1
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 39 deletions.
15 changes: 9 additions & 6 deletions pylint_nautobot/use_fields_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
from astroid import Const
from pylint.checkers import BaseChecker

from .utils import find_meta
from .utils import is_version_compatible

_META_CLASSES = {
"nautobot.core.api.serializers.NautobotModelSerializer.Meta": ">1",
"nautobot.core.tables.BaseTable.Meta": ">=2",
"nautobot.extras.filters.NautobotFilterSet.Meta": ">1",
"nautobot.extras.forms.NautobotModelForm.Meta": ">1",
"nautobot.utilities.tables.BaseTable.Meta": ">1,<2",
"nautobot.core.api.serializers.NautobotModelSerializer": ">=2",
"nautobot.extras.filters.NautobotFilterSet": ">1",
"nautobot.extras.forms.base.NautobotModelForm": ">1",
}


Expand Down Expand Up @@ -46,7 +45,11 @@ def visit_classdef(self, node: ClassDef):
if not any(ancestor.qname() in self.meta_classes for ancestor in node.ancestors()):
return

for child_node in node.get_children():
meta = find_meta(node)
if not meta:
return

for child_node in meta.get_children():
if isinstance(child_node, Assign):
if any(isinstance(target, AssignName) and target.name == "fields" for target in child_node.targets):
value = child_node.value
Expand Down
10 changes: 10 additions & 0 deletions tests/inputs/use-fields-all/error_filter_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from nautobot.apps.filters import NautobotFilterSet


class MyAddressObjectFilterSet(NautobotFilterSet):
"""Filter for AddressObject."""

class Meta:
"""Meta attributes for filter."""

fields = ("name", "description")
10 changes: 10 additions & 0 deletions tests/inputs/use-fields-all/error_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from nautobot.extras.forms import NautobotModelForm


class AddressObjectSerializer(NautobotModelForm):
"""Model Form for AddressObject."""

class Meta:
"""Meta attributes for filter."""

fields = ("name", "description")
10 changes: 10 additions & 0 deletions tests/inputs/use-fields-all/error_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from nautobot.apps.api import NautobotModelSerializer


class AddressObjectSerializer(NautobotModelSerializer):
"""Serializer for AddressObject."""

class Meta:
"""Meta attributes for filter."""

fields = ("name", "description")
14 changes: 0 additions & 14 deletions tests/inputs/use-fields-all/error_table.py

This file was deleted.

10 changes: 10 additions & 0 deletions tests/inputs/use-fields-all/good_filter_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from nautobot.apps.filters import NautobotFilterSet


class AddressObjectFilterSet(NautobotFilterSet):
"""Filter for AddressObject."""

class Meta:
"""Meta attributes for filter."""

fields = "__all__"
10 changes: 10 additions & 0 deletions tests/inputs/use-fields-all/good_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from nautobot.extras.forms import NautobotModelForm


class AddressObjectSerializer(NautobotModelForm):
"""Model Form for AddressObject."""

class Meta:
"""Meta attributes for filter."""

fields = "__all__"
10 changes: 10 additions & 0 deletions tests/inputs/use-fields-all/good_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from nautobot.apps.api import NautobotModelSerializer


class AddressObjectSerializer(NautobotModelSerializer):
"""Serializer for AddressObject."""

class Meta:
"""Meta attributes for filter."""

fields = "__all__"
14 changes: 0 additions & 14 deletions tests/inputs/use-fields-all/good_table.py

This file was deleted.

28 changes: 23 additions & 5 deletions tests/test_use_fields_all.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Tests for use fields all"""

from astroid import Assign
from pylint.testutils import CheckerTestCase

from pylint_nautobot.use_fields_all import NautobotUseFieldsAllChecker
from pylint_nautobot.utils import find_meta

from .utils import assert_error_file
from .utils import assert_good_file
Expand All @@ -12,15 +14,31 @@

def _find_fields_node(module_node):
"""Find the fields node in the class definition."""
class_node = module_node.body[3]
meta = list(class_node.get_children())[3]
return list(meta.get_children())[1].value
class_node = module_node.body[1]
meta = find_meta(class_node)
if meta:
assign = list(meta.get_children())[0]
if isinstance(assign, Assign):
return assign.value


_EXPECTED_ERRORS = {
"table": {
"filter_set": {
"msg_id": "nb-use-fields-all",
"line": 14,
"line": 10,
"col_offset": 17,
"node": _find_fields_node,
},
"form": {
"msg_id": "nb-use-fields-all",
"line": 10,
"col_offset": 17,
"node": _find_fields_node,
},
"serializer": {
"versions": ">=2",
"msg_id": "nb-use-fields-all",
"line": 10,
"col_offset": 17,
"node": _find_fields_node,
},
Expand Down

0 comments on commit 27355d1

Please sign in to comment.