From ad189e71510d138fbf385c1026925bec8a4972a0 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:57:08 +0200 Subject: [PATCH] FIX: remove package even if it has a version specifier --- pyproject.toml | 1 - .../check_dev_files/pixi/_update.py | 2 +- .../utilities/pyproject/setters.py | 46 ++++++++++++++++--- src/compwa_policy/utilities/python.py | 27 ----------- 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d6f1350f..a46369dd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,7 +36,6 @@ dev = [ "compwa-policy[test]", "labels", "sphinx-autobuild", - "tox >=1.9", # for skip_install, use_develop ] doc = [ "Sphinx", diff --git a/src/compwa_policy/check_dev_files/pixi/_update.py b/src/compwa_policy/check_dev_files/pixi/_update.py index eb8b008f..260c5584 100644 --- a/src/compwa_policy/check_dev_files/pixi/_update.py +++ b/src/compwa_policy/check_dev_files/pixi/_update.py @@ -17,7 +17,7 @@ Pyproject, complies_with_subset, ) -from compwa_policy.utilities.python import split_dependency_definition +from compwa_policy.utilities.pyproject.setters import split_dependency_definition from compwa_policy.utilities.readme import add_badge from compwa_policy.utilities.toml import to_toml_array diff --git a/src/compwa_policy/utilities/pyproject/setters.py b/src/compwa_policy/utilities/pyproject/setters.py index fe51633d..c4ac5bbc 100644 --- a/src/compwa_policy/utilities/pyproject/setters.py +++ b/src/compwa_policy/utilities/pyproject/setters.py @@ -2,6 +2,7 @@ from __future__ import annotations +import re from collections import abc from collections.abc import Iterable, Mapping, MutableMapping, Sequence from typing import TYPE_CHECKING, Any, cast @@ -85,7 +86,7 @@ def get_sub_table( return cast(MutableMapping[str, Any], table) -def remove_dependency( +def remove_dependency( # noqa: C901 pyproject: PyprojectTOML, package: str, ignored_sections: Iterable[str] | None = None, @@ -95,23 +96,54 @@ def remove_dependency( return False updated = False dependencies = project.get("dependencies") - if dependencies is not None and package in dependencies: - dependencies.remove(package) - updated = True + if dependencies is not None: + package_names = [split_dependency_definition(p)[0] for p in dependencies] + if package in set(package_names): + idx = package_names.index(package) + dependencies.pop(idx) + updated = True optional_dependencies = project.get("optional-dependencies") if optional_dependencies is not None: if ignored_sections is None: ignored_sections = set() else: ignored_sections = set(ignored_sections) - for section, values in optional_dependencies.items(): + for section, dependencies in optional_dependencies.items(): if section in ignored_sections: continue - if package in values: - values.remove(package) + package_names = [split_dependency_definition(p)[0] for p in dependencies] + if package in set(package_names): + idx = package_names.index(package) + dependencies.pop(idx) updated = True if updated: empty_sections = [k for k, v in optional_dependencies.items() if not v] for section in empty_sections: del optional_dependencies[section] return updated + + +def split_dependency_definition(definition: str) -> tuple[str, str, str]: + """Get the package name, operator, and version from a PyPI dependency definition. + + >>> split_dependency_definition("julia") + ('julia', '', '') + >>> split_dependency_definition("python==3.9.*") + ('python', '==', '3.9.*') + >>> split_dependency_definition("graphviz # for binder") + ('graphviz', '', '') + >>> split_dependency_definition("pip > 19 # needed") + ('pip', '>', '19') + >>> split_dependency_definition("compwa-policy!= 3.14") + ('compwa-policy', '!=', '3.14') + >>> split_dependency_definition("my_package~=1.2") + ('my_package', '~=', '1.2') + >>> split_dependency_definition("any_version_package==*") + ('any_version_package', '==', '*') + """ + matches = re.match(r"^([a-zA-Z0-9_-]+)([\!<=>~\s]*)([^ ^#]*)", definition) + if not matches: + msg = f"Could not extract package name and version from {definition}" + raise ValueError(msg) + package, operator, version = matches.groups() + return package.strip(), operator.strip(), version.strip() diff --git a/src/compwa_policy/utilities/python.py b/src/compwa_policy/utilities/python.py index 0309766c..6f5167f4 100644 --- a/src/compwa_policy/utilities/python.py +++ b/src/compwa_policy/utilities/python.py @@ -1,7 +1,6 @@ # noqa: D100 from __future__ import annotations -import re from pathlib import Path from compwa_policy.utilities import CONFIG_PATH @@ -15,29 +14,3 @@ def has_constraint_files() -> bool: constraint_files = [get_constraints_file(v) for v in python_versions] constraint_paths = [Path(path) for path in constraint_files if path is not None] return any(path.exists() for path in constraint_paths) - - -def split_dependency_definition(definition: str) -> tuple[str, str, str]: - """Get the package name, operator, and version from a PyPI dependency definition. - - >>> split_dependency_definition("julia") - ('julia', '', '') - >>> split_dependency_definition("python==3.9.*") - ('python', '==', '3.9.*') - >>> split_dependency_definition("graphviz # for binder") - ('graphviz', '', '') - >>> split_dependency_definition("pip > 19 # needed") - ('pip', '>', '19') - >>> split_dependency_definition("compwa-policy!= 3.14") - ('compwa-policy', '!=', '3.14') - >>> split_dependency_definition("my_package~=1.2") - ('my_package', '~=', '1.2') - >>> split_dependency_definition("any_version_package==*") - ('any_version_package', '==', '*') - """ - matches = re.match(r"^([a-zA-Z0-9_-]+)([\!<=>~\s]*)([^ ^#]*)", definition) - if not matches: - msg = f"Could not extract package name and version from {definition}" - raise ValueError(msg) - package, operator, version = matches.groups() - return package.strip(), operator.strip(), version.strip()