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

Apply Python 3.10 code changes #4012

Merged
merged 1 commit into from
Feb 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ repos:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/psf/black
rev: 23.12.1
rev: 24.1.1
hooks:
- id: black
language_version: python3
Expand Down
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""PyTest Fixtures."""

import importlib
import os
import platform
Expand Down
1 change: 0 additions & 1 deletion examples/.collection/plugins/modules/alpha.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""An ansible test module."""


DOCUMENTATION = """
module: mod_1
author:
Expand Down
1 change: 0 additions & 1 deletion examples/.collection/plugins/modules/deep/beta.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""An ansible test module."""


DOCUMENTATION = """
module: mod_2
author:
Expand Down
1 change: 1 addition & 0 deletions examples/rules/task_has_tag.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Example implementation of a rule requiring tasks to have tags set."""

from __future__ import annotations

from typing import TYPE_CHECKING
Expand Down
1 change: 1 addition & 0 deletions plugins/modules/fake_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

This is used to test ability to detect and use custom modules.
"""

from ansible.module_utils.basic import AnsibleModule

EXAMPLES = r"""
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ repository = "https://github.com/ansible/ansible-lint"
changelog = "https://github.com/ansible/ansible-lint/releases"

[tool.black]
target-version = ["py39"]
target-version = ["py310"]

[tool.codespell]
skip = ".tox,.mypy_cache,build,.git,.eggs,pip-wheel-metadata"
Expand Down Expand Up @@ -251,7 +251,7 @@ ignore = [
"PD011" # We are not using pandas, any .values attributes are unrelated
]
select = ["ALL"]
target-version = "py39"
target-version = "py310"
# Same as Black.
line-length = 88

Expand Down
10 changes: 3 additions & 7 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import site
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, TextIO
from typing import TYPE_CHECKING, Any, TextIO

from ansible_compat.prerun import get_cache_dir
from filelock import FileLock, Timeout
Expand Down Expand Up @@ -72,6 +72,8 @@
if TYPE_CHECKING:
# RulesCollection must be imported lazily or ansible gets imported too early.

from collections.abc import Callable

from ansiblelint.rules import RulesCollection
from ansiblelint.runner import LintResult

Expand Down Expand Up @@ -205,12 +207,6 @@ def _do_transform(result: LintResult, opts: Options) -> None:

def support_banner() -> None:
"""Display support banner when running on unsupported platform."""
if sys.version_info < (3, 10, 0): # pragma: no cover
prefix = "::warning::" if "GITHUB_ACTION" in os.environ else "WARNING: "
console_stderr.print(
f"{prefix}ansible-lint is no longer tested under Python {sys.version_info.major}.{sys.version_info.minor} and will soon require 3.10. Do not report bugs for this version.",
style="bold red",
)


def fix(runtime_options: Options, result: LintResult, rules: RulesCollection) -> None:
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/_internal/rules.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Internally used rule classes."""

from __future__ import annotations

import inspect
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/_mockings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utilities for mocking ansible modules and roles."""

from __future__ import annotations

import contextlib
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Application."""

from __future__ import annotations

import copy
Expand Down Expand Up @@ -58,7 +59,7 @@ def render_matches(self, matches: list[MatchError]) -> None:

if isinstance(
self.formatter,
(formatters.CodeclimateJSONFormatter, formatters.SarifFormatter),
formatters.CodeclimateJSONFormatter | formatters.SarifFormatter,
):
# If formatter CodeclimateJSONFormatter or SarifFormatter is chosen,
# then print only the matches in JSON
Expand Down
7 changes: 4 additions & 3 deletions src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""CLI parser setup and helpers."""

from __future__ import annotations

import argparse
Expand All @@ -7,7 +8,7 @@
import sys
from argparse import Namespace
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable
from typing import TYPE_CHECKING, Any

from ansiblelint.config import (
DEFAULT_KINDS,
Expand All @@ -29,7 +30,7 @@
from ansiblelint.yaml_utils import clean_json

if TYPE_CHECKING:
from collections.abc import Sequence
from collections.abc import Callable, Sequence


_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -134,7 +135,7 @@ def __call__(
values: str | Sequence[Any] | None,
option_string: str | None = None,
) -> None:
if isinstance(values, (str, Path)):
if isinstance(values, str | Path):
values = [values]
if values:
normalized_values = [
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/color.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Console coloring and terminal support."""

from __future__ import annotations

from typing import Any
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Store configuration options as a singleton."""

from __future__ import annotations

import json
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Constants used by AnsibleLint."""

from enum import Enum
from pathlib import Path
from typing import Literal
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Exceptions and error representations."""

from __future__ import annotations

import functools
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utility functions related to file operations."""

from __future__ import annotations

import copy
Expand Down
7 changes: 4 additions & 3 deletions src/ansiblelint/formatters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Output formatters."""

from __future__ import annotations

import hashlib
Expand Down Expand Up @@ -288,9 +289,9 @@ def _to_sarif_result(self, match: MatchError) -> dict[str, Any]:
"ruleId": match.tag,
"level": self.get_sarif_result_severity_level(match),
"message": {
"text": str(match.details)
if str(match.details)
else str(match.message),
"text": (
str(match.details) if str(match.details) else str(match.message)
),
},
"locations": [
{
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/generate_docs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utils to generate rules documentation."""

import logging
from collections.abc import Iterable

Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/loaders.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utilities for loading various files."""

from __future__ import annotations

import logging
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utils related to logging."""

import logging
import time
from collections.abc import Iterator
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""All internal ansible-lint rules."""

from __future__ import annotations

import copy
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/args.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Rule definition to validate task options."""

from __future__ import annotations

import contextlib
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/avoid_implicit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of avoid-implicit rule."""

# https://github.com/ansible/ansible-lint/issues/2501
from __future__ import annotations

Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/command_instead_of_module.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of command-instead-of-module rule."""

# Copyright (c) 2013-2014 Will Thames <will@thames.id.au>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/command_instead_of_shell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of command-instead-of-shell rule."""

# Copyright (c) 2016 Will Thames <will@thames.id.au>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/complexity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of limiting number of tasks."""

from __future__ import annotations

import re
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Makes pytest fixtures available."""

# pylint: disable=wildcard-import,unused-wildcard-import
from ansiblelint.testing.fixtures import * # noqa: F403
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/deprecated_bare_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def matchtask(
# we just need to check that one variable, and not iterate over it like
# it's a list. Otherwise, loop through and check all items.
items = task[loop_type]
if not isinstance(items, (list, tuple)):
if not isinstance(items, list | tuple):
items = [items]
for var in items:
return self._matchvar(var, task, loop_type)
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/deprecated_local_action.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation for deprecated-local-action rule."""

# Copyright (c) 2016, Tsukinowa Inc. <info@tsukinowa.jp>
# Copyright (c) 2018, Ansible Project
from __future__ import annotations
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/deprecated_module.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of deprecated-module rule."""

# Copyright (c) 2018, Ansible Project

from __future__ import annotations
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/empty_string_compare.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of empty-string-compare rule."""

# Copyright (c) 2016, Will Thames and contributors
# Copyright (c) 2018, Ansible Project

Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/fqcn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Rule definition for usage of fully qualified collection names for builtins."""

from __future__ import annotations

import logging
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/galaxy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of GalaxyRule."""

from __future__ import annotations

import sys
Expand Down Expand Up @@ -167,7 +168,7 @@ def __lt__(self, other: Version) -> bool:
def _coerce(other: object) -> Version:
if isinstance(other, str):
other = Version(other)
if isinstance(other, (int, float)):
if isinstance(other, int | float):
other = Version(str(other))
if isinstance(other, Version):
return other
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/ignore_errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""IgnoreErrorsRule used with ansible-lint."""

from __future__ import annotations

import sys
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/inline_env_var.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of inside-env-var rule."""

# Copyright (c) 2016 Will Thames <will@thames.id.au>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/jinja.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Rule for checking content of jinja template strings."""

from __future__ import annotations

import logging
Expand Down Expand Up @@ -146,15 +147,15 @@ def matchtask(
)
if ignored_re.search(orig_exc_message) or isinstance(
orig_exc,
(AnsibleParserError, TypeError),
AnsibleParserError | TypeError,
):
# An unhandled exception occurred while running the lookup plugin 'template'. Error was a <class 'ansible.errors.AnsibleError'>, original message: the template file ... could not be found for the lookup. the template file ... could not be found for the lookup

# ansible@devel (2.14) new behavior:
# AnsibleError(TemplateSyntaxError): template error while templating string: Could not load "ipwrap": 'Invalid plugin FQCN (ansible.netcommon.ipwrap): unable to locate collection ansible.netcommon'. String: Foo {{ buildset_registry.host | ipwrap }}. Could not load "ipwrap": 'Invalid plugin FQCN (ansible.netcommon.ipwrap): unable to locate collection ansible.netcommon'
bypass = True
elif (
isinstance(orig_exc, (AnsibleError, TemplateSyntaxError))
isinstance(orig_exc, AnsibleError | TemplateSyntaxError)
and match
):
error = match.group("error")
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/key_order.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""All tasks should be have name come first."""

from __future__ import annotations

import functools
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/latest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of latest rule."""

from __future__ import annotations

from typing import TYPE_CHECKING
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/literal_compare.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of the literal-compare rule."""

# Copyright (c) 2016, Will Thames and contributors
# Copyright (c) 2018-2021, Ansible Project

Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/loop_var_prefix.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Optional Ansible-lint rule to enforce use of prefix on role loop vars."""

from __future__ import annotations

import re
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/meta_incorrect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of meta-incorrect rule."""

# Copyright (c) 2018, Ansible Project
from __future__ import annotations

Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/meta_no_tags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of meta-no-tags rule."""

from __future__ import annotations

import re
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/meta_runtime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of meta-runtime rule."""

from __future__ import annotations

import sys
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/meta_video_links.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of meta-video-links rule."""

# Copyright (c) 2018, Ansible Project
from __future__ import annotations

Expand Down
Loading
Loading