Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Exclude dev depenencies
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Mar 2, 2022
1 parent cf54f29 commit 8de5b2d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
18 changes: 16 additions & 2 deletions synapse/util/check_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ def dependencies(self) -> Iterable[str]:
yield '"' + i + '"'


EXTRAS = set(metadata.metadata(DISTRIBUTION_NAME).get_all("Provides-Extra"))
DEV_EXTRAS = {"lint", "mypy", "test", "dev"}
RUNTIME_EXTRAS = (
set(metadata.metadata(DISTRIBUTION_NAME).get_all("Provides-Extra")) - DEV_EXTRAS
)


def _is_dev_dependency(req: Requirement) -> bool:
return req.marker is not None and any(
req.marker.evaluate({"extra": e}) for e in DEV_EXTRAS
)


class Dependency(NamedTuple):
Expand All @@ -67,6 +76,9 @@ def _generic_dependencies() -> Iterable[Dependency]:
assert requirements is not None
for raw_requirement in requirements:
req = Requirement(raw_requirement)
if _is_dev_dependency(req):
continue

# https://packaging.pypa.io/en/latest/markers.html#usage notes that
# > Evaluating an extra marker with no environment is an error
# so we pass in a dummy empty extra value here.
Expand All @@ -80,6 +92,8 @@ def _dependencies_for_extra(extra: str) -> Iterable[Dependency]:
assert requirements is not None
for raw_requirement in requirements:
req = Requirement(raw_requirement)
if _is_dev_dependency(req):
continue
# Exclude mandatory deps by only selecting deps needed with this extra.
if (
req.marker is not None
Expand Down Expand Up @@ -124,7 +138,7 @@ def check_requirements(extra: Optional[str] = None) -> None:
# First work out which dependencies are required, and which are optional.
if extra is None:
dependencies = _generic_dependencies()
elif extra in EXTRAS:
elif extra in RUNTIME_EXTRAS:
dependencies = _dependencies_for_extra(extra)
else:
raise ValueError(f"Synapse does not provide the feature '{extra}'")
Expand Down
19 changes: 18 additions & 1 deletion tests/util/test_check_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ def test_mandatory_dependency(self) -> None:
# should not raise
check_requirements()

def test_checks_ignore_dev_dependencies(self) -> None:
"""Bot generic and per-extra checks should ignore dev dependencies."""
with patch(
"synapse.util.check_dependencies.metadata.requires",
return_value=["dummypkg >= 1; extra == 'mypy'"],
), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
# We're testing that none of these calls raise.
with self.mock_installed_package(None):
check_requirements()
check_requirements("cool-extra")
with self.mock_installed_package(old):
check_requirements()
check_requirements("cool-extra")
with self.mock_installed_package(new):
check_requirements()
check_requirements("cool-extra")

def test_generic_check_of_optional_dependency(self) -> None:
"""Complain if an optional package is old."""
with patch(
Expand All @@ -85,7 +102,7 @@ def test_check_for_extra_dependencies(self) -> None:
with patch(
"synapse.util.check_dependencies.metadata.requires",
return_value=["dummypkg >= 1; extra == 'cool-extra'"],
), patch("synapse.util.check_dependencies.EXTRAS", {"cool-extra"}):
), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
with self.mock_installed_package(None):
self.assertRaises(DependencyException, check_requirements, "cool-extra")
with self.mock_installed_package(old):
Expand Down

0 comments on commit 8de5b2d

Please sign in to comment.