Skip to content

Commit 8575a3b

Browse files
author
Ronald Tse
committed
chore: ruff lint cleanup (auto-fixable + manual)
Fixes all ruff errors flagged by CI: - I001: import ordering (auto-fixed) - F401: unused imports removed - F841: unused pipeline variable removed from cmd_publish - B905: explicit strict=False on zips where lengths intentionally vary - B017: narrower AttributeError instead of bare Exception for frozen dataclass test - E501: wrapped long NotImplementedError signatures in trainer.py 37 tests still pass. Ruff check src tests clean.
1 parent 506a7a4 commit 8575a3b

20 files changed

Lines changed: 54 additions & 61 deletions

File tree

src/cli.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,6 @@ def cmd_export(args: argparse.Namespace) -> int:
132132

133133

134134
def cmd_publish(args: argparse.Namespace) -> int:
135-
from framework.pipeline import TrainingPipeline
136-
137-
pipeline = TrainingPipeline.from_config(
138-
task_name=args.task,
139-
data_root=args.data_root,
140-
out_root=args.out_root,
141-
tasks_root=args.tasks_root,
142-
)
143135
repo_id = args.repo or f"interscript/{args.task}"
144136
print(f"Would upload {args.out_root} to huggingface.co/{repo_id}")
145137
return 0

src/framework/__init__.py

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

99
from framework.config import TaskConfig, load_task_config
1010
from framework.data import DataModule, DataSplit, Example
11-
from framework.model import ModelModule, ModelKind
12-
from framework.trainer import BaseTrainer, TrainingState
1311
from framework.evaluator import BaseEvaluator, MetricSet
14-
from framework.exporter import OnnxExporter, ExportResult
12+
from framework.exporter import ExportResult, OnnxExporter
13+
from framework.model import ModelKind, ModelModule
1514
from framework.pipeline import TrainingPipeline
1615
from framework.registry import (
1716
register_data_module,
18-
register_model_module,
1917
register_evaluator,
18+
register_model_module,
2019
resolve_data_module,
21-
resolve_model_module,
2220
resolve_evaluator,
21+
resolve_model_module,
2322
)
23+
from framework.trainer import BaseTrainer, TrainingState
2424

2525
__all__ = [
2626
"TaskConfig",

src/framework/config.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DataConfig:
2626
cleaner: str = "basic"
2727

2828
@classmethod
29-
def from_dict(cls, raw: dict[str, Any]) -> "DataConfig":
29+
def from_dict(cls, raw: dict[str, Any]) -> DataConfig:
3030
return cls(
3131
module=raw["module"],
3232
source=raw["source"],
@@ -50,7 +50,7 @@ class ModelConfig:
5050
lora_alpha: int = 32
5151

5252
@classmethod
53-
def from_dict(cls, raw: dict[str, Any]) -> "ModelConfig":
53+
def from_dict(cls, raw: dict[str, Any]) -> ModelConfig:
5454
return cls(
5555
module=raw["module"],
5656
teacher_name=raw["teacher_name"],
@@ -80,7 +80,7 @@ class TrainConfig:
8080
out_dir: str = "models"
8181

8282
@classmethod
83-
def from_dict(cls, raw: dict[str, Any]) -> "TrainConfig":
83+
def from_dict(cls, raw: dict[str, Any]) -> TrainConfig:
8484
return cls(
8585
epochs=raw.get("epochs", 3),
8686
batch_size=raw.get("batch_size", 16),
@@ -106,7 +106,7 @@ class EvalConfig:
106106
batch_size: int = 32
107107

108108
@classmethod
109-
def from_dict(cls, raw: dict[str, Any]) -> "EvalConfig":
109+
def from_dict(cls, raw: dict[str, Any]) -> EvalConfig:
110110
return cls(
111111
module=raw["module"],
112112
metric=raw["metric"],
@@ -126,7 +126,7 @@ class ExportConfig:
126126
quantize: bool = False
127127

128128
@classmethod
129-
def from_dict(cls, raw: dict[str, Any]) -> "ExportConfig":
129+
def from_dict(cls, raw: dict[str, Any]) -> ExportConfig:
130130
return cls(
131131
opset=raw.get("opset", 17),
132132
dynamic_axes=raw.get("dynamic_axes", {"input_ids": {0: "batch", 1: "seq"}}),
@@ -152,7 +152,7 @@ class TaskConfig:
152152
export: ExportConfig
153153

154154
@classmethod
155-
def from_dict(cls, name: str, raw: dict[str, Any]) -> "TaskConfig":
155+
def from_dict(cls, name: str, raw: dict[str, Any]) -> TaskConfig:
156156
return cls(
157157
name=name,
158158
description=raw.get("description", ""),

src/framework/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
from __future__ import annotations
1313

1414
from abc import ABC, abstractmethod
15+
from collections.abc import Iterator, Sequence
1516
from dataclasses import dataclass
1617
from pathlib import Path
17-
from typing import Iterator, Sequence
1818

1919
from framework.config import DataConfig
2020

src/framework/evaluator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
from abc import ABC, abstractmethod
1414
from collections import Counter
15+
from collections.abc import Sequence
1516
from dataclasses import dataclass, field
16-
from typing import Sequence
1717

1818

1919
@dataclass(frozen=True)
@@ -110,7 +110,7 @@ def accuracy(pred: Sequence[str], gold: Sequence[str]) -> float:
110110
"""Exact-match accuracy across the corpus."""
111111
if not pred:
112112
return 0.0
113-
correct = sum(1 for p, g in zip(pred, gold) if p == g)
113+
correct = sum(1 for p, g in zip(pred, gold, strict=False) if p == g)
114114
return correct / len(pred)
115115

116116

@@ -121,8 +121,8 @@ def most_common_error_pairs(
121121
) -> dict[str, float]:
122122
"""Frequency of (gold, pred) character-level substitutions."""
123123
counts: Counter[tuple[str, str]] = Counter()
124-
for p, g in zip(pred, gold):
125-
for pc, gc in zip(p, g):
124+
for p, g in zip(pred, gold, strict=False):
125+
for pc, gc in zip(p, g, strict=False):
126126
if pc != gc:
127127
counts[(gc, pc)] += 1
128128
total = sum(counts.values())

src/framework/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
from __future__ import annotations
1313

1414
from abc import ABC, abstractmethod
15+
from collections.abc import Sequence
1516
from dataclasses import dataclass
1617
from enum import Enum
17-
from typing import Any, Sequence
18+
from typing import Any
1819

1920

2021
class ModelKind(str, Enum):

src/framework/pipeline.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@
99
from __future__ import annotations
1010

1111
import importlib
12+
from collections.abc import Callable
1213
from dataclasses import dataclass
1314
from pathlib import Path
14-
from typing import Any, Callable
1515

1616
from framework.config import TaskConfig
1717
from framework.data import DataModule
1818
from framework.evaluator import BaseEvaluator, MetricSet
1919
from framework.exporter import ExportResult, OnnxExporter
2020
from framework.model import ModelModule
21-
from framework.trainer import BaseTrainer, TrainConfig
2221
from framework.registry import (
2322
resolve_data_module,
2423
resolve_evaluator,
2524
resolve_model_module,
2625
)
27-
26+
from framework.trainer import BaseTrainer
2827

2928
TrainerFactory = Callable[..., BaseTrainer]
3029

@@ -73,7 +72,7 @@ def __init__(
7372
model_class: type[ModelModule] | None = None,
7473
evaluator_class: type[BaseEvaluator] | None = None,
7574
exporter: OnnxExporter | None = None,
76-
trainer_factory: "TrainerFactory | None" = None,
75+
trainer_factory: TrainerFactory | None = None,
7776
) -> None:
7877
self.config = config
7978
self.data_root = data_root
@@ -92,7 +91,7 @@ def from_config(
9291
data_root: Path,
9392
out_root: Path,
9493
tasks_root: Path | None = None,
95-
) -> "TrainingPipeline":
94+
) -> TrainingPipeline:
9695
"""Build a pipeline from ``src/tasks/<task_name>/config.yaml``."""
9796
from framework.config import load_task_config
9897

@@ -139,7 +138,6 @@ def run(
139138

140139
model = self.build_model()
141140

142-
from framework.trainer import BaseTrainer
143141

144142
trainer = self._construct_trainer(model, data)
145143
state = trainer.fit(max_steps=max_steps)

src/framework/trainer.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from abc import ABC, abstractmethod
1919
from dataclasses import dataclass, field
2020
from pathlib import Path
21-
from typing import Any, Callable
21+
from typing import Any
2222

2323
from framework.config import TrainConfig
2424
from framework.data import DataModule
@@ -148,11 +148,15 @@ def _checkpoint(self, tag: str) -> None:
148148
class FineTuneTrainer(BaseTrainer):
149149
"""Teacher trainer: supervised CE loss on gold labels."""
150150

151-
def compute_loss(self, batch: Any) -> tuple[Any, dict[str, float]]: # pragma: no cover - torch-bound
152-
raise NotImplementedError("FineTuneTrainer requires torch; implement in tasks that use it")
151+
def compute_loss(self, batch): # pragma: no cover - torch-bound
152+
raise NotImplementedError(
153+
"FineTuneTrainer requires torch; implement in tasks that use it"
154+
)
153155

154156
def make_optimizer(self) -> Any: # pragma: no cover - torch-bound
155-
raise NotImplementedError("FineTuneTrainer requires torch; implement in tasks that use it")
157+
raise NotImplementedError(
158+
"FineTuneTrainer requires torch; implement in tasks that use it"
159+
)
156160

157161

158162
class DistillTrainer(BaseTrainer):
@@ -174,8 +178,10 @@ def __init__(
174178
super().__init__(config, model, data, out_dir)
175179
self.teacher = teacher
176180

177-
def compute_loss(self, batch: Any) -> tuple[Any, dict[str, float]]: # pragma: no cover - torch-bound
178-
raise NotImplementedError("DistillTrainer requires torch; implement in tasks that use it")
181+
def compute_loss(self, batch): # pragma: no cover - torch-bound
182+
raise NotImplementedError(
183+
"DistillTrainer requires torch; implement in tasks that use it"
184+
)
179185

180186
def make_optimizer(self) -> Any: # pragma: no cover - torch-bound
181187
raise NotImplementedError("DistillTrainer requires torch; implement in tasks that use it")

src/tasks/rababa_arabic/data.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
from __future__ import annotations
1313

1414
import random
15-
from collections import Counter
15+
from collections.abc import Sequence
1616
from pathlib import Path
17-
from typing import Sequence
1817

19-
from framework.config import DataConfig
2018
from framework.data import DataModule, DataSplit, Example, PreparedData
2119
from framework.registry import register_data_module
2220

src/tasks/rababa_arabic/metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Sequence
14+
from collections.abc import Sequence
1515

1616
from framework.evaluator import (
1717
BaseEvaluator,
@@ -30,7 +30,7 @@ class DEREvaluator(BaseEvaluator):
3030

3131
def compute_metric(self, predictions: Sequence[str], gold: Sequence[str]) -> float:
3232
total = 0.0
33-
for pred, ref in zip(predictions, gold):
33+
for pred, ref in zip(predictions, gold, strict=False):
3434
total += char_error_rate(pred, ref)
3535
return total / len(predictions)
3636

0 commit comments

Comments
 (0)