Skip to content

Commit f46b349

Browse files
committed
Make imports private when only used internally
This is to avoid mistakes from users accidentally importing something that is not supposed to be exposed by a module but just to be used internally. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent d2dd6c6 commit f46b349

File tree

6 files changed

+60
-59
lines changed

6 files changed

+60
-59
lines changed

src/frequenz/repo/config/_core.py

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

44
"""Base types used accross the project."""
55

6-
import enum
6+
import enum as _enum
77

88

9-
class RepositoryType(enum.Enum):
9+
class RepositoryType(_enum.Enum):
1010
"""Supported types of repository."""
1111

1212
ACTOR = "actor"

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,37 @@
1212
The `configure()` function must be called before `get()` is used.
1313
"""
1414

15-
import dataclasses
15+
import dataclasses as _dataclasses
1616
from typing import Self
1717

18-
import nox
18+
import nox as _nox
1919

20-
from . import util
20+
from . import util as _util
2121

2222

23-
@dataclasses.dataclass(kw_only=True, slots=True)
23+
@_dataclasses.dataclass(kw_only=True, slots=True)
2424
class CommandsOptions:
2525
"""Command-line options for each command."""
2626

27-
black: list[str] = dataclasses.field(default_factory=lambda: [])
27+
black: list[str] = _dataclasses.field(default_factory=lambda: [])
2828
"""Command-line options for the `black` command."""
2929

30-
darglint: list[str] = dataclasses.field(default_factory=lambda: [])
30+
darglint: list[str] = _dataclasses.field(default_factory=lambda: [])
3131
"""Command-line options for the `darglint` command."""
3232

33-
isort: list[str] = dataclasses.field(default_factory=lambda: [])
33+
isort: list[str] = _dataclasses.field(default_factory=lambda: [])
3434
"""Command-line options for the `isort` command."""
3535

36-
mypy: list[str] = dataclasses.field(default_factory=lambda: [])
36+
mypy: list[str] = _dataclasses.field(default_factory=lambda: [])
3737
"""Command-line options for the `mypy` command."""
3838

39-
pydocstyle: list[str] = dataclasses.field(default_factory=lambda: [])
39+
pydocstyle: list[str] = _dataclasses.field(default_factory=lambda: [])
4040
"""Command-line options for the `pydocstyle` command."""
4141

42-
pylint: list[str] = dataclasses.field(default_factory=lambda: [])
42+
pylint: list[str] = _dataclasses.field(default_factory=lambda: [])
4343
"""Command-line options for the `pylint` command."""
4444

45-
pytest: list[str] = dataclasses.field(default_factory=lambda: [])
45+
pytest: list[str] = _dataclasses.field(default_factory=lambda: [])
4646
"""Command-line options for the `pytest` command."""
4747

4848
def copy(self) -> Self:
@@ -51,20 +51,20 @@ def copy(self) -> Self:
5151
Returns:
5252
The copy of self.
5353
"""
54-
return dataclasses.replace(self)
54+
return _dataclasses.replace(self)
5555

5656

57-
@dataclasses.dataclass(kw_only=True, slots=True)
57+
@_dataclasses.dataclass(kw_only=True, slots=True)
5858
class Config:
5959
"""Configuration for nox sessions."""
6060

61-
opts: CommandsOptions = dataclasses.field(default_factory=CommandsOptions)
61+
opts: CommandsOptions = _dataclasses.field(default_factory=CommandsOptions)
6262
"""Command-line options for each command used by sessions."""
6363

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

67-
source_paths: set[str] = dataclasses.field(default_factory=set)
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: set[str] = dataclasses.field(default_factory=set)
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
@@ -89,7 +89,7 @@ def __post_init__(self) -> None:
8989
9090
This will add extra paths discovered in config files and other sources.
9191
"""
92-
for path in util.discover_paths():
92+
for path in _util.discover_paths():
9393
if path not in self.extra_paths:
9494
self.extra_paths.add(path)
9595

@@ -99,9 +99,9 @@ def copy(self, /) -> Self:
9999
Returns:
100100
The copy of self.
101101
"""
102-
return dataclasses.replace(self)
102+
return _dataclasses.replace(self)
103103

104-
def path_args(self, session: nox.Session, /) -> set[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
@@ -118,10 +118,10 @@ def path_args(self, session: nox.Session, /) -> set[str]:
118118
return set(session.posargs)
119119

120120
return {
121-
str(p) for p in util.existing_paths(self.source_paths | self.extra_paths)
121+
str(p) for p in _util.existing_paths(self.source_paths | self.extra_paths)
122122
}
123123

124-
def package_args(self, session: nox.Session, /) -> set[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
@@ -141,18 +141,18 @@ def package_args(self, session: nox.Session, /) -> set[str]:
141141
return set(session.posargs)
142142

143143
sources_package_dirs_with_roots = (
144-
(p, util.find_toplevel_package_dirs(p))
145-
for p in util.existing_paths(self.source_paths)
144+
(p, _util.find_toplevel_package_dirs(p))
145+
for p in _util.existing_paths(self.source_paths)
146146
)
147147

148148
source_packages = (
149-
util.path_to_package(pkg_path, root=root)
149+
_util.path_to_package(pkg_path, root=root)
150150
for root, pkg_paths in sources_package_dirs_with_roots
151151
for pkg_path in pkg_paths
152152
)
153153

154154
extra_packages = (
155-
util.path_to_package(p) for p in util.existing_paths(self.extra_paths)
155+
_util.path_to_package(p) for p in _util.existing_paths(self.extra_paths)
156156
)
157157

158158
return {*source_packages, *extra_packages}
@@ -182,4 +182,4 @@ def configure(conf: Config, /) -> None:
182182
"""
183183
global _config # pylint: disable=global-statement
184184
_config = conf
185-
nox.options.sessions = _config.sessions
185+
_nox.options.sessions = _config.sessions

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import dataclasses
2828

2929
from . import config as _config
30-
from . import util
30+
from . import util as _util
3131

3232
common_command_options: _config.CommandsOptions = _config.CommandsOptions(
3333
black=[
@@ -92,7 +92,7 @@
9292
# We don't check the sources at all because they are automatically generated.
9393
source_paths=set(),
9494
# We adapt the path to the tests.
95-
extra_paths=set(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

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import nox
1010

11-
from . import config, util
11+
from . import config as _config
12+
from . import util as _util
1213

1314

1415
@nox.session
@@ -42,7 +43,7 @@ def formatting(session: nox.Session, install_deps: bool = True) -> None:
4243
if install_deps:
4344
session.install("-e", ".[dev-formatting]")
4445

45-
conf = config.get()
46+
conf = _config.get()
4647
session.run("black", *conf.opts.black, *conf.path_args(session))
4748
session.run("isort", *conf.opts.isort, *conf.path_args(session))
4849

@@ -60,8 +61,8 @@ def mypy(session: nox.Session, install_deps: bool = True) -> None:
6061
# fast local tests with `nox -R -e mypy`.
6162
session.install("-e", ".[dev-mypy]")
6263

63-
conf = config.get()
64-
pkg_args = util.flatten(("-p", p) for p in conf.package_args(session))
64+
conf = _config.get()
65+
pkg_args = _util.flatten(("-p", p) for p in conf.package_args(session))
6566
session.run("mypy", *conf.opts.mypy, *pkg_args)
6667

6768

@@ -78,7 +79,7 @@ def pylint(session: nox.Session, install_deps: bool = True) -> None:
7879
# fast local tests with `nox -R -e pylint`.
7980
session.install("-e", ".[dev-pylint]")
8081

81-
conf = config.get()
82+
conf = _config.get()
8283
session.run("pylint", *conf.opts.pylint, *conf.path_args(session))
8384

8485

@@ -93,7 +94,7 @@ def docstrings(session: nox.Session, install_deps: bool = True) -> None:
9394
if install_deps:
9495
session.install("-e", ".[dev-docstrings]")
9596

96-
conf = config.get()
97+
conf = _config.get()
9798
session.run("pydocstyle", *conf.opts.pydocstyle, *conf.path_args(session))
9899

99100
# Darglint checks that function argument and return values are documented.
@@ -136,15 +137,15 @@ def pytest_min(session: nox.Session, install_deps: bool = True) -> None:
136137
if install_deps:
137138
# install the package itself as editable, so that it is possible to do
138139
# fast local tests with `nox -R -e pytest_min`.
139-
session.install("-e", ".[dev-pytest]", *util.min_dependencies())
140+
session.install("-e", ".[dev-pytest]", *_util.min_dependencies())
140141

141142
_pytest_impl(session, "min")
142143

143144

144145
def _pytest_impl(
145146
session: nox.Session, max_or_min_deps: str # pylint: disable=unused-argument
146147
) -> None:
147-
conf = config.get()
148+
conf = _config.get()
148149
session.run("pytest", *conf.opts.pytest, *session.posargs)
149150

150151
# pylint: disable=fixme

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"""
99

1010

11-
import pathlib
12-
import tomllib
11+
import pathlib as _pathlib
12+
import tomllib as _tomllib
1313
from collections.abc import Iterable, Mapping
1414
from typing import TypeVar
1515

@@ -51,7 +51,7 @@ def replace(iterable: Iterable[_T], replacements: Mapping[_T, _T], /) -> Iterabl
5151
yield item
5252

5353

54-
def existing_paths(paths: Iterable[str], /) -> Iterable[pathlib.Path]:
54+
def existing_paths(paths: Iterable[str], /) -> Iterable[_pathlib.Path]:
5555
"""Filter paths to only leave valid paths that exist.
5656
5757
Args:
@@ -63,10 +63,10 @@ def existing_paths(paths: Iterable[str], /) -> Iterable[pathlib.Path]:
6363
Example:
6464
>>> assert list(existing_paths([".", "/fake"])) == [pathlib.Path(".")]
6565
"""
66-
return (p for p in map(pathlib.Path, paths) if p.exists())
66+
return (p for p in map(_pathlib.Path, paths) if p.exists())
6767

6868

69-
def is_python_file(path: pathlib.Path, /) -> bool:
69+
def is_python_file(path: _pathlib.Path, /) -> bool:
7070
"""Check if a path is a Python file.
7171
7272
Args:
@@ -78,7 +78,7 @@ def is_python_file(path: pathlib.Path, /) -> bool:
7878
return path.suffix in (".py", ".pyi")
7979

8080

81-
def path_to_package(path: pathlib.Path, root: pathlib.Path | None = None) -> str:
81+
def path_to_package(path: _pathlib.Path, root: _pathlib.Path | None = None) -> str:
8282
"""Convert paths to Python package names.
8383
8484
Paths should exist and be either a directory or a file ending with `.pyi?`
@@ -111,8 +111,8 @@ def path_to_package(path: pathlib.Path, root: pathlib.Path | None = None) -> str
111111

112112

113113
def find_toplevel_package_dirs(
114-
path: pathlib.Path, /, *, root: pathlib.Path | None = None
115-
) -> Iterable[pathlib.Path]:
114+
path: _pathlib.Path, /, *, root: _pathlib.Path | None = None
115+
) -> Iterable[_pathlib.Path]:
116116
"""Find top-level packages directories in a `path`.
117117
118118
Searches recursively for the top-level packages in `path`, relative to
@@ -176,7 +176,7 @@ def min_dependencies() -> list[str]:
176176
177177
"""
178178
with open("pyproject.toml", "rb") as toml_file:
179-
data = tomllib.load(toml_file)
179+
data = _tomllib.load(toml_file)
180180

181181
dependencies = data.get("project", {}).get("dependencies", {})
182182
if not dependencies:
@@ -207,7 +207,7 @@ def discover_paths() -> list[str]:
207207
The discovered paths to check.
208208
"""
209209
with open("pyproject.toml", "rb") as toml_file:
210-
data = tomllib.load(toml_file)
210+
data = _tomllib.load(toml_file)
211211

212212
testpaths: list[str] = (
213213
data.get("tool", {})

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
protocol buffer files are compiled automatically before the project is built.
1111
"""
1212

13-
import pathlib
14-
import subprocess
15-
import sys
13+
import pathlib as _pathlib
14+
import subprocess as _subprocess
15+
import sys as _sys
1616

17-
import setuptools
17+
import setuptools as _setuptools
1818

1919
# The typing stub for this module is missing
20-
import setuptools.command.build # type: ignore[import]
20+
import setuptools.command.build as _build_command # type: ignore[import]
2121

2222

23-
class CompileProto(setuptools.Command):
23+
class CompileProto(_setuptools.Command):
2424
"""Build the Python protobuf files."""
2525

2626
proto_path: str
@@ -72,15 +72,15 @@ def run(self) -> None:
7272
"""Compile the Python protobuf files."""
7373
include_paths = self.include_paths.split(",")
7474
proto_files = [
75-
str(p) for p in pathlib.Path(self.proto_path).rglob(self.proto_glob)
75+
str(p) for p in _pathlib.Path(self.proto_path).rglob(self.proto_glob)
7676
]
7777

7878
if not proto_files:
7979
print(f"No proto files found in {self.proto_path}/**/{self.proto_glob}/")
8080
return
8181

8282
protoc_cmd = (
83-
[sys.executable, "-m", "grpc_tools.protoc"]
83+
[_sys.executable, "-m", "grpc_tools.protoc"]
8484
+ [f"-I{p}" for p in [*include_paths, self.proto_path]]
8585
+ [
8686
f"--{opt}={self.py_path}"
@@ -90,12 +90,12 @@ def run(self) -> None:
9090
)
9191

9292
print(f"Compiling proto files via: {' '.join(protoc_cmd)}")
93-
subprocess.run(protoc_cmd, check=True)
93+
_subprocess.run(protoc_cmd, check=True)
9494

9595

9696
# This adds the compile_proto command to the build sub-command.
9797
# The name of the command is mapped to the class name in the pyproject.toml file,
9898
# in the [project.entry-points.distutils.commands] section.
9999
# The None value is an optional function that can be used to determine if the
100100
# sub-command should be executed or not.
101-
setuptools.command.build.build.sub_commands.insert(0, ("compile_proto", None))
101+
_build_command.build.sub_commands.insert(0, ("compile_proto", None))

0 commit comments

Comments
 (0)