Skip to content

Commit b7c26fc

Browse files
committed
refactor: Switch method to module function
I should have done this ages ago. Instead of `_validate_stage_name` being a static method in the `StagedScript` class, make it a function in the `staged_script` module. We then no longer need to ignore the MyPy error about `__class__` being undefined, and Ruff's SLF linter won't complain about private member access.
1 parent 9bc25f7 commit b7c26fc

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ extend-select = [
116116
"RUF",
117117
"S",
118118
"SIM",
119-
# "SLF",
119+
"SLF",
120120
"SLOT",
121121
"T10",
122122
"T20",

staged_script/staged_script.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@
4343
rich.traceback.install()
4444

4545

46+
def validate_stage_name(stage_name: str) -> None:
47+
"""
48+
Validate a stage name.
49+
50+
Ensure a stage name consists of only lowercase letters. This is
51+
both to simplify implementation details within the
52+
:class:`StagedScript` class, and to provide the best user experience
53+
for users of your :class:`StagedScript` subclasses.
54+
55+
Args:
56+
stage_name: The name of the stage.
57+
58+
Raises:
59+
ValueError: If the stage name is invalid.
60+
"""
61+
if not re.match("^[a-z]+$", stage_name):
62+
message = (
63+
f"Stage name {stage_name!r} must contain only lowercase letters."
64+
)
65+
raise ValueError(message)
66+
67+
4668
class StagedScript:
4769
"""
4870
The base class for all staged scripts.
@@ -147,7 +169,7 @@ def __init__(
147169
and optionally pass in additional arguments.
148170
"""
149171
for stage in stages:
150-
self._validate_stage_name(stage)
172+
validate_stage_name(stage)
151173
self.args = Namespace()
152174
self.commands_executed: list[str] = []
153175
self.console = Console(
@@ -165,29 +187,6 @@ def __init__(
165187
self.stages_to_run: set[str] = set()
166188
self.start_time = datetime.now(tz=timezone.utc)
167189

168-
@staticmethod
169-
def _validate_stage_name(stage_name: str) -> None:
170-
"""
171-
Validate the stage name.
172-
173-
Ensure the stage name consists of only lowercase letters. This
174-
is both to simplify implementation details within the class, and
175-
to provide the best user experience for users of your
176-
:class:`StagedScript` subclasses.
177-
178-
Args:
179-
stage_name: The name of the stage.
180-
181-
Raises:
182-
ValueError: If the stage name is invalid.
183-
"""
184-
if not re.match("^[a-z]+$", stage_name):
185-
message = (
186-
f"Stage name {stage_name!r} must contain only lowercase "
187-
"letters."
188-
)
189-
raise ValueError(message)
190-
191190
#
192191
# The `stage` decorator.
193192
#
@@ -245,9 +244,7 @@ def stage(stage_name: str, heading: str) -> Callable:
245244
heading: A heading message to print indicating what will
246245
happen in the stage.
247246
"""
248-
__class__._validate_stage_name( # type: ignore[name-defined]
249-
stage_name
250-
)
247+
validate_stage_name(stage_name)
251248

252249
def decorator(func: Callable) -> Callable:
253250
def get_phase_method( # noqa: D417

test/test_staged_script.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from rich.console import Console
1919

2020
from staged_script import StagedScript, StageDuration
21+
from staged_script.staged_script import validate_stage_name
2122

2223

2324
@pytest.fixture
@@ -39,21 +40,21 @@ def test_print_dry_run_message(
3940
assert expected in captured.out
4041

4142

42-
def test__validate_stage_name() -> None:
43-
"""Test the :func:`validate_stage_name` method."""
44-
StagedScript._validate_stage_name("valid")
43+
def test_validate_stage_name() -> None:
44+
"""Test the :func:`validate_stage_name` function."""
45+
validate_stage_name("valid")
4546

4647

4748
@pytest.mark.parametrize(
4849
"stage_name", ["Uppercase", "spa ces", "hyphen-ated", "under_scores"]
4950
)
50-
def test__validate_stage_name_raises(stage_name: str) -> None:
51+
def test_validate_stage_name_raises(stage_name: str) -> None:
5152
"""Ensure :func:`validate_stage_name` raises an exception when needed."""
5253
with pytest.raises(
5354
ValueError,
5455
match=f"'{stage_name}' must contain only lowercase letters",
5556
):
56-
StagedScript._validate_stage_name(stage_name)
57+
validate_stage_name(stage_name)
5758

5859

5960
def test__begin_stage(

0 commit comments

Comments
 (0)