From f3646adf15922ed11ea8a1cf1787e8b2cb530f83 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 28 Jun 2023 08:19:08 +0200 Subject: [PATCH] fix linting --- ansiblelater/candidate.py | 2 +- ansiblelater/rules/CheckBecomeUser.py | 4 +++- ansiblelater/rules/CheckBracesSpaces.py | 5 ++++- ansiblelater/rules/CheckChangedInWhen.py | 4 +++- ansiblelater/rules/CheckCommandHasChanges.py | 4 +++- ansiblelater/rules/CheckCommandInsteadOfArgument.py | 3 ++- ansiblelater/rules/CheckCommandInsteadOfModule.py | 3 ++- ansiblelater/rules/CheckCompareToEmptyString.py | 3 ++- ansiblelater/rules/CheckCompareToLiteralBool.py | 3 ++- ansiblelater/rules/CheckDeprecated.py | 4 +++- ansiblelater/rules/CheckDeprecatedBareVars.py | 3 ++- ansiblelater/rules/CheckFilePermissionMissing.py | 7 ++++--- ansiblelater/rules/CheckFilePermissionOctal.py | 4 +++- ansiblelater/rules/CheckFilterSeparation.py | 4 +++- ansiblelater/rules/CheckGitHasVersion.py | 4 +++- ansiblelater/rules/CheckInstallUseLatest.py | 4 +++- ansiblelater/rules/CheckLiteralBoolFormat.py | 4 +++- ansiblelater/rules/CheckLocalAction.py | 4 +++- ansiblelater/rules/CheckMetaChangeFromDefault.py | 4 +++- ansiblelater/rules/CheckMetaMain.py | 4 +++- ansiblelater/rules/CheckNameFormat.py | 3 ++- ansiblelater/rules/CheckNamedTask.py | 4 +++- ansiblelater/rules/CheckNativeYaml.py | 4 +++- ansiblelater/rules/CheckNestedJinja.py | 4 +++- ansiblelater/rules/CheckRelativeRolePaths.py | 4 +++- ansiblelater/rules/CheckScmInSrc.py | 4 +++- ansiblelater/rules/CheckShellInsteadCommand.py | 4 +++- ansiblelater/rules/CheckTaskSeparation.py | 3 ++- ansiblelater/rules/CheckUniqueNamedTask.py | 3 ++- ansiblelater/rules/CheckVersion.py | 5 ++++- ansiblelater/rules/CheckWhenFormat.py | 4 +++- ansiblelater/rules/CheckYamlColons.py | 6 +++++- ansiblelater/rules/CheckYamlDocumentEnd.py | 6 +++++- ansiblelater/rules/CheckYamlDocumentStart.py | 6 +++++- ansiblelater/rules/CheckYamlEmptyLines.py | 6 +++++- ansiblelater/rules/CheckYamlFile.py | 3 ++- ansiblelater/rules/CheckYamlHasContent.py | 4 +++- ansiblelater/rules/CheckYamlHyphens.py | 6 +++++- ansiblelater/rules/CheckYamlIndent.py | 6 +++++- ansiblelater/standard.py | 2 +- ansiblelater/utils/__init__.py | 3 ++- ansiblelater/utils/yamlhelper.py | 6 ++---- 42 files changed, 126 insertions(+), 47 deletions(-) diff --git a/ansiblelater/candidate.py b/ansiblelater/candidate.py index 12194e2..007a122 100644 --- a/ansiblelater/candidate.py +++ b/ansiblelater/candidate.py @@ -191,7 +191,7 @@ class Candidate: if sid: standard_id = f"[{sid}] " - return standard_id # noqa + return standard_id def __repr__(self): return f"{type(self).__name__} ({self.path})" diff --git a/ansiblelater/rules/CheckBecomeUser.py b/ansiblelater/rules/CheckBecomeUser.py index 6eb3a1c..5cab707 100644 --- a/ansiblelater/rules/CheckBecomeUser.py +++ b/ansiblelater/rules/CheckBecomeUser.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -7,7 +9,7 @@ class CheckBecomeUser(StandardBase): description = "Become should be combined with become_user" helptext = "the task has `become` enabled but `become_user` is missing" version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckBracesSpaces.py b/ansiblelater/rules/CheckBracesSpaces.py index 3cf3cdf..1d95c27 100644 --- a/ansiblelater/rules/CheckBracesSpaces.py +++ b/ansiblelater/rules/CheckBracesSpaces.py @@ -1,4 +1,5 @@ import re +from typing import ClassVar from ansiblelater.standard import StandardBase from ansiblelater.utils import count_spaces @@ -10,7 +11,9 @@ class CheckBracesSpaces(StandardBase): description = "YAML should use consistent number of spaces around variables" helptext = "no suitable numbers of spaces (min: {min} max: {max})" version = "0.1" - types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta"] + types: ClassVar[list[str]] = [ + "playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta" + ] def check(self, candidate, settings): yamllines, errors = self.get_normalized_yaml(candidate, settings) diff --git a/ansiblelater/rules/CheckChangedInWhen.py b/ansiblelater/rules/CheckChangedInWhen.py index 09e63fd..faad404 100644 --- a/ansiblelater/rules/CheckChangedInWhen.py +++ b/ansiblelater/rules/CheckChangedInWhen.py @@ -18,6 +18,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -27,7 +29,7 @@ class CheckChangedInWhen(StandardBase): description = "Use handlers instead of `when: changed`" helptext = "tasks using `when: result.changed` setting are effectively acting as a handler" version = "0.2" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckCommandHasChanges.py b/ansiblelater/rules/CheckCommandHasChanges.py index 6391178..0dac9e1 100644 --- a/ansiblelater/rules/CheckCommandHasChanges.py +++ b/ansiblelater/rules/CheckCommandHasChanges.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -10,7 +12,7 @@ class CheckCommandHasChanges(StandardBase): "idempotent while using controls like `creates`, `removes` or `when`" ) version = "0.1" - types = ["playbook", "task"] + types: ClassVar[list[str]] = ["playbook", "task"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckCommandInsteadOfArgument.py b/ansiblelater/rules/CheckCommandInsteadOfArgument.py index 21f68e3..482ca44 100644 --- a/ansiblelater/rules/CheckCommandInsteadOfArgument.py +++ b/ansiblelater/rules/CheckCommandInsteadOfArgument.py @@ -19,6 +19,7 @@ # THE SOFTWARE. import os +from typing import ClassVar from ansiblelater.standard import StandardBase @@ -29,7 +30,7 @@ class CheckCommandInsteadOfArgument(StandardBase): description = "Commands should not be used in place of module arguments" helptext = "{exec} used in place of file modules argument {arg}" version = "0.2" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckCommandInsteadOfModule.py b/ansiblelater/rules/CheckCommandInsteadOfModule.py index 4902f5f..f2ddf62 100644 --- a/ansiblelater/rules/CheckCommandInsteadOfModule.py +++ b/ansiblelater/rules/CheckCommandInsteadOfModule.py @@ -1,4 +1,5 @@ import os +from typing import ClassVar from ansiblelater.standard import StandardBase @@ -9,7 +10,7 @@ class CheckCommandInsteadOfModule(StandardBase): description = "Commands should not be used in place of modules" helptext = "{exec} command used in place of {module} module" version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckCompareToEmptyString.py b/ansiblelater/rules/CheckCompareToEmptyString.py index 2291320..d24c191 100644 --- a/ansiblelater/rules/CheckCompareToEmptyString.py +++ b/ansiblelater/rules/CheckCompareToEmptyString.py @@ -1,4 +1,5 @@ import re +from typing import ClassVar from ansiblelater.candidate import Template from ansiblelater.standard import StandardBase @@ -10,7 +11,7 @@ class CheckCompareToEmptyString(StandardBase): description = "Don't compare to empty string \"\"" helptext = ("use `when: var` rather than `when: var !=` (or conversely `when: not var`)") version = "0.1" - types = ["playbook", "task", "handler", "template"] + types: ClassVar[list[str]] = ["playbook", "task", "handler", "template"] def check(self, candidate, settings): yamllines, errors = self.get_normalized_yaml(candidate, settings) diff --git a/ansiblelater/rules/CheckCompareToLiteralBool.py b/ansiblelater/rules/CheckCompareToLiteralBool.py index fe4ce9a..9dc193d 100644 --- a/ansiblelater/rules/CheckCompareToLiteralBool.py +++ b/ansiblelater/rules/CheckCompareToLiteralBool.py @@ -1,4 +1,5 @@ import re +from typing import ClassVar from ansiblelater.candidate import Template from ansiblelater.standard import StandardBase @@ -10,7 +11,7 @@ class CheckCompareToLiteralBool(StandardBase): description = "Don't compare to True or False" helptext = ("use `when: var` rather than `when: var == True` (or conversely `when: not var`)") version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): yamllines, errors = self.get_normalized_yaml(candidate, settings) diff --git a/ansiblelater/rules/CheckDeprecated.py b/ansiblelater/rules/CheckDeprecated.py index 8301482..1ed5bf2 100644 --- a/ansiblelater/rules/CheckDeprecated.py +++ b/ansiblelater/rules/CheckDeprecated.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -7,7 +9,7 @@ class CheckDeprecated(StandardBase): description = "Deprecated features should not be used" helptext = "'{old}' is deprecated and should not be used anymore. Use '{new}' instead." version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings, full=True) diff --git a/ansiblelater/rules/CheckDeprecatedBareVars.py b/ansiblelater/rules/CheckDeprecatedBareVars.py index 26677c1..8293326 100644 --- a/ansiblelater/rules/CheckDeprecatedBareVars.py +++ b/ansiblelater/rules/CheckDeprecatedBareVars.py @@ -19,6 +19,7 @@ # THE SOFTWARE. import os +from typing import ClassVar from ansiblelater.standard import StandardBase from ansiblelater.utils import has_glob, has_jinja @@ -33,7 +34,7 @@ class CheckDeprecatedBareVars(StandardBase): "or be converted to a list" ) version = "0.3" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, self.errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckFilePermissionMissing.py b/ansiblelater/rules/CheckFilePermissionMissing.py index 30ab294..5c2a7ef 100644 --- a/ansiblelater/rules/CheckFilePermissionMissing.py +++ b/ansiblelater/rules/CheckFilePermissionMissing.py @@ -18,6 +18,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import re +from typing import ClassVar from ansiblelater.standard import StandardBase @@ -31,9 +32,9 @@ class CheckFilePermissionMissing(StandardBase): "to avoid unexpected file permissions" ) version = "0.2" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] - _modules = { + _modules: ClassVar[dict] = { "archive", "assemble", "copy", @@ -41,7 +42,7 @@ class CheckFilePermissionMissing(StandardBase): "replace", "template", } - _create_modules = { + _create_modules: ClassVar[dict[str, bool]] = { "blockinfile": False, "htpasswd": True, "ini_file": True, diff --git a/ansiblelater/rules/CheckFilePermissionOctal.py b/ansiblelater/rules/CheckFilePermissionOctal.py index 61f9738..f6936df 100644 --- a/ansiblelater/rules/CheckFilePermissionOctal.py +++ b/ansiblelater/rules/CheckFilePermissionOctal.py @@ -18,6 +18,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -27,7 +29,7 @@ class CheckFilePermissionOctal(StandardBase): description = "Octal file permissions must contain leading zero or be a string" helptext = "numeric file permissions without leading zero can behave in unexpected ways" version = "0.2" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckFilterSeparation.py b/ansiblelater/rules/CheckFilterSeparation.py index 248f96c..fe1e0af 100644 --- a/ansiblelater/rules/CheckFilterSeparation.py +++ b/ansiblelater/rules/CheckFilterSeparation.py @@ -1,4 +1,5 @@ import re +from typing import ClassVar from ansiblelater.standard import StandardBase @@ -9,7 +10,8 @@ class CheckFilterSeparation(StandardBase): description = "Jinja2 filters should be separated with spaces" helptext = "no suitable numbers of spaces (required: 1)" version = "0.1" - types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars"] + types: ClassVar[list[str] + ] = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars"] def check(self, candidate, settings): yamllines, errors = self.get_normalized_yaml(candidate, settings) diff --git a/ansiblelater/rules/CheckGitHasVersion.py b/ansiblelater/rules/CheckGitHasVersion.py index bdd45c2..114efa4 100644 --- a/ansiblelater/rules/CheckGitHasVersion.py +++ b/ansiblelater/rules/CheckGitHasVersion.py @@ -18,6 +18,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -27,7 +29,7 @@ class CheckGitHasVersion(StandardBase): description = "Git checkouts should use explicit version" helptext = "git checkouts should point to an explicit commit or tag, not `latest`" version = "0.2" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckInstallUseLatest.py b/ansiblelater/rules/CheckInstallUseLatest.py index 7025c04..9d15d89 100644 --- a/ansiblelater/rules/CheckInstallUseLatest.py +++ b/ansiblelater/rules/CheckInstallUseLatest.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -7,7 +9,7 @@ class CheckInstallUseLatest(StandardBase): description = "Package installs should use present, not latest" helptext = "package installs should use `state=present` with or without a version" version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckLiteralBoolFormat.py b/ansiblelater/rules/CheckLiteralBoolFormat.py index 71e9ebb..42d3f2e 100644 --- a/ansiblelater/rules/CheckLiteralBoolFormat.py +++ b/ansiblelater/rules/CheckLiteralBoolFormat.py @@ -1,4 +1,5 @@ import re +from typing import ClassVar from ansiblelater.standard import StandardBase @@ -9,7 +10,8 @@ class CheckLiteralBoolFormat(StandardBase): description = "Literal bools should be consistent" helptext = "literal bools should be written as `{bools}`" version = "0.1" - types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars"] + types: ClassVar[list[str] + ] = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars"] def check(self, candidate, settings): yamllines, errors = self.get_normalized_yaml(candidate, settings) diff --git a/ansiblelater/rules/CheckLocalAction.py b/ansiblelater/rules/CheckLocalAction.py index edf81ee..5ee3682 100644 --- a/ansiblelater/rules/CheckLocalAction.py +++ b/ansiblelater/rules/CheckLocalAction.py @@ -1,5 +1,7 @@ # Copyright (c) 2016, Tsukinowa Inc. # Copyright (c) 2018, Ansible Project +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -9,7 +11,7 @@ class CheckLocalAction(StandardBase): description = "Don't use local_action" helptext = ("`delegate_to: localhost` should be used instead of `local_action`") version = "0.2" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): yamllines, errors = self.get_normalized_yaml(candidate, settings) diff --git a/ansiblelater/rules/CheckMetaChangeFromDefault.py b/ansiblelater/rules/CheckMetaChangeFromDefault.py index 2f9f599..694f84b 100644 --- a/ansiblelater/rules/CheckMetaChangeFromDefault.py +++ b/ansiblelater/rules/CheckMetaChangeFromDefault.py @@ -1,4 +1,6 @@ # Copyright (c) 2018, Ansible Project +from typing import ClassVar + from nested_lookup import nested_lookup from ansiblelater.standard import StandardBase @@ -10,7 +12,7 @@ class CheckMetaChangeFromDefault(StandardBase): description = "Roles meta/main.yml default values should be changed" helptext = "meta/main.yml default values should be changed for: `{field}`" version = "0.2" - types = ["meta"] + types: ClassVar[list[str]] = ["meta"] def check(self, candidate, settings): content, errors = self.get_raw_yaml(candidate, settings) diff --git a/ansiblelater/rules/CheckMetaMain.py b/ansiblelater/rules/CheckMetaMain.py index 1e1ca86..9ca96a1 100644 --- a/ansiblelater/rules/CheckMetaMain.py +++ b/ansiblelater/rules/CheckMetaMain.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from nested_lookup import nested_lookup from ansiblelater.standard import StandardBase @@ -9,7 +11,7 @@ class CheckMetaMain(StandardBase): description = "Roles must contain suitable meta/main.yml" helptext = "file should contain `{key}` key" version = "0.1" - types = ["meta"] + types: ClassVar[list[str]] = ["meta"] def check(self, candidate, settings): content, errors = self.get_raw_yaml(candidate, settings) diff --git a/ansiblelater/rules/CheckNameFormat.py b/ansiblelater/rules/CheckNameFormat.py index 8747db8..5ad17ef 100644 --- a/ansiblelater/rules/CheckNameFormat.py +++ b/ansiblelater/rules/CheckNameFormat.py @@ -1,4 +1,5 @@ from collections import defaultdict +from typing import ClassVar from ansiblelater.standard import StandardBase @@ -9,7 +10,7 @@ class CheckNameFormat(StandardBase): description = "Name of tasks and handlers must be formatted" helptext = "name '{name}' should start with uppercase" version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckNamedTask.py b/ansiblelater/rules/CheckNamedTask.py index f100097..ae6d20f 100644 --- a/ansiblelater/rules/CheckNamedTask.py +++ b/ansiblelater/rules/CheckNamedTask.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -7,7 +9,7 @@ class CheckNamedTask(StandardBase): description = "Tasks and handlers must be named" helptext = "module '{module}' used without or empty `name` attribute" version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckNativeYaml.py b/ansiblelater/rules/CheckNativeYaml.py index 75554f8..a9e646b 100644 --- a/ansiblelater/rules/CheckNativeYaml.py +++ b/ansiblelater/rules/CheckNativeYaml.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -7,7 +9,7 @@ class CheckNativeYaml(StandardBase): description = "Use YAML format for tasks and handlers rather than key=value" helptext = "task arguments appear to be in key value rather than YAML format" version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_action_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckNestedJinja.py b/ansiblelater/rules/CheckNestedJinja.py index 2d5f19b..041c74f 100644 --- a/ansiblelater/rules/CheckNestedJinja.py +++ b/ansiblelater/rules/CheckNestedJinja.py @@ -20,6 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import re +from typing import ClassVar from ansiblelater.standard import StandardBase @@ -33,7 +34,8 @@ class CheckNestedJinja(StandardBase): "like `{{ list_one + {{ list_two | max }} }}`" ) version = "0.2" - types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars"] + types: ClassVar[list[str] + ] = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars"] def check(self, candidate, settings): yamllines, errors = self.get_normalized_yaml(candidate, settings) diff --git a/ansiblelater/rules/CheckRelativeRolePaths.py b/ansiblelater/rules/CheckRelativeRolePaths.py index 01ad66f..293a5b8 100644 --- a/ansiblelater/rules/CheckRelativeRolePaths.py +++ b/ansiblelater/rules/CheckRelativeRolePaths.py @@ -1,5 +1,7 @@ # Copyright (c) 2016, Tsukinowa Inc. # Copyright (c) 2018, Ansible Project +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -9,7 +11,7 @@ class CheckRelativeRolePaths(StandardBase): description = "Don't use a relative path in a role" helptext = "`copy` and `template` modules don't need relative path for `src`" version = "0.2" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckScmInSrc.py b/ansiblelater/rules/CheckScmInSrc.py index f0a2f7a..46e40d7 100644 --- a/ansiblelater/rules/CheckScmInSrc.py +++ b/ansiblelater/rules/CheckScmInSrc.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansible.parsing.yaml.objects import AnsibleMapping from ansiblelater.standard import StandardBase @@ -9,7 +11,7 @@ class CheckScmInSrc(StandardBase): description = "Use `scm:` key rather than `src: scm+url`" helptext = "usage of `src: scm+url` not recommended" version = "0.1" - types = ["rolesfile"] + types: ClassVar[list[str]] = ["rolesfile"] def check(self, candidate, settings): roles, errors = self.get_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckShellInsteadCommand.py b/ansiblelater/rules/CheckShellInsteadCommand.py index b36a42f..ce93612 100644 --- a/ansiblelater/rules/CheckShellInsteadCommand.py +++ b/ansiblelater/rules/CheckShellInsteadCommand.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -7,7 +9,7 @@ class CheckShellInsteadCommand(StandardBase): description = "Shell should only be used when essential" helptext = "shell should only be used when piping, redirecting or chaining commands" version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckTaskSeparation.py b/ansiblelater/rules/CheckTaskSeparation.py index b7b58f5..1763a11 100644 --- a/ansiblelater/rules/CheckTaskSeparation.py +++ b/ansiblelater/rules/CheckTaskSeparation.py @@ -1,5 +1,6 @@ import re from collections import defaultdict +from typing import ClassVar from ansiblelater.standard import StandardBase @@ -10,7 +11,7 @@ class CheckTaskSeparation(StandardBase): description = "Single tasks should be separated by empty line" helptext = "missing task separation (required: 1 empty line)" version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): options = defaultdict(dict) diff --git a/ansiblelater/rules/CheckUniqueNamedTask.py b/ansiblelater/rules/CheckUniqueNamedTask.py index 558a145..f8ee2dc 100644 --- a/ansiblelater/rules/CheckUniqueNamedTask.py +++ b/ansiblelater/rules/CheckUniqueNamedTask.py @@ -1,4 +1,5 @@ from collections import defaultdict +from typing import ClassVar from ansiblelater.standard import StandardBase @@ -9,7 +10,7 @@ class CheckUniqueNamedTask(StandardBase): description = "Tasks and handlers must be uniquely named within a single file" helptext = "name '{name}' appears multiple times" version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckVersion.py b/ansiblelater/rules/CheckVersion.py index b122164..4a057d8 100644 --- a/ansiblelater/rules/CheckVersion.py +++ b/ansiblelater/rules/CheckVersion.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -6,7 +8,8 @@ class CheckVersion(StandardBase): sid = "ANSIBLE9998" description = "Standards version should be pinned" helptext = "Standards version not set. Using latest standards version {version}" - types = ["task", "handler", "rolevars", "meta", "template", "file", "playbook"] + types: ClassVar[list[str] + ] = ["task", "handler", "rolevars", "meta", "template", "file", "playbook"] def check(self, candidate, settings): # noqa errors = [] diff --git a/ansiblelater/rules/CheckWhenFormat.py b/ansiblelater/rules/CheckWhenFormat.py index 74acca4..ad3a441 100644 --- a/ansiblelater/rules/CheckWhenFormat.py +++ b/ansiblelater/rules/CheckWhenFormat.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -10,7 +12,7 @@ class CheckWhenFormat(StandardBase): "should be removed from variable(s)" ) version = "0.2" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) diff --git a/ansiblelater/rules/CheckYamlColons.py b/ansiblelater/rules/CheckYamlColons.py index 8d48f8d..2b8daa9 100644 --- a/ansiblelater/rules/CheckYamlColons.py +++ b/ansiblelater/rules/CheckYamlColons.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -6,7 +8,9 @@ class CheckYamlColons(StandardBase): sid = "LINT0005" description = "YAML should use consistent number of spaces around colons" version = "0.1" - types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta"] + types: ClassVar[list[str]] = [ + "playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta" + ] def check(self, candidate, settings): options = f"rules: {{colons: {settings['yamllint']['colons']}}}" diff --git a/ansiblelater/rules/CheckYamlDocumentEnd.py b/ansiblelater/rules/CheckYamlDocumentEnd.py index c8de70d..945063f 100644 --- a/ansiblelater/rules/CheckYamlDocumentEnd.py +++ b/ansiblelater/rules/CheckYamlDocumentEnd.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -6,7 +8,9 @@ class CheckYamlDocumentEnd(StandardBase): sid = "LINT0009" description = "YAML should contain document end marker" version = "0.1" - types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta"] + types: ClassVar[list[str]] = [ + "playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta" + ] def check(self, candidate, settings): options = f"rules: {{document-end: {settings['yamllint']['document-end']}}}" diff --git a/ansiblelater/rules/CheckYamlDocumentStart.py b/ansiblelater/rules/CheckYamlDocumentStart.py index 89383ac..2f636ef 100644 --- a/ansiblelater/rules/CheckYamlDocumentStart.py +++ b/ansiblelater/rules/CheckYamlDocumentStart.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -6,7 +8,9 @@ class CheckYamlDocumentStart(StandardBase): sid = "LINT0004" description = "YAML should contain document start marker" version = "0.1" - types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta"] + types: ClassVar[list[str]] = [ + "playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta" + ] def check(self, candidate, settings): options = f"rules: {{document-start: {settings['yamllint']['document-start']}}}" diff --git a/ansiblelater/rules/CheckYamlEmptyLines.py b/ansiblelater/rules/CheckYamlEmptyLines.py index 4a9504d..34f9170 100644 --- a/ansiblelater/rules/CheckYamlEmptyLines.py +++ b/ansiblelater/rules/CheckYamlEmptyLines.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -6,7 +8,9 @@ class CheckYamlEmptyLines(StandardBase): sid = "LINT0001" description = "YAML should not contain unnecessarily empty lines" version = "0.1" - types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta"] + types: ClassVar[list[str]] = [ + "playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta" + ] def check(self, candidate, settings): options = f"rules: {{empty-lines: {settings['yamllint']['empty-lines']}}}" diff --git a/ansiblelater/rules/CheckYamlFile.py b/ansiblelater/rules/CheckYamlFile.py index dcae58d..636edab 100644 --- a/ansiblelater/rules/CheckYamlFile.py +++ b/ansiblelater/rules/CheckYamlFile.py @@ -1,4 +1,5 @@ import os +from typing import ClassVar from ansiblelater.standard import StandardBase @@ -9,7 +10,7 @@ class CheckYamlFile(StandardBase): description = "Roles file should be in yaml format" helptext = "file does not have a .yml extension" version = "0.1" - types = ["playbook", "task", "handler"] + types: ClassVar[list[str]] = ["playbook", "task", "handler"] def check(self, candidate, settings): errors = [] diff --git a/ansiblelater/rules/CheckYamlHasContent.py b/ansiblelater/rules/CheckYamlHasContent.py index 1e489d9..5c71799 100644 --- a/ansiblelater/rules/CheckYamlHasContent.py +++ b/ansiblelater/rules/CheckYamlHasContent.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -7,7 +9,7 @@ class CheckYamlHasContent(StandardBase): description = "Files should contain useful content" helptext = "the file appears to have no useful content" version = "0.1" - types = ["playbook", "task", "handler", "rolevars", "defaults", "meta"] + types: ClassVar[list[str]] = ["playbook", "task", "handler", "rolevars", "defaults", "meta"] def check(self, candidate, settings): yamllines, errors = self.get_normalized_yaml(candidate, settings) diff --git a/ansiblelater/rules/CheckYamlHyphens.py b/ansiblelater/rules/CheckYamlHyphens.py index 679bf74..bb58679 100644 --- a/ansiblelater/rules/CheckYamlHyphens.py +++ b/ansiblelater/rules/CheckYamlHyphens.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -6,7 +8,9 @@ class CheckYamlHyphens(StandardBase): sid = "LINT0003" description = "YAML should use consistent number of spaces after hyphens" version = "0.1" - types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta"] + types: ClassVar[list[str]] = [ + "playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta" + ] def check(self, candidate, settings): options = f"rules: {{hyphens: {settings['yamllint']['hyphens']}}}" diff --git a/ansiblelater/rules/CheckYamlIndent.py b/ansiblelater/rules/CheckYamlIndent.py index 79bf15e..b2b0d19 100644 --- a/ansiblelater/rules/CheckYamlIndent.py +++ b/ansiblelater/rules/CheckYamlIndent.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from ansiblelater.standard import StandardBase @@ -6,7 +8,9 @@ class CheckYamlIndent(StandardBase): sid = "LINT0002" description = "YAML should not contain unnecessarily empty lines" version = "0.1" - types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta"] + types: ClassVar[list[str]] = [ + "playbook", "task", "handler", "rolevars", "hostvars", "groupvars", "meta" + ] def check(self, candidate, settings): options = f"rules: {{document-start: {settings['yamllint']['document-start']}}}" diff --git a/ansiblelater/standard.py b/ansiblelater/standard.py index 4a66bc7..f9760bf 100644 --- a/ansiblelater/standard.py +++ b/ansiblelater/standard.py @@ -304,7 +304,7 @@ class StandardBase(metaclass=StandardExtendedMeta): return "\n".join([f"{self.candidate}:{error}" for error in self.errors]) -class StandardLoader(): +class StandardLoader: def __init__(self, source): self.rules = [] diff --git a/ansiblelater/utils/__init__.py b/ansiblelater/utils/__init__.py index dac16f7..1f804b2 100644 --- a/ansiblelater/utils/__init__.py +++ b/ansiblelater/utils/__init__.py @@ -5,6 +5,7 @@ import re import sys from contextlib import suppress from distutils.version import LooseVersion +from typing import ClassVar import yaml @@ -114,7 +115,7 @@ def sysexit_with_message(msg, code=1): class Singleton(type): """Meta singleton class.""" - _instances = {} + _instances: ClassVar[dict] = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: diff --git a/ansiblelater/utils/yamlhelper.py b/ansiblelater/utils/yamlhelper.py index 05207a9..3807ac2 100644 --- a/ansiblelater/utils/yamlhelper.py +++ b/ansiblelater/utils/yamlhelper.py @@ -209,11 +209,10 @@ def template(basedir, value, variables, fail_on_undefined=False, **kwargs): # Hack to skip the following exception when using to_json filter on a variable. # I guess the filter doesn't like empty vars... with suppress(AnsibleError, ValueError): - value = ansible_template( + return ansible_template( os.path.abspath(basedir), value, variables, **dict(kwargs, fail_on_undefined=fail_on_undefined) ) - return value def play_children(basedir, item, parent_type): @@ -373,8 +372,7 @@ def rolename(filepath): if idx < 0: return "" role = filepath[idx + 6:] - role = role[:role.find("/")] - return role + return role[:role.find("/")] def _kv_to_dict(v):