diff options
author | Abhijeet Kasurde <akasurde@redhat.com> | 2025-01-03 03:14:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-03 03:14:56 +0100 |
commit | f72aa1dc0c587f4e27124063f4e9c341fc51ba33 (patch) | |
tree | 5600fd5a3ed1cd33034606221c84836f4b1fe40a | |
parent | Added docstrings to V2 methods in the CallbackBase Class (4 & 5 of 27) (#83507) (diff) | |
download | ansible-f72aa1dc0c587f4e27124063f4e9c341fc51ba33.tar.xz ansible-f72aa1dc0c587f4e27124063f4e9c341fc51ba33.zip |
test: Update regex for required and default in FieldAttributes (#84209)
Fixes: #61460
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
-rw-r--r-- | test/sanity/code-smell/required-and-default-attributes.py | 26 | ||||
-rw-r--r-- | test/sanity/ignore.txt | 1 |
2 files changed, 16 insertions, 11 deletions
diff --git a/test/sanity/code-smell/required-and-default-attributes.py b/test/sanity/code-smell/required-and-default-attributes.py index 900829dce7..7b1c89f9b8 100644 --- a/test/sanity/code-smell/required-and-default-attributes.py +++ b/test/sanity/code-smell/required-and-default-attributes.py @@ -1,19 +1,25 @@ from __future__ import annotations -import re +import ast +import pathlib import sys -def main(): - for path in sys.argv[1:] or sys.stdin.read().splitlines(): - with open(path, 'r') as path_fd: - for line, text in enumerate(path_fd.readlines()): - match = re.search(r'(FieldAttribute.*(default|required).*(default|required))', text) +class CallVisitor(ast.NodeVisitor): + def __init__(self, path: str) -> None: + self.path = path + + def visit_Call(self, node: ast.Call) -> None: + if isinstance(node.func, ast.Name) and node.func.id.endswith("FieldAttribute"): + if len([kw for kw in node.keywords if kw.arg in ("default", "required")]) > 1: + print(f"{self.path}:{node.lineno}:{node.col_offset}: use only one of `default` or `required` with `{node.func.id}`") - if match: - print('%s:%d:%d: use only one of `default` or `required` with `FieldAttribute`' % ( - path, line + 1, match.start(1) + 1)) + +def main() -> None: + for path in sys.argv[1:] or sys.stdin.read().splitlines(): + tree = ast.parse(pathlib.Path(path).read_text(), path) + CallVisitor(path).visit(tree) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/test/sanity/ignore.txt b/test/sanity/ignore.txt index 44378a63c2..a38f60b726 100644 --- a/test/sanity/ignore.txt +++ b/test/sanity/ignore.txt @@ -50,7 +50,6 @@ lib/ansible/module_utils/six/__init__.py pylint:unidiomatic-typecheck lib/ansible/module_utils/six/__init__.py replace-urlopen lib/ansible/module_utils/urls.py replace-urlopen lib/ansible/parsing/yaml/objects.py pylint:arguments-renamed -lib/ansible/playbook/collectionsearch.py required-and-default-attributes # https://github.com/ansible/ansible/issues/61460 lib/ansible/playbook/role/include.py pylint:arguments-renamed lib/ansible/plugins/action/normal.py action-plugin-docs # default action plugin for modules without a dedicated action plugin lib/ansible/plugins/cache/base.py ansible-doc!skip # not a plugin, but a stub for backwards compatibility |