Skip to content

Commit

Permalink
ruff: Address EM (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Apr 29, 2023
1 parent e16386d commit 4382c1b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ ignore = [
"ARG",
"B018", # Found useless expression. Either assign it to a variable or remove it.
"C901",
"EM",
"FBT001",
"FBT002", # Boolean default value in function definition
"PGH003",
Expand All @@ -142,6 +141,9 @@ ignore = [
]
target-version = "py39"

[tool.ruff.pydocstyle]
convention = "pep257"

[tool.ruff.flake8-pytest-style]
parametrize-values-type = "tuple"

Expand Down
10 changes: 3 additions & 7 deletions src/ansible_compat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ def parse_ansible_version(stdout: str) -> Version:
)
if match:
return Version(match.group("version"))
raise InvalidPrerequisiteError(
f"Unable to parse ansible cli version: {stdout}\nKeep in mind that only {ANSIBLE_MIN_VERSION } or newer are supported.",
)
msg = f"Unable to parse ansible cli version: {stdout}\nKeep in mind that only {ANSIBLE_MIN_VERSION } or newer are supported."
raise InvalidPrerequisiteError(msg)


@cache
Expand All @@ -61,10 +60,7 @@ def ansible_version(version: str = "") -> Version:
capture_output=True,
)
if proc.returncode != 0:
raise MissingAnsibleError(
"Unable to find a working copy of ansible executable.",
proc=proc,
)
raise MissingAnsibleError(proc=proc)

return parse_ansible_version(proc.stdout)

Expand Down
2 changes: 1 addition & 1 deletion src/ansible_compat/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MissingAnsibleError(AnsibleCompatError):

def __init__(
self,
message: Optional[str] = None,
message: Optional[str] = "Unable to find a working copy of ansible executable.",
proc: Optional[Any] = None,
) -> None:
"""."""
Expand Down
5 changes: 2 additions & 3 deletions src/ansible_compat/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def colpath_from_path(path: Path) -> str | None:
galaxy = yaml_from_file(galaxy_file)
for k in ("namespace", "name"):
if k not in galaxy:
raise InvalidPrerequisiteError(
f"{galaxy_file} is missing the following mandatory field {k}",
)
msg = f"{galaxy_file} is missing the following mandatory field {k}"
raise InvalidPrerequisiteError(msg)
return f"{galaxy['namespace']}/{galaxy['name']}"
return None
32 changes: 16 additions & 16 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ def __init__(
self.config = AnsibleConfig()

if not self.version_in_range(lower=min_required_version):
raise RuntimeError(
f"Found incompatible version of ansible runtime {self.version}, instead of {min_required_version} or newer.",
)
msg = f"Found incompatible version of ansible runtime {self.version}, instead of {min_required_version} or newer."
raise RuntimeError(msg)
if require_module:
self._ensure_module_available()

Expand All @@ -129,17 +128,15 @@ def _ensure_module_available(self) -> None:
ansible_release_module = importlib.import_module("ansible.release")

if ansible_release_module is None:
raise RuntimeError("Unable to find Ansible python module.")
msg = "Unable to find Ansible python module."
raise RuntimeError(msg)

ansible_module_version = packaging.version.parse(
ansible_release_module.__version__,
)
if ansible_module_version != self.version:
raise RuntimeError(
f"Ansible CLI ({self.version}) and python module"
f" ({ansible_module_version}) versions do not match. This "
"indicates a broken execution environment.",
)
msg = f"Ansible CLI ({self.version}) and python module ({ansible_module_version}) versions do not match. This indicates a broken execution environment."
raise RuntimeError(msg)

# For ansible 2.15+ we need to initialize the plugin loader
# https://github.com/ansible/ansible-lint/issues/2945
Expand Down Expand Up @@ -337,9 +334,8 @@ def install_requirements(
return
reqs_yaml = yaml_from_file(Path(requirement))
if not isinstance(reqs_yaml, (dict, list)):
raise InvalidPrerequisiteError(
f"{requirement} file is not a valid Ansible requirements file.",
)
msg = f"{requirement} file is not a valid Ansible requirements file."
raise InvalidPrerequisiteError(msg)

if isinstance(reqs_yaml, list) or "roles" in reqs_yaml:
cmd = [
Expand Down Expand Up @@ -483,14 +479,16 @@ def require_collection(
try:
ns, coll = name.split(".", 1)
except ValueError as exc:
msg = f"Invalid collection name supplied: {name}%s"
raise InvalidPrerequisiteError(
f"Invalid collection name supplied: {name}%s",
msg,
) from exc

paths: list[str] = self.config.collections_paths
if not paths or not isinstance(paths, list):
msg = f"Unable to determine ansible collection paths. ({paths})"
raise InvalidPrerequisiteError(
f"Unable to determine ansible collection paths. ({paths})",
msg,
)

if self.cache_dir:
Expand Down Expand Up @@ -538,7 +536,8 @@ def _prepare_ansible_paths(self) -> None:
roles_path: list[str] = self.config.default_roles_path.copy()
collections_path: list[str] = self.config.collections_paths.copy()
except AttributeError as exc:
raise RuntimeError("Unexpected ansible configuration") from exc
msg = "Unexpected ansible configuration"
raise RuntimeError(msg) from exc

alterations_list = [
(library_paths, "plugins/modules", True),
Expand Down Expand Up @@ -692,7 +691,8 @@ def _get_galaxy_role_ns(galaxy_infos: dict[str, Any]) -> str:
if len(role_namespace) == 0:
role_namespace = galaxy_infos.get("author", "")
if not isinstance(role_namespace, str):
raise AnsibleCompatError(f"Role namespace must be string, not {role_namespace}")
msg = f"Role namespace must be string, not {role_namespace}"
raise AnsibleCompatError(msg)
# if there's a space in the name space, it's likely author name
# and not the galaxy login, so act as if there was no namespace
if not role_namespace or re.match(r"^\w+ \w+", role_namespace):
Expand Down
3 changes: 2 additions & 1 deletion src/ansible_compat/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def validate(
schema = json.loads(schema)
try:
if not isinstance(schema, Mapping):
raise jsonschema.SchemaError("Invalid schema, must be a mapping")
msg = "Invalid schema, must be a mapping"
raise jsonschema.SchemaError(msg)
validator = validator_for(schema)
validator.check_schema(schema)
except jsonschema.SchemaError as exc:
Expand Down

0 comments on commit 4382c1b

Please sign in to comment.