-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Check for imports whose paths have changed in 2.0.""" | ||
|
||
from astroid import ClassDef | ||
from pylint.checkers import BaseChecker | ||
|
||
from .utils import find_ancestor | ||
from .utils import get_model_name | ||
from .utils import is_version_compatible | ||
|
||
_ANCESTORS = { | ||
"nautobot.extras.filters.NautobotFilterSet": ">=2", | ||
} | ||
|
||
_VERSION_COMPATIBLE_ANCESTORS = [key for key, value in _ANCESTORS.items() if is_version_compatible(value)] | ||
|
||
|
||
class NautobotSubClassNameChecker(BaseChecker): | ||
"""Ensure subclass name is <model class name><ancestor class type>. | ||
This can typically be done via <ancestor class name>.replace("Nautobot", <model class name>) | ||
""" | ||
|
||
version_specifier = ">1,<3" | ||
|
||
name = "nautobot-sub-class-name" | ||
msgs = { | ||
"E4242": ( | ||
"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>.", | ||
) | ||
} | ||
|
||
def visit_classdef(self, node: ClassDef): | ||
"""Visit class definitions.""" | ||
ancestor = find_ancestor(node, _VERSION_COMPATIBLE_ANCESTORS) | ||
if not ancestor: | ||
return | ||
|
||
class_name = node.name | ||
model_name = get_model_name(ancestor, node) | ||
expected_name = ancestor.split(".")[-1].replace("Nautobot", model_name) | ||
if expected_name != class_name: | ||
self.add_message("nb-sub-class-name", node=node, args=(expected_name,)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from nautobot.apps.filters import NautobotFilterSet | ||
from nautobot.core.models.generics import PrimaryModel | ||
|
||
|
||
class AddressObject(PrimaryModel): | ||
pass | ||
|
||
|
||
class MyAddressObjectFilterSet(NautobotFilterSet): | ||
"""Filter for AddressObject.""" | ||
|
||
class Meta: | ||
"""Meta attributes for filter.""" | ||
|
||
model = AddressObject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from nautobot.apps.filters import NautobotFilterSet | ||
from nautobot.core.models.generics import PrimaryModel | ||
|
||
|
||
class AddressObject(PrimaryModel): | ||
pass | ||
|
||
|
||
class AddressObjectFilterSet(NautobotFilterSet): | ||
"""Filter for AddressObject.""" | ||
|
||
class Meta: | ||
"""Meta attributes for filter.""" | ||
|
||
model = AddressObject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""Tests for sub class name checker.""" | ||
|
||
from pylint.testutils import CheckerTestCase | ||
|
||
from pylint_nautobot.sub_class_name import NautobotSubClassNameChecker | ||
|
||
from .utils import assert_error_file | ||
from .utils import assert_good_file | ||
from .utils import parametrize_error_files | ||
from .utils import parametrize_good_files | ||
|
||
|
||
def _find_failing_node(module_node): | ||
return module_node.body[3] | ||
|
||
|
||
_EXPECTED_ERRORS = { | ||
"filter_set": { | ||
"msg_id": "nb-sub-class-name", | ||
"line": 9, | ||
"col_offset": 0, | ||
"args": ("AddressObjectFilterSet",), | ||
"node": _find_failing_node, | ||
}, | ||
} | ||
|
||
|
||
class TestSubClassNameChecker(CheckerTestCase): | ||
"""Test sub class name checker""" | ||
|
||
CHECKER_CLASS = NautobotSubClassNameChecker | ||
|
||
@parametrize_error_files(__file__, _EXPECTED_ERRORS) | ||
def test_sub_class_name(self, path, expected_error): | ||
assert_error_file(self, path, expected_error) | ||
|
||
@parametrize_good_files(__file__) | ||
def test_no_issues(self, path): | ||
assert_good_file(self, path) |