Skip to content

Commit

Permalink
Merge pull request #76 from nautobot/u/snaselj-fix-viewset
Browse files Browse the repository at this point in the history
Fix UIViewSet sub class name
  • Loading branch information
snaselj authored Feb 19, 2024
2 parents cec95d5 + 87b6634 commit 2f9d93f
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pylint_nautobot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def get_model_name(node: ClassDef) -> str:
"""Get the model name from the class definition."""
queryset = find_attr(node, "queryset")
if queryset:
return find_model_name_from_queryset(queryset.value)
return get_model_name_from_queryset(queryset.value)

model_attr = find_attr(node, "model")
if not model_attr:
Expand All @@ -130,14 +130,19 @@ def get_model_name(node: ClassDef) -> str:
return get_model_name_from_attr(model_attr)


def find_model_name_from_queryset(node: NodeNG) -> str:
def get_model_name_from_queryset(node: NodeNG) -> str:
"""Get the model name from the queryset assignment value."""
while node:
if isinstance(node, Call):
node = node.func
elif isinstance(node, Attribute):
if node.attrname == "objects" and isinstance(node.expr, Name):
return node.expr.name
if node.attrname == "objects":
if isinstance(node.expr, Name):
# Covers `queryset = AddressObject.objects.all()`
return node.expr.name
if isinstance(node.expr, Attribute):
# Covers `queryset = models.AddressObject.objects.all()`
return node.expr.attrname
node = node.expr
else:
break
Expand Down
Empty file.
12 changes: 12 additions & 0 deletions tests/inputs/sub-class-name/error_table_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from nautobot.apps.tables import BaseTable

from . import models


class MyAddressObjectTable(BaseTable):
"""Filter for AddressObject."""

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

model = models.AddressObject
9 changes: 9 additions & 0 deletions tests/inputs/sub-class-name/error_viewset_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from nautobot.apps.views import NautobotUIViewSet

from . import models


class MyAddressObjectUIViewSet(NautobotUIViewSet):
"""Filter for AddressObject."""

queryset = models.AddressObject.objects.all()
12 changes: 12 additions & 0 deletions tests/inputs/sub-class-name/good_table_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from nautobot.apps.tables import BaseTable

from . import models


class AddressObjectTable(BaseTable):
"""Filter for AddressObject."""

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

model = models.AddressObject
9 changes: 9 additions & 0 deletions tests/inputs/sub-class-name/good_viewset_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from nautobot.apps.views import NautobotUIViewSet

from . import models


class AddressObjectUIViewSet(NautobotUIViewSet):
"""Filter for AddressObject."""

queryset = models.AddressObject.objects.all()
5 changes: 5 additions & 0 deletions tests/inputs/sub-class-name/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from nautobot.core.models.generics import PrimaryModel


class AddressObject(PrimaryModel):
pass
20 changes: 20 additions & 0 deletions tests/test_sub_class_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def _find_failing_node(module_node):
return module_node.body[3]


def _find_failing_node_model(module_node):
return module_node.body[2]


_EXPECTED_ERRORS = {
"filter_form": {
"versions": ">=2",
Expand Down Expand Up @@ -55,6 +59,14 @@ def _find_failing_node(module_node):
"args": ("AddressObjectUIViewSet",),
"node": _find_failing_node,
},
"viewset_model": {
"versions": ">=2",
"msg_id": "nb-sub-class-name",
"line": 6,
"col_offset": 0,
"args": ("AddressObjectUIViewSet",),
"node": _find_failing_node_model,
},
"table": {
"versions": ">=2",
"msg_id": "nb-sub-class-name",
Expand All @@ -63,6 +75,14 @@ def _find_failing_node(module_node):
"args": ("AddressObjectTable",),
"node": _find_failing_node,
},
"table_model": {
"versions": ">=2",
"msg_id": "nb-sub-class-name",
"line": 6,
"col_offset": 0,
"args": ("AddressObjectTable",),
"node": _find_failing_node_model,
},
}


Expand Down

0 comments on commit 2f9d93f

Please sign in to comment.