Skip to content

Commit 101ba4a

Browse files
nordicjmstephanosio
authored andcommitted
scripts: pylint: Add argument parser abbreviation checker
Adds a pylint checker for ensuring that the argument parser library is setup with abbreviations disabled. Signed-off-by: Jamie McCrae <[email protected]>
1 parent ec70444 commit 101ba4a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright (c) 2023 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from __future__ import annotations
5+
6+
from pylint.checkers import BaseChecker
7+
8+
import astroid
9+
from astroid import nodes
10+
11+
class ZephyrArgParseChecker(BaseChecker):
12+
"""Class implementing function checker for Zephyr."""
13+
14+
# The name defines a custom section of the config for this checker.
15+
name = "zephyr-arg-parse"
16+
17+
# Register messages emitted by the checker.
18+
msgs = {
19+
"E9901": (
20+
"Argument parser with abbreviations is disallowed",
21+
"argument-parser-with-abbreviations",
22+
"An ArgumentParser object must set `allow_abbrev=false` to disable "
23+
"abbreviations and prevent issues with these being used by projects"
24+
" and/or scripts."
25+
)
26+
}
27+
28+
# Function that looks at evert function call for ArgumentParser invocation
29+
def visit_call(self, node: nodes.Call) -> None:
30+
if isinstance(node.func, astroid.nodes.node_classes.Attribute) and \
31+
node.func.attrname == "ArgumentParser":
32+
abbrev_disabled = False
33+
34+
# Check that allow_abbrev is set and that the value is False
35+
for keyword in node.keywords:
36+
if keyword.arg == "allow_abbrev":
37+
if not isinstance(keyword.value, astroid.nodes.node_classes.Const):
38+
continue
39+
if keyword.value.pytype() != "builtins.bool":
40+
continue
41+
if keyword.value.value is False:
42+
abbrev_disabled = True
43+
44+
if abbrev_disabled is False:
45+
self.add_message(
46+
"argument-parser-with-abbreviations", node=node
47+
)
48+
49+
return ()
50+
51+
# This is called from pylint, hence PyLinter not being declared in this file
52+
# pylint: disable=undefined-variable
53+
def register(linter: PyLinter) -> None:
54+
linter.register_checker(ZephyrArgParseChecker(linter))

0 commit comments

Comments
 (0)