Skip to content

Commit e1f760f

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 e1f760f

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_print_dry_run_message(
4141

4242
def test__validate_stage_name() -> None:
4343
"""Test the :func:`validate_stage_name` method."""
44-
StagedScript._validate_stage_name("valid")
44+
StagedScript.validate_stage_name("valid")
4545

4646

4747
@pytest.mark.parametrize(
@@ -53,7 +53,7 @@ def test__validate_stage_name_raises(stage_name: str) -> None:
5353
ValueError,
5454
match=f"'{stage_name}' must contain only lowercase letters",
5555
):
56-
StagedScript._validate_stage_name(stage_name)
56+
StagedScript.validate_stage_name(stage_name)
5757

5858

5959
def test__begin_stage(

0 commit comments

Comments
 (0)