Skip to content

Commit d2dd6c6

Browse files
committed
Use sets for config paths and sessions
When using lists there are time where duplicates can be introduced and we never want to process paths or sessions more than once. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent ce2e8bc commit d2dd6c6

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ class Config:
6161
opts: CommandsOptions = dataclasses.field(default_factory=CommandsOptions)
6262
"""Command-line options for each command used by sessions."""
6363

64-
sessions: list[str] = dataclasses.field(default_factory=lambda: [])
64+
sessions: set[str] = dataclasses.field(default_factory=set)
6565
"""List of sessions to run."""
6666

67-
source_paths: list[str] = dataclasses.field(default_factory=lambda: [])
67+
source_paths: set[str] = dataclasses.field(default_factory=set)
6868
"""List of paths containing source files that should be analyzed by the sessions.
6969
7070
Source paths are inspected for `__init__.py` files to look for packages.
@@ -77,7 +77,7 @@ class Config:
7777
checking.
7878
"""
7979

80-
extra_paths: list[str] = dataclasses.field(default_factory=lambda: [])
80+
extra_paths: set[str] = dataclasses.field(default_factory=set)
8181
"""List of extra paths to be analyzed by the sessions.
8282
8383
These are not inspected for packages, as they are passed verbatim to the
@@ -91,7 +91,7 @@ def __post_init__(self) -> None:
9191
"""
9292
for path in util.discover_paths():
9393
if path not in self.extra_paths:
94-
self.extra_paths.append(path)
94+
self.extra_paths.add(path)
9595

9696
def copy(self, /) -> Self:
9797
"""Create a new object as a copy of self.
@@ -101,7 +101,7 @@ def copy(self, /) -> Self:
101101
"""
102102
return dataclasses.replace(self)
103103

104-
def path_args(self, session: nox.Session, /) -> list[str]:
104+
def path_args(self, session: nox.Session, /) -> set[str]:
105105
"""Return the file paths to run the checks on.
106106
107107
If positional arguments are present in the nox session, those are used
@@ -115,13 +115,13 @@ def path_args(self, session: nox.Session, /) -> list[str]:
115115
The file paths to run the checks on.
116116
"""
117117
if session.posargs:
118-
return session.posargs
118+
return set(session.posargs)
119119

120-
return [
121-
str(p) for p in util.existing_paths(self.source_paths + self.extra_paths)
122-
]
120+
return {
121+
str(p) for p in util.existing_paths(self.source_paths | self.extra_paths)
122+
}
123123

124-
def package_args(self, session: nox.Session, /) -> list[str]:
124+
def package_args(self, session: nox.Session, /) -> set[str]:
125125
"""Return the package names to run the checks on.
126126
127127
If positional arguments are present in the nox session, those are used
@@ -138,7 +138,7 @@ def package_args(self, session: nox.Session, /) -> list[str]:
138138
The package names found in the `source_paths`.
139139
"""
140140
if session.posargs:
141-
return session.posargs
141+
return set(session.posargs)
142142

143143
sources_package_dirs_with_roots = (
144144
(p, util.find_toplevel_package_dirs(p))
@@ -155,7 +155,7 @@ def package_args(self, session: nox.Session, /) -> list[str]:
155155
util.path_to_package(p) for p in util.existing_paths(self.extra_paths)
156156
)
157157

158-
return [*source_packages, *extra_packages]
158+
return {*source_packages, *extra_packages}
159159

160160

161161
_config: Config | None = None

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,24 @@
5656

5757
common_config = _config.Config(
5858
opts=common_command_options.copy(),
59-
sessions=[
59+
sessions={
6060
"formatting",
6161
"mypy",
6262
"pylint",
6363
"docstrings",
6464
"pytest_min",
6565
"pytest_max",
66-
],
67-
source_paths=[
66+
},
67+
source_paths={
6868
"src",
69-
],
70-
extra_paths=[
69+
},
70+
extra_paths={
7171
"benchmarks",
7272
"docs",
7373
"examples",
7474
"noxfile.py",
7575
"tests",
76-
],
76+
},
7777
)
7878
"""Default configuration for all types of repositories."""
7979

@@ -90,9 +90,9 @@
9090
common_config,
9191
opts=api_command_options,
9292
# We don't check the sources at all because they are automatically generated.
93-
source_paths=[],
93+
source_paths=set(),
9494
# We adapt the path to the tests.
95-
extra_paths=list(util.replace(common_config.extra_paths, {"tests": "pytests"})),
95+
extra_paths=set(util.replace(common_config.extra_paths, {"tests": "pytests"})),
9696
)
9797
"""Default configuration for APIs.
9898

0 commit comments

Comments
 (0)