Skip to content

Commit

Permalink
Merge branch 'main' into utilize-create-matcherror
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Nov 22, 2024
2 parents fe6e7eb + 0ff4ee7 commit b7368ff
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 82 deletions.
23 changes: 10 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,18 @@ examples/playbooks/vars/strings.transformed.yml
examples/playbooks/vars/transform_nested_data.yml

# other
.cache
*.tar.gz
*.tmp.*
.DS_Store
.vscode
.cache
.envrc
.idea
src/ansiblelint/_version.py
*.tar.gz
.pytest_cache
test/eco/CODENOTIFY.html
test/eco
test/schemas/node_modules
test/local-content
.envrc
# collections
# !/collections
site
.vscode/launch.json
_readthedocs
*.tmp.*
coverage.lcov
site
src/ansiblelint/_version.py
test/eco
test/local-content
test/schemas/node_modules
2 changes: 2 additions & 0 deletions .taplo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[formatting]
array_trailing_comma = false
22 changes: 20 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# cspell: ignore FURB

[build-system]
build-backend = "setuptools.build_meta"
requires = [
Expand Down Expand Up @@ -222,11 +224,13 @@ cache-dir = "./.cache/.ruff"
fix = true
# Same as Black.
line-length = 88
preview = true
target-version = "py310"

[tool.ruff.lint]
ignore = [
"COM812", # conflicts with ISC001 on format
"CPY001", # missing-copyright-notice
"D203", # incompatible with D211
"D213", # incompatible with D212
"E501", # we use black
Expand All @@ -245,7 +249,21 @@ ignore = [
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
"PERF203",
"PD011", # We are not using pandas, any .values attributes are unrelated
"PLW0603" # global lock file in cache dir
"PLW0603", # global lock file in cache dir
# part of preview rules:
"B909", # raise-missing-from
"DOC201", # docstring-missing-returns
"DOC402", # docstring-missing-summary
"DOC501", # docstring-missing-exception
"FURB101",
"FURB103",
"FURB110",
"FURB113",
"FURB118",
"PLC0415",
"PLC2701",
"PLW1641",
"S404"
]
select = ["ALL"]

Expand All @@ -269,7 +287,7 @@ max-complexity = 20
"src/ansiblelint/{utils,file_utils,runner,loaders,constants,config,cli,_mockings}.py" = [
"PTH"
]
"test/**/*.py" = ["S"]
"test/**/*.py" = ["DOC201", "DOC501", "PLC2701", "S"]

[tool.ruff.lint.pydocstyle]
convention = "google"
Expand Down
7 changes: 3 additions & 4 deletions src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,8 @@ def report_outcome(
os.environ.get("ANSIBLE_LINT_IGNORE_FILE", IGNORE_FILE.default),
)
console_stderr.print(f"Writing ignore file to {ignore_file_path}")
lines = set()
for rule in result.matches:
lines.add(f"{rule.filename} {rule.tag}\n")
lines: set[str] = set()
lines.update(f"{rule.filename} {rule.tag}\n" for rule in result.matches)
with ignore_file_path.open("w", encoding="utf-8") as ignore_file:
ignore_file.write(
"# This file contains ignores rule violations for ansible-lint\n",
Expand Down Expand Up @@ -330,7 +329,7 @@ def report_summary( # pylint: disable=too-many-locals # noqa: C901
for tag, stats in summary.tag_stats.items():
table.add_row(
str(stats.count),
f"[link={RULE_DOC_URL}{ tag.split('[')[0] }]{escape(tag)}[/link]",
f"[link={RULE_DOC_URL}{tag.split('[')[0]}]{escape(tag)}[/link]",
stats.profile,
f"{', '.join(stats.associated_tags)}{' (warning)' if stats.warning else ''}",
style="yellow" if stats.warning else "red",
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def __init__(
self.file = NamedTemporaryFile( # noqa: SIM115
mode="w+",
suffix="playbook.yml",
encoding="utf-8",
)
self.filename = self.file.name
self._content = sys.stdin.read()
Expand Down Expand Up @@ -479,7 +480,7 @@ def discover_lintables(options: Options) -> list[str]:

def strip_dotslash_prefix(fname: str) -> str:
"""Remove ./ leading from filenames."""
return fname[2:] if fname.startswith("./") else fname
return fname.removeprefix("./")


def find_project_root(
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def load_ignore_txt(filepath: Path | None = None) -> dict[str, set[str]]:


__all__ = [
"IGNORE_FILE",
"YAMLError",
"load_ignore_txt",
"yaml_from_file",
"yaml_load",
"yaml_load_safe",
"YAMLError",
"IGNORE_FILE",
]
3 changes: 1 addition & 2 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,7 @@ def known_tags(self) -> list[str]:
tags = set()
for rule in self.rules:
tags.add(rule.id)
for tag in rule.tags:
tags.add(tag)
tags.update(rule.tags)
return sorted(tags)

def list_tags(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/inline_env_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def matchtask(
task: Task,
file: Lintable | None = None,
) -> bool | str:
if task["action"]["__ansible_module__"] in ["command"]:
if task["action"]["__ansible_module__"] == "command":
first_cmd_arg = get_first_cmd_arg(task)
if not first_cmd_arg:
return False
Expand Down
6 changes: 2 additions & 4 deletions src/ansiblelint/rules/no_same_owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ def handle_synchronize(task: Any, action: dict[str, Any]) -> bool:
def handle_unarchive(task: Any, action: dict[str, Any]) -> bool:
"""Process unarchive task."""
delegate_to = task.get("delegate_to")
if (
delegate_to == "localhost"
or delegate_to != "localhost"
and not action.get("remote_src")
if delegate_to == "localhost" or (
delegate_to != "localhost" and not action.get("remote_src")
):
src = action.get("src")
if not isinstance(src, str):
Expand Down
3 changes: 1 addition & 2 deletions src/ansiblelint/rules/yaml_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ def _combine_skip_rules(data: Any) -> set[str]:
result = set(data.get(SKIPPED_RULES_KEY, []))
tags = data.get("tags", [])
if tags and (
isinstance(tags, Iterable)
and "skip_ansible_lint" in tags
(isinstance(tags, Iterable) and "skip_ansible_lint" in tags)
or tags == "skip_ansible_lint"
):
result.add("skip_ansible_lint")
Expand Down
2 changes: 2 additions & 0 deletions src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ def _get_ansible_syntax_check_matches(
mode="w",
suffix=".yml",
prefix="play",
encoding="utf-8",
)
fh.write(playbook_text)
fh.flush()
Expand Down Expand Up @@ -598,6 +599,7 @@ def plugin_children(self, lintable: Lintable) -> list[Lintable]:
examples.file = NamedTemporaryFile( # noqa: SIM115
mode="w+",
suffix=f"_{lintable.path.name}.yaml",
encoding="utf-8",
)
examples.file.write(content)
examples.file.flush()
Expand Down
4 changes: 3 additions & 1 deletion src/ansiblelint/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def run_playbook(
prefix: str = "playbook",
) -> list[MatchError]:
"""Lints received text as a playbook."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".yml", prefix=prefix) as fh:
with tempfile.NamedTemporaryFile(
mode="w", suffix=".yml", prefix=prefix, encoding="utf-8"
) as fh:
fh.write(playbook_text)
fh.flush()
results = self._call_runner(Path(fh.name))
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ def is_handler(self) -> bool:
if file_name:
paths = file_name.split("/")
is_handler_file = "handlers" in paths
return is_handler_file if is_handler_file else ".handlers[" in self.position
return is_handler_file or ".handlers[" in self.position

def __repr__(self) -> str:
"""Return a string representation of the task."""
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ def choose_scalar_style(self) -> Any:
"""Select how to quote scalars if needed."""
style = super().choose_scalar_style()
if (
style == ""
style == "" # noqa: PLC1901
and self.event.value.startswith("0")
and len(self.event.value) > 1
):
Expand Down
2 changes: 0 additions & 2 deletions test/rules/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
"""Test rules resources."""

__all__ = ["ematcher", "raw_task", "unset_variable_matcher"]
86 changes: 43 additions & 43 deletions test/schemas/src/rebuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,49 @@
play_keywords = list(
filter(
None,
"""\
any_errors_fatal
become
become_exe
become_flags
become_method
become_user
check_mode
collections
connection
debugger
diff
environment
fact_path
force_handlers
gather_facts
gather_subset
gather_timeout
handlers
hosts
ignore_errors
ignore_unreachable
max_fail_percentage
module_defaults
name
no_log
order
port
post_tasks
pre_tasks
remote_user
roles
run_once
serial
strategy
tags
tasks
throttle
timeout
vars
vars_files
vars_prompt
""".split(),
[
"any_errors_fatal",
"become",
"become_exe",
"become_flags",
"become_method",
"become_user",
"check_mode",
"collections",
"connection",
"debugger",
"diff",
"environment",
"fact_path",
"force_handlers",
"gather_facts",
"gather_subset",
"gather_timeout",
"handlers",
"hosts",
"ignore_errors",
"ignore_unreachable",
"max_fail_percentage",
"module_defaults",
"name",
"no_log",
"order",
"port",
"post_tasks",
"pre_tasks",
"remote_user",
"roles",
"run_once",
"serial",
"strategy",
"tags",
"tasks",
"throttle",
"timeout",
"vars",
"vars_files",
"vars_prompt",
],
),
)

Expand Down
4 changes: 3 additions & 1 deletion test/test_formatter_sarif.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ def test_sarif_parsable_ignored() -> None:
)
def test_sarif_file(file: str, return_code: int) -> None:
"""Test ability to dump sarif file (--sarif-file)."""
with NamedTemporaryFile(mode="w", suffix=".sarif", prefix="output") as output_file:
with NamedTemporaryFile(
mode="w", suffix=".sarif", prefix="output", encoding="utf-8"
) as output_file:
cmd = [
sys.executable,
"-m",
Expand Down
4 changes: 2 additions & 2 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_get_version_warning(
def test_get_version_warning_no_pip(mocker: MockerFixture) -> None:
"""Test that we do not display any message if install method is not pip."""
mocker.patch("ansiblelint.config.guess_install_method", return_value="")
assert get_version_warning() == ""
assert get_version_warning() == "" # noqa: PLC1901


def test_get_version_warning_remote_disconnect(mocker: MockerFixture) -> None:
Expand All @@ -109,7 +109,7 @@ def test_get_version_warning_offline(mocker: MockerFixture) -> None:
# ensures a real cache_file is not loaded
mocker.patch("ansiblelint.config.CACHE_DIR", Path(temporary_directory))
options.offline = True
assert get_version_warning() == ""
assert get_version_warning() == "" # noqa: PLC1901


@pytest.mark.parametrize(
Expand Down

0 comments on commit b7368ff

Please sign in to comment.