Skip to content

Commit d1bbd9e

Browse files
committed
Deduplicate config paths
When using lists there are time where duplicates can be introduced and we never want to process paths more than once. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 064cff2 commit d1bbd9e

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/frequenz/repo/config/nox/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def package_args(self, session: _nox.Session, /) -> list[str]:
156156
_util.path_to_package(p) for p in _util.existing_paths(self.extra_paths)
157157
)
158158

159-
return list(_itertools.chain(source_packages, extra_packages))
159+
return list(
160+
_util.deduplicate(_itertools.chain(source_packages, extra_packages))
161+
)
160162

161163

162164
_config: Config | None = None

src/frequenz/repo/config/nox/util.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,22 @@ def replace(iterable: Iterable[_T], replacements: Mapping[_T, _T], /) -> Iterabl
5151
yield item
5252

5353

54+
def deduplicate(iterable: Iterable[_T], /) -> Iterable[_T]:
55+
"""Filter out duplicates from an iterable preserving the original iterable order.
56+
57+
Args:
58+
iterable: The iterable to remove duplicates from.
59+
60+
Returns:
61+
The elements of `iterable`, without duplicates but preserving order.
62+
"""
63+
# We can't use a set() here because sets don't preserve order. We use this hack
64+
# with dict.fromkeys() because dicts do preserve order in Python 3.7+.
65+
return dict.fromkeys(iterable).keys()
66+
67+
5468
def existing_paths(paths: Iterable[str], /) -> Iterable[_pathlib.Path]:
55-
"""Filter paths to only leave valid paths that exist.
69+
"""Filter paths to only leave valid paths that exist and are unique.
5670
5771
Args:
5872
paths: The paths to check and filter.
@@ -63,7 +77,7 @@ def existing_paths(paths: Iterable[str], /) -> Iterable[_pathlib.Path]:
6377
Example:
6478
>>> assert list(existing_paths([".", "/fake"])) == [pathlib.Path(".")]
6579
"""
66-
return (p for p in map(_pathlib.Path, paths) if p.exists())
80+
return deduplicate(p for p in map(_pathlib.Path, paths) if p.exists())
6781

6882

6983
def is_python_file(path: _pathlib.Path, /) -> bool:
@@ -215,4 +229,4 @@ def discover_paths() -> list[str]:
215229
.get("testpaths", [])
216230
)
217231

218-
return testpaths
232+
return list(deduplicate(testpaths))

0 commit comments

Comments
 (0)