From e0515651b9ad956812369e73ff143459bcc0a341 Mon Sep 17 00:00:00 2001 From: Jan Snasel Date: Fri, 1 Mar 2024 12:29:39 +0000 Subject: [PATCH 1/2] fix: Failing check on Golden Config for missing model --- pylint_nautobot/sub_class_name.py | 17 +++++++++++++---- pylint_nautobot/utils.py | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pylint_nautobot/sub_class_name.py b/pylint_nautobot/sub_class_name.py index 68c3ffc..122c6cf 100644 --- a/pylint_nautobot/sub_class_name.py +++ b/pylint_nautobot/sub_class_name.py @@ -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 @@ -69,6 +69,11 @@ class NautobotSubClassNameChecker(BaseChecker): "Sub-class name should be %s.", "nb-sub-class-name", "All classes should have a sub-class name that is .", + ), + "I4282": ( + "Model was not found in the class.", + "nb-no-model-found", + "Model was not found in the class.", ) } @@ -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) diff --git a/pylint_nautobot/utils.py b/pylint_nautobot/utils.py index 34672b2..97392ca 100644 --- a/pylint_nautobot/utils.py +++ b/pylint_nautobot/utils.py @@ -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: @@ -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) From be882f986c43bdde448b55fa8910840d7e763146 Mon Sep 17 00:00:00 2001 From: Jan Snasel Date: Fri, 1 Mar 2024 12:32:05 +0000 Subject: [PATCH 2/2] fix: Formatting --- pylint_nautobot/sub_class_name.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylint_nautobot/sub_class_name.py b/pylint_nautobot/sub_class_name.py index 122c6cf..6753724 100644 --- a/pylint_nautobot/sub_class_name.py +++ b/pylint_nautobot/sub_class_name.py @@ -74,7 +74,7 @@ class NautobotSubClassNameChecker(BaseChecker): "Model was not found in the class.", "nb-no-model-found", "Model was not found in the class.", - ) + ), } def __init__(self, *args, **kwargs):