diff --git a/pyproject.toml b/pyproject.toml index 94471d7e..a03332e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", @@ -142,6 +141,9 @@ ignore = [ ] target-version = "py39" +[tool.ruff.pydocstyle] +convention = "pep257" + [tool.ruff.flake8-pytest-style] parametrize-values-type = "tuple" diff --git a/src/ansible_compat/config.py b/src/ansible_compat/config.py index 01b7a457..b08d25d0 100644 --- a/src/ansible_compat/config.py +++ b/src/ansible_compat/config.py @@ -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 @@ -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) diff --git a/src/ansible_compat/errors.py b/src/ansible_compat/errors.py index 43162a50..2ce74e71 100644 --- a/src/ansible_compat/errors.py +++ b/src/ansible_compat/errors.py @@ -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: """.""" diff --git a/src/ansible_compat/loaders.py b/src/ansible_compat/loaders.py index 174e214d..173a70bd 100644 --- a/src/ansible_compat/loaders.py +++ b/src/ansible_compat/loaders.py @@ -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 diff --git a/src/ansible_compat/runtime.py b/src/ansible_compat/runtime.py index 58d9ff7a..70d91495 100644 --- a/src/ansible_compat/runtime.py +++ b/src/ansible_compat/runtime.py @@ -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() @@ -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 @@ -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 = [ @@ -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: @@ -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), @@ -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): diff --git a/src/ansible_compat/schema.py b/src/ansible_compat/schema.py index da145564..97bdad3d 100644 --- a/src/ansible_compat/schema.py +++ b/src/ansible_compat/schema.py @@ -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: