Skip to content

Commit

Permalink
refactor(distribution): better name methods
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Maas <[email protected]>
  • Loading branch information
mkniewallner and fpgmaas committed Nov 3, 2024
1 parent bcfac93 commit 6b3700c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
10 changes: 5 additions & 5 deletions python/deptry/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def normalize_distribution_name(name: str) -> str:


@lru_cache(maxsize=1)
def get_packages_normalized_distributions() -> dict[str, set[str]]:
def get_packages_to_normalized_distributions_mapping() -> dict[str, set[str]]:
"""
Return a mapping of top-level packages to their normalized distributions.
Cache ensures that we only build this mapping once, since it should not change during the invocation of deptry.
Expand All @@ -28,14 +28,14 @@ def get_packages_normalized_distributions() -> dict[str, set[str]]:


@lru_cache(maxsize=1)
def get_normalized_distributions_packages() -> dict[str, set[str]]:
def get_normalized_distributions_to_packages_mapping() -> dict[str, set[str]]:
"""
Return a mapping of normalized distributions to their top-level packages.
Cache ensures that we only build this mapping once, since it should not change during the invocation of deptry.
"""
distributions_packages: dict[str, set[str]] = defaultdict(set)

for package, distributions in get_packages_normalized_distributions().items():
for package, distributions in get_packages_to_normalized_distributions_mapping().items():
for distribution in distributions:
distributions_packages[distribution].add(package)

Expand All @@ -46,11 +46,11 @@ def get_distributions_from_package(name: str) -> set[str] | None:
"""
Retrieve the distributions provided by the package, if any.
"""
return get_packages_normalized_distributions().get(name)
return get_packages_to_normalized_distributions_mapping().get(name)


def get_packages_from_distribution(name: str) -> set[str] | None:
"""
Normalize the distribution and retrieve the packages it provides, if any.
"""
return get_normalized_distributions_packages().get(normalize_distribution_name(name))
return get_normalized_distributions_to_packages_mapping().get(normalize_distribution_name(name))
32 changes: 17 additions & 15 deletions tests/unit/test_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

from deptry.distribution import (
get_distributions_from_package,
get_normalized_distributions_packages,
get_normalized_distributions_to_packages_mapping,
get_packages_from_distribution,
get_packages_normalized_distributions,
get_packages_to_normalized_distributions_mapping,
normalize_distribution_name,
)

Expand All @@ -29,9 +29,9 @@ def test_normalize_distribution_name(name: str) -> None:
assert normalize_distribution_name(name) == "friendly-bard"


def test_get_packages_normalized_distributions() -> None:
def test_get_packages_to_normalized_distributions_mapping() -> None:
# Clear cache before calling the function, as it is also populated during testing.
get_packages_normalized_distributions.cache_clear()
get_packages_to_normalized_distributions_mapping.cache_clear()

with patch(
"deptry.distribution.importlib_metadata.packages_distributions",
Expand All @@ -44,13 +44,13 @@ def test_get_packages_normalized_distributions() -> None:
"setuptools": ["setuptools"],
},
) as mock_packages_distributions:
normalized_packages_distributions = get_packages_normalized_distributions()
normalized_packages_distributions = get_packages_to_normalized_distributions_mapping()

# Call function a second time, to ensure that we only call `packages_distributions` once.
get_packages_normalized_distributions()
get_packages_to_normalized_distributions_mapping()

# Clear cache after calling the function to avoid keeping our mocked values, in case test invocation depend on it.
get_packages_normalized_distributions.cache_clear()
get_packages_to_normalized_distributions_mapping.cache_clear()

assert normalized_packages_distributions == {
"requests": {"requests"},
Expand All @@ -63,12 +63,12 @@ def test_get_packages_normalized_distributions() -> None:
mock_packages_distributions.assert_called_once()


def test_get_normalized_distributions_packages() -> None:
def test_get_normalized_distributions_to_packages_mapping() -> None:
# Clear cache before calling the function, as it is also populated during testing.
get_normalized_distributions_packages.cache_clear()
get_normalized_distributions_to_packages_mapping.cache_clear()

with patch(
"deptry.distribution.get_packages_normalized_distributions",
"deptry.distribution.get_packages_to_normalized_distributions_mapping",
return_value={
"requests": {"requests"},
"charset_normalizer": {"charset-normalizer"},
Expand All @@ -78,13 +78,13 @@ def test_get_normalized_distributions_packages() -> None:
"setuptools": {"setuptools"},
},
) as mock_packages_distributions:
distributions_packages = get_normalized_distributions_packages()
distributions_packages = get_normalized_distributions_to_packages_mapping()

# Call function a second time, to ensure that we only call `packages_distributions` once.
get_normalized_distributions_packages()
get_normalized_distributions_to_packages_mapping()

# Clear cache after calling the function to avoid keeping our mocked values, in case test invocation depend on it.
get_normalized_distributions_packages.cache_clear()
get_normalized_distributions_to_packages_mapping.cache_clear()

assert distributions_packages == {
"requests": {"requests"},
Expand All @@ -97,7 +97,7 @@ def test_get_normalized_distributions_packages() -> None:

def test_get_distributions_from_package() -> None:
with patch(
"deptry.distribution.get_packages_normalized_distributions",
"deptry.distribution.get_packages_to_normalized_distributions_mapping",
return_value={
"bar": {"foo-bar"},
"foo": {"foo-bar", "foo"},
Expand All @@ -107,5 +107,7 @@ def test_get_distributions_from_package() -> None:


def test_get_packages_from_distribution() -> None:
with patch("deptry.distribution.get_normalized_distributions_packages", return_value={"foo-bar": {"bar", "foo"}}):
with patch(
"deptry.distribution.get_normalized_distributions_to_packages_mapping", return_value={"foo-bar": {"bar", "foo"}}
):
assert get_packages_from_distribution("foo_Bar") == {"bar", "foo"}

0 comments on commit 6b3700c

Please sign in to comment.