Skip to content

Commit

Permalink
Merge pull request #77 from nautobot/u/snaselj-fix-gc-missing-model
Browse files Browse the repository at this point in the history
Fix missing model
  • Loading branch information
Kircheneer authored Mar 5, 2024
2 parents 11ca12f + be882f9 commit baefb71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 14 additions & 5 deletions pylint_nautobot/sub_class_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pylint.checkers import BaseChecker

from .utils import find_ancestor
from .utils import get_model_name
from .utils import find_model_name
from .utils import is_abstract_class
from .utils import is_version_compatible
from .utils import trim_first_pascal_word
Expand Down Expand Up @@ -69,7 +69,12 @@ class NautobotSubClassNameChecker(BaseChecker):
"Sub-class name should be %s.",
"nb-sub-class-name",
"All classes should have a sub-class name that is <model class name><ancestor class type>.",
)
),
"I4282": (
"Model was not found in the class.",
"nb-no-model-found",
"Model was not found in the class.",
),
}

def __init__(self, *args, **kwargs):
Expand All @@ -87,6 +92,10 @@ def visit_classdef(self, node: ClassDef):
if not ancestor:
return

expected_name = get_model_name(node) + ancestor.suffix
if expected_name != node.name:
self.add_message("nb-sub-class-name", node=node, args=(expected_name,))
model_name = find_model_name(node)
if model_name:
expected_name = model_name + ancestor.suffix
if expected_name != node.name:
self.add_message("nb-sub-class-name", node=node, args=(expected_name,))
else:
self.add_message("nb-no-model-found", node=node)
6 changes: 3 additions & 3 deletions pylint_nautobot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def find_meta(node: ClassDef) -> Optional[ClassDef]:
return None


def get_model_name(node: ClassDef) -> str:
def find_model_name(node: ClassDef) -> str:
"""Get the model name from the class definition."""
queryset = find_attr(node, "queryset")
if queryset:
Expand All @@ -122,10 +122,10 @@ def get_model_name(node: ClassDef) -> str:
if not model_attr:
meta = find_meta(node)
if not meta:
raise NotImplementedError("This class does not have a Meta class.")
return ""
model_attr = find_attr(meta, "model")
if not model_attr:
raise NotImplementedError("The Meta class does not define a model attribute.")
return ""

return get_model_name_from_attr(model_attr)

Expand Down

0 comments on commit baefb71

Please sign in to comment.