From 27b168f7e15fa1b8db00b3d407f0d60510863386 Mon Sep 17 00:00:00 2001 From: Shatakshi Mishra Date: Thu, 4 May 2023 14:30:11 +0530 Subject: [PATCH] ruff: Address FBT002 (#3389) Co-authored-by: Ajinkya Udgirkar --- pyproject.toml | 3 ++- src/ansiblelint/app.py | 7 ++++++- src/ansiblelint/cli.py | 1 + src/ansiblelint/file_utils.py | 4 ++-- src/ansiblelint/generate_docs.py | 2 +- src/ansiblelint/rules/__init__.py | 3 ++- src/ansiblelint/rules/jinja.py | 8 ++++---- src/ansiblelint/utils.py | 9 ++++++++- src/ansiblelint/yaml_utils.py | 10 +++++++--- 9 files changed, 33 insertions(+), 14 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bd30a99c32..5b73aaaa3b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", ] diff --git a/src/ansiblelint/app.py b/src/ansiblelint/app.py index c86b87c80d..3755f941a9 100644 --- a/src/ansiblelint/app.py +++ b/src/ansiblelint/app.py @@ -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. diff --git a/src/ansiblelint/cli.py b/src/ansiblelint/cli.py index f98a263999..a6983511d3 100644 --- a/src/ansiblelint/cli.py +++ b/src/ansiblelint/cli.py @@ -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, diff --git a/src/ansiblelint/file_utils.py b/src/ansiblelint/file_utils.py index 3298719918..0b68becd83 100644 --- a/src/ansiblelint/file_utils.py +++ b/src/ansiblelint/file_utils.py @@ -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 @@ -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``). diff --git a/src/ansiblelint/generate_docs.py b/src/ansiblelint/generate_docs.py index 1ef02af99b..1498a67790 100644 --- a/src/ansiblelint/generate_docs.py +++ b/src/ansiblelint/generate_docs.py @@ -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 = "" diff --git a/src/ansiblelint/rules/__init__.py b/src/ansiblelint/rules/__init__.py index dd9731082b..5625c1b283 100644 --- a/src/ansiblelint/rules/__init__.py +++ b/src/ansiblelint/rules/__init__.py @@ -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.""" @@ -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 diff --git a/src/ansiblelint/rules/jinja.py b/src/ansiblelint/rules/jinja.py index 6820aae6ca..8a22eb4bc1 100644 --- a/src/ansiblelint/rules/jinja.py +++ b/src/ansiblelint/rules/jinja.py @@ -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 @@ -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 @@ -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 diff --git a/src/ansiblelint/utils.py b/src/ansiblelint/utils.py index e5464cb0d6..598c1c2132 100644 --- a/src/ansiblelint/utils.py +++ b/src/ansiblelint/utils.py @@ -305,6 +305,7 @@ def template( basedir: Path, value: Any, variables: Any, + *, fail_on_error: bool = False, fail_on_undefined: bool = False, **kwargs: str, @@ -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.""" @@ -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: @@ -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) diff --git a/src/ansiblelint/yaml_utils.py b/src/ansiblelint/yaml_utils.py index 230ac10fb4..539b7758df 100644 --- a/src/ansiblelint/yaml_utils.py +++ b/src/ansiblelint/yaml_utils.py @@ -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 @@ -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.