Skip to content

Commit

Permalink
Make file argument optional to BaseRule.matchtask method and revert
Browse files Browse the repository at this point in the history
change to BaseRule.match method

Signed-Off-By: Satoru SATOH <satoru.satoh@gmail.com>
  • Loading branch information
ssato committed Apr 20, 2021
1 parent 44e5cd1 commit 960687f
Show file tree
Hide file tree
Showing 24 changed files with 47 additions and 41 deletions.
5 changes: 3 additions & 2 deletions src/ansiblelint/_internal/rules.py
Expand Up @@ -2,6 +2,7 @@
from typing import TYPE_CHECKING, Any, Dict, List, Union

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.constants import odict
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
Expand Down Expand Up @@ -44,7 +45,7 @@ def matchlines(self, file: "Lintable") -> List["MatchError"]:
return []

def matchtask(
self, file: "Lintable", task: Dict[str, Any]
self, task: Dict[str, Any], file: "Optional[Lintable]" = None
) -> Union[bool, str]:
"""Confirm if current rule is matching a specific task."""
return False
Expand All @@ -71,7 +72,7 @@ def verbose(self) -> str:
"""Return a verbose representation of the rule."""
return self.id + ": " + self.shortdesc + "\n " + self.description

def match(self, file: "Lintable", line: str) -> Union[bool, str]:
def match(self, line: str) -> Union[bool, str]:
"""Confirm if current rule matches the given string."""
return False

Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/CommandHasChangesCheckRule.py
Expand Up @@ -23,6 +23,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -42,7 +43,7 @@ class CommandHasChangesCheckRule(AnsibleLintRule):
_commands = ['command', 'shell', 'raw']

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
if task["__ansible_action_type__"] == 'task':
if task["action"]["__ansible_module__"] in self._commands:
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/CommandsInsteadOfArgumentsRule.py
Expand Up @@ -25,6 +25,7 @@
from ansiblelint.utils import convert_to_boolean, get_first_cmd_arg

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -51,7 +52,7 @@ class CommandsInsteadOfArgumentsRule(AnsibleLintRule):
}

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
if task["action"]["__ansible_module__"] in self._commands:
first_cmd_arg = get_first_cmd_arg(task)
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/CommandsInsteadOfModulesRule.py
Expand Up @@ -25,6 +25,7 @@
from ansiblelint.utils import convert_to_boolean, get_first_cmd_arg

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand Down Expand Up @@ -63,7 +64,7 @@ class CommandsInsteadOfModulesRule(AnsibleLintRule):
}

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
if task['action']['__ansible_module__'] not in self._commands:
return False
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/ComparisonToEmptyStringRule.py
Expand Up @@ -9,6 +9,7 @@
from ansiblelint.utils import nested_items

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -26,7 +27,7 @@ class ComparisonToEmptyStringRule(AnsibleLintRule):
empty_string_compare = re.compile("[=!]= ?(\"{2}|'{2})")

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
for k, v, _ in nested_items(task):
if k == 'when':
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/ComparisonToLiteralBoolRule.py
Expand Up @@ -8,6 +8,7 @@
from ansiblelint.utils import nested_items

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -25,7 +26,7 @@ class ComparisonToLiteralBoolRule(AnsibleLintRule):
literal_bool_compare = re.compile("[=!]= ?(True|true|False|false)")

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
for k, v, _ in nested_items(task):
if k == 'when':
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/DeprecatedModuleRule.py
Expand Up @@ -5,6 +5,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand Down Expand Up @@ -64,7 +65,7 @@ class DeprecatedModuleRule(AnsibleLintRule):
]

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
module = task["action"]["__ansible_module__"]
if module in self._modules:
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/EnvVarsInCommandRule.py
Expand Up @@ -24,6 +24,7 @@
from ansiblelint.utils import FILENAME_KEY, LINE_NUMBER_KEY, get_first_cmd_arg

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand Down Expand Up @@ -55,7 +56,7 @@ class EnvVarsInCommandRule(AnsibleLintRule):
]

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
if task["action"]["__ansible_module__"] in ['command']:
first_cmd_arg = get_first_cmd_arg(task)
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/GitHasVersionRule.py
Expand Up @@ -23,6 +23,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -38,7 +39,7 @@ class GitHasVersionRule(AnsibleLintRule):
version_added = 'historic'

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
return bool(
task['action']['__ansible_module__'] == 'git'
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/MercurialHasRevisionRule.py
Expand Up @@ -23,6 +23,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -38,7 +39,7 @@ class MercurialHasRevisionRule(AnsibleLintRule):
version_added = 'historic'

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
return bool(
task['action']['__ansible_module__'] == 'hg'
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/MissingFilePermissionsRule.py
Expand Up @@ -22,6 +22,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand Down Expand Up @@ -65,7 +66,7 @@ class MissingFilePermissionsRule(AnsibleLintRule):
}

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
module = task["action"]["__ansible_module__"]
mode = task['action'].get('mode', None)
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/NestedJinjaRule.py
Expand Up @@ -27,6 +27,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -46,7 +47,7 @@ class NestedJinjaRule(AnsibleLintRule):
pattern = re.compile(r"{{(?:[^{}]*)?[^'\"]{{")

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
command = "".join(
str(value)
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/NoFormattingInWhenRule.py
Expand Up @@ -3,6 +3,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.constants import odict
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
Expand Down Expand Up @@ -31,7 +32,7 @@ def matchplay(
if 'roles' not in data or data['roles'] is None:
return errors
for role in data['roles']:
if self.matchtask(file, role):
if self.matchtask(role, file=file):
errors.append(self.create_matcherror(details=str({'when': role})))
if isinstance(data, list):
for play_item in data:
Expand All @@ -41,6 +42,6 @@ def matchplay(
return errors

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
return 'when' in task and not self._is_valid(task['when'])
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/NoTabsRule.py
Expand Up @@ -7,6 +7,7 @@
from ansiblelint.utils import nested_items

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -25,7 +26,7 @@ class NoTabsRule(AnsibleLintRule):
]

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
for k, v, parent in nested_items(task):
if isinstance(k, str) and '\t' in k:
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/PackageIsNotLatestRule.py
Expand Up @@ -23,6 +23,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand Down Expand Up @@ -66,7 +67,7 @@ class PackageIsNotLatestRule(AnsibleLintRule):
]

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
return (
task['action']['__ansible_module__'] in self._package_managers
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/ShellWithoutPipefail.py
Expand Up @@ -5,6 +5,7 @@
from ansiblelint.utils import convert_to_boolean

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -27,7 +28,7 @@ class ShellWithoutPipefail(AnsibleLintRule):
_pipe_re = re.compile(r"(?<!\|)\|(?!\|)")

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
if task["__ansible_action_type__"] != "task":
return False
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/TaskHasNameRule.py
Expand Up @@ -23,6 +23,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -38,6 +39,6 @@ class TaskHasNameRule(AnsibleLintRule):
version_added = 'historic'

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
return not task.get('name')
7 changes: 1 addition & 6 deletions src/ansiblelint/rules/TaskNoLocalAction.py
@@ -1,12 +1,7 @@
# Copyright (c) 2016, Tsukinowa Inc. <info@tsukinowa.jp>
# Copyright (c) 2018, Ansible Project
import typing

from ansiblelint.rules import AnsibleLintRule

if typing.TYPE_CHECKING:
from ansiblelint.file_utils import Lintable


class TaskNoLocalAction(AnsibleLintRule):
id = 'deprecated-local-action'
Expand All @@ -16,7 +11,7 @@ class TaskNoLocalAction(AnsibleLintRule):
tags = ['deprecations']
version_added = 'v4.0.0'

def match(self, file: 'Lintable', line: str) -> bool:
def match(self, line: str) -> bool:
if 'local_action' in line:
return True
return False
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/UseCommandInsteadOfShellRule.py
Expand Up @@ -23,6 +23,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -39,7 +40,7 @@ class UseCommandInsteadOfShellRule(AnsibleLintRule):
version_added = 'historic'

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
# Use unjinja so that we don't match on jinja filters
# rather than pipes
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/UseHandlerRatherThanWhenChangedRule.py
Expand Up @@ -24,6 +24,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand Down Expand Up @@ -56,7 +57,7 @@ class UseHandlerRatherThanWhenChangedRule(AnsibleLintRule):
version_added = 'historic'

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
if task["__ansible_action_type__"] != 'task':
return False
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/UsingBareVariablesIsDeprecatedRule.py
Expand Up @@ -25,6 +25,7 @@
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -44,7 +45,7 @@ class UsingBareVariablesIsDeprecatedRule(AnsibleLintRule):
_glob = re.compile('[][*?]')

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
loop_type = next((key for key in task if key.startswith("with_")), None)
if loop_type:
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/VariableHasSpacesRule.py
Expand Up @@ -9,6 +9,7 @@
from ansiblelint.utils import nested_items

if TYPE_CHECKING:
from typing import Optional
from ansiblelint.file_utils import Lintable


Expand All @@ -25,7 +26,7 @@ class VariableHasSpacesRule(AnsibleLintRule):
exclude_json_re = re.compile(r"[^{]{'\w+': ?[^{]{.*?}}")

def matchtask(
self, file: 'Lintable', task: Dict[str, Any]
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:
for k, v, _ in nested_items(task):
if isinstance(v, str):
Expand Down

0 comments on commit 960687f

Please sign in to comment.