Skip to content

Commit acf3c83

Browse files
committed
Add a nox.configure() overload that takes a RepositoryType
To make the most common (default) configuration easier and more obvious, we add an overload of `nox.configure()` that takes a `RepositoryType` instead of a `Config` object. This will then pick the default configuration for that type of repository automatically. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 37f2974 commit acf3c83

File tree

4 files changed

+83
-26
lines changed

4 files changed

+83
-26
lines changed

cookiecutter/{{cookiecutter.github_repo_name}}/noxfile.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""Configuration file for nox."""
55

6-
from frequenz.repo.config import nox
7-
from frequenz.repo.config.nox import default
6+
from frequenz.repo.config import RepositoryType, nox
87

9-
nox.configure(default.{{cookiecutter.type}}_config)
8+
nox.configure(RepositoryType.{{cookiecutter.type | upper}})

src/frequenz/repo/config/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@
2626
package and use the [`frequenz.repo.config.nox.configure`][] function,
2727
which will configure all nox sessions.
2828
29-
You should call `configure()` using one of the default configurations provided
30-
in the [`frequenz.repo.config.nox.default`][] module. For example:
29+
To use the default options, you should call `configure()` using one of the [repository
30+
types][frequenz.repo.config.RepositoryType]. For example:
3131
3232
```python
33-
from frequenz.repo.config import nox
34-
from frequenz.repo.config.nox import default
33+
from frequenz.repo.config import RepositoryType, nox
3534
36-
nox.configure(default.lib_config)
35+
nox.configure(RepositoryType.LIB)
3736
```
3837
39-
Again, make sure to pick the correct default configuration based on the type of your
38+
Again, make sure to pick the correct project typedefault configuration based on the type of your
4039
project (`actor_config`, `api_config`, `app_config`, `lib_config`, `model_config`).
4140
42-
If you need to modify the configuration, you can copy one of the default
43-
configurations by using the
44-
[`copy()`][frequenz.repo.config.nox.config.Config.copy] method:
41+
If you need to use some custom configuration, you can start from the default settings in
42+
the [`frequenz.repo.config.nox.default`][] module,
43+
[copying][frequenz.repo.config.nox.config.Config.copy] it and changing whatever you
44+
need to customize. For example:
4545
4646
```python
4747
from frequenz.repo.config import nox

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@
66
The main entry point is the [`configure()`][configure] function, which will
77
configure all nox sessions according to some configuration.
88
9-
You should call `configure()` using one of the default configurations provided
10-
in the [`default`][] module. For example:
9+
To use the default options, you should call `configure()` using one of the [repository
10+
types][frequenz.repo.config.RepositoryType]. For example:
1111
1212
```python
13-
from frequenz.repo.config import nox
14-
from frequenz.repo.config.nox import default
13+
from frequenz.repo.config import RepositoryType, nox
1514
16-
nox.configure(default.lib_config)
15+
nox.configure(RepositoryType.LIB)
1716
```
1817
19-
If you need to modify the configuration, you can copy one of the default
20-
configurations by using the
21-
[`copy()`][frequenz.repo.config.nox.config.Config.copy] method:
18+
Again, make sure to pick the correct project typedefault configuration based on the type of your
19+
project (`actor_config`, `api_config`, `app_config`, `lib_config`, `model_config`).
20+
21+
If you need to use some custom configuration, you can start from the default settings in
22+
the [`frequenz.repo.config.nox.default`][] module,
23+
[copying][frequenz.repo.config.nox.config.Config.copy] it and changing whatever you
24+
need to customize. For example:
2225
2326
```python
2427
from frequenz.repo.config import nox
2528
from frequenz.repo.config.nox import default
2629
27-
conf = default.lib_config.copy()
28-
conf.opts.black.append("--diff")
29-
nox.configure(conf)
30+
config = default.lib_config.copy()
31+
config.opts.black.append("--diff")
32+
nox.configure(config)
3033
```
3134
3235
If you need further customization or to define new sessions, you can use the

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

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414

1515
import dataclasses as _dataclasses
1616
import itertools as _itertools
17-
from typing import Self
17+
from typing import Self, assert_never, overload
1818

1919
import nox as _nox
2020

21+
from .._core import RepositoryType
2122
from . import util as _util
2223

2324

@@ -177,6 +178,7 @@ def get() -> Config:
177178
return _config
178179

179180

181+
@overload
180182
def configure(conf: Config, /, *, import_default_sessions: bool = True) -> None:
181183
"""Configure nox using the provided configuration.
182184
@@ -186,10 +188,63 @@ def configure(conf: Config, /, *, import_default_sessions: bool = True) -> None:
186188
This is only necessary if you want to avoid using the default provided
187189
sessions and use your own.
188190
"""
191+
192+
193+
@overload
194+
def configure(
195+
repo_type: RepositoryType, /, *, import_default_sessions: bool = True
196+
) -> None:
197+
"""Configure nox using the provided repository type.
198+
199+
Args:
200+
repo_type: The repository type to use to configure nox. This will use the
201+
default configuration in [`frequenz.repo.config.nox.default`][] for that
202+
type of repository.
203+
import_default_sessions: Whether to import the default sessions or not.
204+
This is only necessary if you want to avoid using the default provided
205+
sessions and use your own.
206+
"""
207+
208+
209+
def configure(
210+
conf: Config | RepositoryType, /, *, import_default_sessions: bool = True
211+
) -> None:
212+
"""Configure nox using the provided configuration or repository type.
213+
214+
Args:
215+
conf: The configuration to use to configure nox, or the repository type to use
216+
to configure nox. The later will use the default configuration in
217+
[`frequenz.repo.config.nox.default`][] for that type of repository.
218+
import_default_sessions: Whether to import the default sessions or not.
219+
This is only necessary if you want to avoid using the default provided
220+
sessions and use your own.
221+
"""
222+
global _config # pylint: disable=global-statement
223+
189224
# We need to make sure sessions are imported, otherwise they won't be visible to nox.
190225
if import_default_sessions:
191226
# pylint: disable=import-outside-toplevel,cyclic-import
192227
from . import session as _
193-
global _config # pylint: disable=global-statement
194-
_config = conf
228+
229+
match conf:
230+
case Config():
231+
_config = conf
232+
case RepositoryType() as repo_type:
233+
# pylint: disable=import-outside-toplevel,cyclic-import
234+
from . import default
235+
236+
match repo_type:
237+
case RepositoryType.ACTOR:
238+
_config = default.actor_config
239+
case RepositoryType.API:
240+
_config = default.api_config
241+
case RepositoryType.APP:
242+
_config = default.app_config
243+
case RepositoryType.LIB:
244+
_config = default.lib_config
245+
case RepositoryType.MODEL:
246+
_config = default.model_config
247+
case _ as unhandled:
248+
assert_never(unhandled)
249+
195250
_nox.options.sessions = _config.sessions

0 commit comments

Comments
 (0)