Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load rule classes from any modules #1978

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ hwcksum
idempotency
importlib
iniconfig
isclass
isdir
isdisjoint
iskeyword
Expand Down
18 changes: 10 additions & 8 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ def matchyaml(self, file: Lintable) -> List[MatchError]:
return matches


def is_valid_rule(rule: AnsibleLintRule) -> bool:
def is_valid_rule(rule: Any) -> bool:
"""Check if given rule is valid or not."""
return isinstance(rule, AnsibleLintRule) and bool(rule.id) and bool(rule.shortdesc)
return issubclass(rule, AnsibleLintRule) and bool(rule.id) and bool(rule.shortdesc)


def load_plugins(directory: str) -> Iterator[AnsibleLintRule]:
Expand All @@ -212,12 +212,14 @@ def load_plugins(directory: str) -> Iterator[AnsibleLintRule]:
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
try:
rule = getattr(module, pluginname)()
if is_valid_rule(rule):
yield rule

except (TypeError, ValueError, AttributeError):
_logger.warning("Skipped invalid rule from %s", pluginname)
for _, obj in inspect.getmembers(module):
if inspect.isclass(obj) and is_valid_rule(obj):
yield obj()

except (TypeError, ValueError, AttributeError) as exc:
_logger.warning(
"Skipped invalid rule from %s due to %s", pluginname, exc
)


class RulesCollection:
Expand Down