Skip to content

Commit

Permalink
fix: Failing sub-class-name rule for model = models.MyModel
Browse files Browse the repository at this point in the history
  • Loading branch information
snaselj committed Feb 19, 2024
1 parent cec95d5 commit 784b572
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 4 deletions.
11 changes: 7 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,17 @@ 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):
return node.expr.name
if isinstance(node.expr, Attribute):
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": 5,
"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": 5,
"col_offset": 0,
"args": ("AddressObjectTable",),
"node": _find_failing_node_model,
},
}


Expand Down

0 comments on commit 784b572

Please sign in to comment.