Skip to content

Commit 9938821

Browse files
committed
docs: add more docstrings
1 parent 744d91f commit 9938821

27 files changed

+136
-30
lines changed

src/twyn/base/constants.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@
1212

1313

1414
MANUAL_INPUT_SOURCE = "manual_input"
15+
"""Source identifier for manually provided dependencies."""
1516

1617
SELECTOR_METHOD_MAPPING: dict[str, type[selectors.AbstractSelector]] = {
1718
"first-letter": selectors.FirstLetterExact,
1819
"nearby-letter": selectors.FirstLetterNearbyInKeyboard,
1920
"all": selectors.AllSimilar,
2021
}
22+
"""Mapping of selector method names to their corresponding classes."""
2123

2224
SELECTOR_METHOD_KEYS = set(SELECTOR_METHOD_MAPPING.keys())
25+
"""Set of available selector method names."""
26+
2327
SelectorMethod = Literal["first-letter", "nearby-letter", "all"]
28+
"""Type alias for valid selector method strings."""
2429

2530
DEPENDENCY_FILE_MAPPING: dict[str, type[AbstractParser]] = {
2631
"requirements.txt": dependency_parser.RequirementsTxtParser,
@@ -29,13 +34,24 @@
2934
"package-lock.json": dependency_parser.PackageLockJsonParser,
3035
"yarn.lock": dependency_parser.YarnLockParser,
3136
}
37+
"""Mapping of dependency file names to their parser classes."""
3238

3339

3440
DEFAULT_SELECTOR_METHOD = "all"
41+
"""Default method for selecting similar packages."""
42+
3543
DEFAULT_PROJECT_TOML_FILE = "pyproject.toml"
44+
"""Default filename for project configuration."""
45+
3646
DEFAULT_TWYN_TOML_FILE = "twyn.toml"
47+
"""Default filename for Twyn-specific configuration."""
48+
3749
DEFAULT_USE_CACHE = True
50+
"""Default setting for cache usage."""
51+
3852
DEFAULT_RECURSIVE = False
53+
"""Default setting for recursive processing."""
3954

4055

4156
PackageEcosystems: TypeAlias = Literal["pypi", "npm"]
57+
"""Type alias for supported package ecosystems."""

src/twyn/base/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __init__(self, message: str = "") -> None:
2929
super().__init__(message)
3030

3131
def show(self, file: Optional[IO[Any]] = None) -> None:
32+
"""Display the error message."""
3233
logger.debug(self.format_message(), exc_info=True)
3334
logger.error(self.format_message(), exc_info=False)
3435

src/twyn/base/utils.py

Whitespace-only changes.

src/twyn/cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
@click.group()
4040
@click.version_option(__version__, "--version")
4141
def entry_point() -> None:
42+
"""Provide main CLI entry point for Twyn."""
4243
pass
4344

4445

@@ -188,13 +189,15 @@ def run( # noqa: C901
188189

189190
@entry_point.group()
190191
def allowlist() -> None:
192+
"""Manage package allowlist configuration."""
191193
pass
192194

193195

194196
@allowlist.command()
195197
@click.option("--config", type=click.STRING)
196198
@click.argument("package_name")
197199
def add(package_name: str, config: str) -> None:
200+
"""Add package to allowlist."""
198201
fh = FileHandler(config or ConfigHandler.get_default_config_file_path())
199202
ConfigHandler(fh).add_package_to_allowlist(package_name)
200203

@@ -203,12 +206,14 @@ def add(package_name: str, config: str) -> None:
203206
@click.option("--config", type=click.STRING)
204207
@click.argument("package_name")
205208
def remove(package_name: str, config: str) -> None:
209+
"""Remove package from allowlist."""
206210
fh = FileHandler(config or DEFAULT_PROJECT_TOML_FILE)
207211
ConfigHandler(fh).remove_package_from_allowlist(package_name)
208212

209213

210214
@entry_point.group()
211215
def cache() -> None:
216+
"""Manage cache operations."""
212217
pass
213218

214219

src/twyn/config/config_handler.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,43 @@ class TwynConfiguration:
3333
"""Fully resolved configuration for Twyn."""
3434

3535
dependency_files: set[str]
36+
"""Set of dependency file paths to analyze."""
3637
selector_method: str
38+
"""Method for selecting similar packages."""
3739
allowlist: set[str]
40+
"""Set of package names to allow without checking."""
3841
pypi_source: Optional[str]
42+
"""Alternative PyPI source URL."""
3943
npm_source: Optional[str]
44+
"""Alternative npm source URL."""
4045
use_cache: bool
46+
"""Whether to use cached trusted packages."""
4147
package_ecosystem: Optional[PackageEcosystems]
48+
"""Target package ecosystem for analysis."""
4249
recursive: Optional[bool]
50+
"""Whether to recursively search for dependency files."""
4351

4452

4553
@dataclass
4654
class ReadTwynConfiguration:
4755
"""Configuration for twyn as set by the user. It may have None values."""
4856

4957
dependency_files: Optional[set[str]] = field(default_factory=set)
58+
"""Optional set of dependency file paths to analyze."""
5059
selector_method: Optional[str] = None
60+
"""Optional method for selecting similar packages."""
5161
allowlist: set[str] = field(default_factory=set)
62+
"""Set of package names to allow without checking."""
5263
pypi_source: Optional[str] = None
64+
"""Optional alternative PyPI source URL."""
5365
npm_source: Optional[str] = None
66+
"""Optional alternative npm source URL."""
5467
use_cache: Optional[bool] = None
68+
"""Optional setting for using cached trusted packages."""
5569
package_ecosystem: Optional[PackageEcosystems] = None
70+
"""Optional target package ecosystem for analysis."""
5671
recursive: Optional[bool] = None
72+
"""Optional setting for recursive dependency file search."""
5773

5874

5975
class ConfigHandler:
@@ -200,11 +216,13 @@ def _write_config(self, toml: TOMLDocument, config: ReadTwynConfiguration) -> No
200216
self._write_toml(toml)
201217

202218
def _write_toml(self, toml: TOMLDocument) -> None:
219+
"""Write TOML document to file."""
203220
if not self.file_handler:
204221
raise ConfigFileNotConfiguredError("Config file not configured. Cannot perform write operation.")
205222
self.file_handler.write(dumps(toml))
206223

207224
def _read_toml(self) -> TOMLDocument:
225+
"""Read TOML document from file."""
208226
if not self.file_handler:
209227
raise ConfigFileNotConfiguredError("Config file not configured. Cannot perform read operation.")
210228
try:
@@ -227,6 +245,8 @@ def get_default_config_file_path() -> str:
227245

228246

229247
def _serialize_config(x: Any) -> Union[Any, str, list[Any]]:
248+
"""Serialize configuration values for TOML format."""
249+
230250
def _value_to_for_config(v: Any) -> Union[str, list[Any], Any]:
231251
if isinstance(v, Enum):
232252
return v.name

src/twyn/dependency_managers/dependency_manager.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/twyn/dependency_managers/managers/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@ class BaseDependencyManager:
1313
"""
1414

1515
name: str
16+
"""Name identifier for the package ecosystem."""
1617
trusted_packages_source: type[AbstractPackageReference]
18+
"""Reference class for trusted packages source."""
1719
dependency_files: set[str]
20+
"""Set of supported dependency file names."""
1821

1922
@classmethod
2023
def matches_dependency_file(cls, dependency_file: str) -> bool:
24+
"""Check if this manager can handle the given dependency file."""
2125
return Path(dependency_file).name in cls.dependency_files
2226

2327
@classmethod
2428
def matches_ecosystem_name(cls, name: str) -> bool:
29+
"""Check if this manager matches the given ecosystem name."""
2530
return cls.name == Path(name).name.lower()
2631

2732
@classmethod
2833
def get_alternative_source(cls, sources: dict[str, str]) -> Optional[str]:
34+
"""Get alternative source URL for this ecosystem from sources dict."""
2935
match = [x for x in sources if x == cls.name]
3036

3137
return sources[match[0]] if match else None

src/twyn/dependency_managers/managers/npm_dependency_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88
@dataclass
99
class NpmDependencyManager(BaseDependencyManager):
1010
name = "npm"
11+
"""Name of the npm package ecosystem."""
1112
trusted_packages_source = TopNpmReference
13+
"""Reference source for trusted npm packages."""
1214
dependency_files = {PACKAGE_LOCK_JSON, YARN_LOCK}
15+
"""Set of supported npm dependency file names."""

src/twyn/dependency_managers/managers/pypi_dependency_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88
@dataclass
99
class PypiDependencyManager(BaseDependencyManager):
1010
name = "pypi"
11+
"""Name of the PyPI package ecosystem."""
1112
trusted_packages_source = TopPyPiReference
13+
"""Reference source for trusted PyPI packages."""
1214
dependency_files = {UV_LOCK, POETRY_LOCK, REQUIREMENTS_TXT}
15+
"""Set of supported Python dependency file names."""

src/twyn/dependency_managers/utils.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33
from twyn.dependency_managers.managers.npm_dependency_manager import NpmDependencyManager
44
from twyn.dependency_managers.managers.pypi_dependency_manager import PypiDependencyManager
55

6+
DEPENDENCY_MANAGERS: list[type[BaseDependencyManager]] = [
7+
PypiDependencyManager,
8+
NpmDependencyManager,
9+
]
10+
"""List of available dependency manager classes."""
11+
12+
PACKAGE_ECOSYSTEMS = {x.name for x in DEPENDENCY_MANAGERS}
13+
"""Set of package ecosystem names from available dependency managers."""
14+
615

716
def get_dependency_manager_from_file(dependency_file: str) -> type[BaseDependencyManager]:
17+
"""Get dependency manager that can handle the given file."""
818
for manager in DEPENDENCY_MANAGERS:
919
if manager.matches_dependency_file(dependency_file):
1020
return manager
1121
raise NoMatchingDependencyManagerError
1222

1323

1424
def get_dependency_manager_from_name(name: str) -> type[BaseDependencyManager]:
25+
"""Get dependency manager by ecosystem name."""
1526
for manager in DEPENDENCY_MANAGERS:
1627
if manager.matches_ecosystem_name(name):
1728
return manager
1829
raise NoMatchingDependencyManagerError
19-
20-
21-
DEPENDENCY_MANAGERS: list[type[BaseDependencyManager]] = [
22-
PypiDependencyManager,
23-
NpmDependencyManager,
24-
]
25-
PACKAGE_ECOSYSTEMS = {x.name for x in DEPENDENCY_MANAGERS}

0 commit comments

Comments
 (0)