Skip to content

Commit e8beb6b

Browse files
committed
Don't import modules into packages
Importing modules in package's __init__.py has the unintended effect of indirectly importing everything the module imports too. This has a few problems: * Impacts performance, as many, probably unused, dependencies need to be imported * Makes it impossible to use optional dependencies, as all dependencies will be imported anyway Also not importing modules makes it easier to understand what is a module and what is a symbol/object living in the package. Because of this change, now `default` needs to be explicitly imported in `noxfile.py`. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent fa18d70 commit e8beb6b

File tree

5 files changed

+16
-25
lines changed

5 files changed

+16
-25
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"""Configuration file for nox."""
55

66
from frequenz.repo.config import nox
7+
from frequenz.repo.config.nox import default
78

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

noxfile.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"""Configuration file for nox."""
55

66
from frequenz.repo.config import nox
7+
from frequenz.repo.config.nox import default
78

8-
config = nox.default.lib_config.copy()
9+
config = default.lib_config.copy()
910
config.extra_paths.extend(
1011
[
1112
"cookiecutter/hooks",
1213
"cookiecutter/local_extensions.py",
1314
]
1415
)
15-
nox.configure(nox.default.lib_config)
16+
nox.configure(default.lib_config)

src/frequenz/repo/config/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
3232
```python
3333
from frequenz.repo.config import nox
34+
from frequenz.repo.config.nox import default
3435
35-
nox.configure(nox.default.lib_config)
36+
nox.configure(default.lib_config)
3637
```
3738
3839
Again, make sure to pick the correct default configuration based on the type of your
@@ -44,8 +45,9 @@
4445
4546
```python
4647
from frequenz.repo.config import nox
48+
from frequenz.repo.config.nox import default
4749
48-
config = nox.default.lib_config.copy()
50+
config = default.lib_config.copy()
4951
config.opts.black.append("--diff")
5052
nox.configure(config)
5153
```
@@ -330,12 +332,8 @@
330332
defaults.
331333
"""
332334

333-
from . import mkdocs, nox, setuptools
334335
from ._core import RepositoryType
335336

336337
__all__ = [
337338
"RepositoryType",
338-
"mkdocs",
339-
"nox",
340-
"setuptools",
341339
]

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@
1010
in the [`default`][] module. For example:
1111
1212
```python
13-
from frequenz.repo import config
13+
from frequenz.repo.config import nox
14+
from frequenz.repo.config.nox import default
1415
15-
config.nox.configure(config.nox.default.lib_config)
16+
nox.configure(default.lib_config)
1617
```
1718
1819
If you need to modify the configuration, you can copy one of the default
1920
configurations by using the
2021
[`copy()`][frequenz.repo.config.nox.config.Config.copy] method:
2122
2223
```python
23-
from frequenz.repo import config
24+
from frequenz.repo.config import nox
25+
from frequenz.repo.config.nox import default
2426
25-
conf = config.nox.default.lib_config.copy()
27+
conf = default.lib_config.copy()
2628
conf.opts.black.append("--diff")
27-
config.nox.configure(conf)
29+
nox.configure(conf)
2830
```
2931
3032
If you need further customization or to define new sessions, you can use the
@@ -41,13 +43,8 @@
4143
- [`util`][]: General purpose utility functions.
4244
"""
4345

44-
from . import config, default, session, util
4546
from .config import configure
4647

4748
__all__ = [
48-
"config",
4949
"configure",
50-
"default",
51-
"session",
52-
"util",
5350
]

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,3 @@
22
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
33

44
"""Setuptools utilities."""
5-
6-
from . import grpc_tools
7-
8-
__all__ = [
9-
"grpc_tools",
10-
]

0 commit comments

Comments
 (0)