Skip to content

Commit

Permalink
FIX: remove package even if it has a version specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Oct 24, 2024
1 parent f937a57 commit ad189e7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dev = [
"compwa-policy[test]",
"labels",
"sphinx-autobuild",
"tox >=1.9", # for skip_install, use_develop
]
doc = [
"Sphinx",
Expand Down
2 changes: 1 addition & 1 deletion src/compwa_policy/check_dev_files/pixi/_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 39 additions & 7 deletions src/compwa_policy/utilities/pyproject/setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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()
27 changes: 0 additions & 27 deletions src/compwa_policy/utilities/python.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# noqa: D100
from __future__ import annotations

import re
from pathlib import Path

from compwa_policy.utilities import CONFIG_PATH
Expand All @@ -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()

0 comments on commit ad189e7

Please sign in to comment.