Skip to content

Commit

Permalink
ruff: Address FBT002 (#3389)
Browse files Browse the repository at this point in the history
Co-authored-by: Ajinkya Udgirkar <ajinkyaudgirkar@gmail.com>
  • Loading branch information
shatakshiiii and audgirka authored May 4, 2023
1 parent e0d4e7d commit 27b168f
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 14 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ ignore = [
"ANN",
"ARG002", # Unused method argument (currently in too many places)
"D102", # Missing docstring in public method (currently in too many places)
"FBT",
"FBT001",
"FBT003",
"PLR",
"TRY",
]
Expand Down
7 changes: 6 additions & 1 deletion src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ def _get_matched_skippable_rules(
matched_rules.pop(rule_id)
return matched_rules

def report_outcome(self, result: LintResult, mark_as_success: bool = False) -> int:
def report_outcome(
self,
result: LintResult,
*,
mark_as_success: bool = False,
) -> int:
"""Display information about how to skip found rules.
Returns exit code, 2 if errors were found, 0 when only warnings were found.
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def __init__( # pylint: disable=too-many-arguments,redefined-builtin
default: Any = None,
type: Callable[[str], Any] | None = None, # noqa: A002
choices: list[Any] | None = None,
*,
required: bool = False,
help: str | None = None, # noqa: A002
metavar: str | None = None,
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def expand_paths_vars(paths: list[str]) -> list[str]:
return paths


def kind_from_path(path: Path, base: bool = False) -> FileType:
def kind_from_path(path: Path, *, base: bool = False) -> FileType:
"""Determine the file kind based on its name.
When called with base=True, it will return the base file type instead
Expand Down Expand Up @@ -316,7 +316,7 @@ def content(self) -> None:
"""Reset the internal content cache."""
self._content = None

def write(self, force: bool = False) -> None:
def write(self, *, force: bool = False) -> None:
"""Write the value of ``Lintable.content`` to disk.
This only writes to disk if the content has been updated (``Lintable.updated``).
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def rules_as_rich(rules: RulesCollection) -> Iterable[Table]:
yield table


def profiles_as_md(header: bool = False, docs_url: str = RULE_DOC_URL) -> str:
def profiles_as_md(*, header: bool = False, docs_url: str = RULE_DOC_URL) -> str:
"""Return markdown representation of supported profiles."""
result = ""

Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def __init__(
rulesdirs: list[str] | list[Path] | None = None,
options: Options = default_options,
profile_name: str | None = None,
*,
conditional: bool = True,
) -> None:
"""Initialize a RulesCollection instance."""
Expand Down Expand Up @@ -401,7 +402,7 @@ def __init__(
if profile_name and not (self.options.list_rules or self.options.list_tags):
filter_rules_with_profile(self.rules, profile_name)

def register(self, obj: AnsibleLintRule, conditional: bool = False) -> None:
def register(self, obj: AnsibleLintRule, *, conditional: bool = False) -> None:
"""Register a rule."""
# We skip opt-in rules which were not manually enabled.
# But we do include opt-in rules when listing all rules or tags
Expand Down
8 changes: 4 additions & 4 deletions src/ansiblelint/rules/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def check_whitespace( # noqa: max-complexity: 13
:returns: (string, string, string) reformatted text, detailed error, error tag
"""

def cook(value: str, implicit: bool = False) -> str:
def cook(value: str, *, implicit: bool = False) -> str:
"""Prepare an implicit string for jinja parsing when needed."""
if not implicit:
return value
Expand All @@ -265,7 +265,7 @@ def cook(value: str, implicit: bool = False) -> str:
return value
return f"{{{{ {value} }}}}"

def uncook(value: str, implicit: bool = False) -> str:
def uncook(value: str, *, implicit: bool = False) -> str:
"""Restore an string to original form when it was an implicit one."""
if not implicit:
return value
Expand Down Expand Up @@ -349,12 +349,12 @@ def uncook(value: str, implicit: bool = False) -> str:
# newlines, as we decided to not touch them yet.
# These both are documented as known limitations.
_logger.debug("Ignored jinja internal error %s", exc)
return uncook(text, implicit), "", "spacing"
return uncook(text, implicit=implicit), "", "spacing"

# finalize
reformatted = self.unlex(tokens)
failed = reformatted != text
reformatted = uncook(reformatted, implicit)
reformatted = uncook(reformatted, implicit=implicit)
details = (
f"Jinja2 template rewrite recommendation: `{reformatted}`."
if failed
Expand Down
9 changes: 8 additions & 1 deletion src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ def template(
basedir: Path,
value: Any,
variables: Any,
*,
fail_on_error: bool = False,
fail_on_undefined: bool = False,
**kwargs: str,
Expand Down Expand Up @@ -731,6 +732,7 @@ def task_to_str(task: dict[str, Any]) -> str:
def extract_from_list(
blocks: AnsibleBaseYAMLObject,
candidates: list[str],
*,
recursive: bool = False,
) -> list[Any]:
"""Get action tasks from block structures."""
Expand All @@ -742,7 +744,11 @@ def extract_from_list(
subresults = add_action_type(block[candidate], candidate)
if recursive:
subresults.extend(
extract_from_list(subresults, candidates, recursive),
extract_from_list(
subresults,
candidates,
recursive=recursive,
),
)
results.extend(subresults)
elif block[candidate] is not None:
Expand Down Expand Up @@ -909,6 +915,7 @@ def compose_node(parent: yaml.nodes.Node, index: int) -> yaml.nodes.Node:

def construct_mapping(
node: AnsibleBaseYAMLObject,
*,
deep: bool = False,
) -> AnsibleMapping:
mapping = AnsibleConstructor.construct_mapping(loader, node, deep=deep)
Expand Down
10 changes: 7 additions & 3 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,8 @@ def write_indicator(
self,
indicator: str, # ruamel.yaml typehint is wrong. This is a string.
need_whitespace: bool,
whitespace: bool = False,
indention: bool = False, # (sic) ruamel.yaml has this typo in their API
whitespace: bool = False, # noqa: FBT002
indention: bool = False, # (sic) ruamel.yaml has this typo in their API # noqa: FBT002
) -> None:
"""Make sure that flow maps get whitespace by the curly braces."""
# We try to go with one whitespace by the curly braces and adjust accordingly
Expand Down Expand Up @@ -670,7 +670,11 @@ def analyze_scalar(self, scalar: str) -> ScalarAnalysis:
return analysis

# comment is a CommentToken, not Any (Any is ruamel.yaml's lazy type hint).
def write_comment(self, comment: CommentToken, pre: bool = False) -> None:
def write_comment(
self,
comment: CommentToken,
pre: bool = False, # noqa: FBT002
) -> None:
"""Clean up extra new lines and spaces in comments.
ruamel.yaml treats new or empty lines as comments.
Expand Down

0 comments on commit 27b168f

Please sign in to comment.