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

ruff: Address FBT002 #3389

Merged
merged 3 commits into from May 4, 2023
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
3 changes: 2 additions & 1 deletion pyproject.toml
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
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
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
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
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
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
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
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
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