diff --git a/pylint_nautobot/use_fields_all.py b/pylint_nautobot/use_fields_all.py index 1ee40cd..466c8aa 100644 --- a/pylint_nautobot/use_fields_all.py +++ b/pylint_nautobot/use_fields_all.py @@ -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", } @@ -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 diff --git a/tests/inputs/use-fields-all/error_filter_set.py b/tests/inputs/use-fields-all/error_filter_set.py new file mode 100644 index 0000000..16242c7 --- /dev/null +++ b/tests/inputs/use-fields-all/error_filter_set.py @@ -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") diff --git a/tests/inputs/use-fields-all/error_form.py b/tests/inputs/use-fields-all/error_form.py new file mode 100644 index 0000000..4654221 --- /dev/null +++ b/tests/inputs/use-fields-all/error_form.py @@ -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") diff --git a/tests/inputs/use-fields-all/error_serializer.py b/tests/inputs/use-fields-all/error_serializer.py new file mode 100644 index 0000000..730b0a3 --- /dev/null +++ b/tests/inputs/use-fields-all/error_serializer.py @@ -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") diff --git a/tests/inputs/use-fields-all/error_table.py b/tests/inputs/use-fields-all/error_table.py deleted file mode 100644 index d6632d8..0000000 --- a/tests/inputs/use-fields-all/error_table.py +++ /dev/null @@ -1,14 +0,0 @@ -from nautobot.apps.tables import BaseTable -from nautobot.apps.tables import ToggleColumn -from nautobot.extras.tables import StatusTableMixin - - -class MyTable(StatusTableMixin, BaseTable): - """Table for list view.""" - - pk = ToggleColumn() - - class Meta(BaseTable.Meta): - """Meta attributes.""" - - fields = ("pk", "my_field1", "my_field2") diff --git a/tests/inputs/use-fields-all/good_filter_set.py b/tests/inputs/use-fields-all/good_filter_set.py new file mode 100644 index 0000000..ce5b42e --- /dev/null +++ b/tests/inputs/use-fields-all/good_filter_set.py @@ -0,0 +1,10 @@ +from nautobot.apps.filters import NautobotFilterSet + + +class AddressObjectFilterSet(NautobotFilterSet): + """Filter for AddressObject.""" + + class Meta: + """Meta attributes for filter.""" + + fields = "__all__" diff --git a/tests/inputs/use-fields-all/good_form.py b/tests/inputs/use-fields-all/good_form.py new file mode 100644 index 0000000..4112174 --- /dev/null +++ b/tests/inputs/use-fields-all/good_form.py @@ -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__" diff --git a/tests/inputs/use-fields-all/good_serializer.py b/tests/inputs/use-fields-all/good_serializer.py new file mode 100644 index 0000000..a6910cb --- /dev/null +++ b/tests/inputs/use-fields-all/good_serializer.py @@ -0,0 +1,10 @@ +from nautobot.apps.api import NautobotModelSerializer + + +class AddressObjectSerializer(NautobotModelSerializer): + """Serializer for AddressObject.""" + + class Meta: + """Meta attributes for filter.""" + + fields = "__all__" diff --git a/tests/inputs/use-fields-all/good_table.py b/tests/inputs/use-fields-all/good_table.py deleted file mode 100644 index b42857e..0000000 --- a/tests/inputs/use-fields-all/good_table.py +++ /dev/null @@ -1,14 +0,0 @@ -from nautobot.apps.tables import BaseTable -from nautobot.apps.tables import ToggleColumn -from nautobot.extras.tables import StatusTableMixin - - -class MyTable(StatusTableMixin, BaseTable): - """Table for list view.""" - - pk = ToggleColumn() - - class Meta(BaseTable.Meta): - """Meta attributes.""" - - fields = "__all__" diff --git a/tests/test_use_fields_all.py b/tests/test_use_fields_all.py index 073a442..49f7cad 100644 --- a/tests/test_use_fields_all.py +++ b/tests/test_use_fields_all.py @@ -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 @@ -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, },