fix linting

This commit is contained in:
Robert Kaussow 2023-06-28 08:19:08 +02:00
parent d2eb295bd6
commit f3646adf15
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
42 changed files with 126 additions and 47 deletions

View File

@ -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})"

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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,

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -1,5 +1,7 @@
# Copyright (c) 2016, Tsukinowa Inc. <info@tsukinowa.jp>
# 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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -1,5 +1,7 @@
# Copyright (c) 2016, Tsukinowa Inc. <info@tsukinowa.jp>
# 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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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 = []

View File

@ -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)

View File

@ -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']}}}"

View File

@ -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']}}}"

View File

@ -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']}}}"

View File

@ -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']}}}"

View File

@ -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 = []

View File

@ -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)

View File

@ -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']}}}"

View File

@ -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']}}}"

View File

@ -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 = []

View File

@ -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:

View File

@ -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):