Skip to content

Commit 2fcc594

Browse files
committed
Fix all ruff lint errors in tests/ and scripts/
- I001: Fix import ordering in test files and convert_hf_tokenizer.py - C401: Replace generator with set comprehension - B023: Replace lambda with direct method reference (parts.append) - E501: Replace long pattern strings with PAT_GPT2 constant - F401: Remove unused imports (regex, save_model in test_trainer.py) - F841: Remove unused variable assignment
1 parent 38a6fb7 commit 2fcc594

6 files changed

Lines changed: 15 additions & 19 deletions

File tree

scripts/convert_hf_tokenizer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import sys
2222
from pathlib import Path
2323

24-
2524
# ---------------------------------------------------------------------------
2625
# GPT-2 ByteLevel mapping: bytes (0-255) ↔ visible Unicode characters
2726
# ---------------------------------------------------------------------------
@@ -205,7 +204,7 @@ def _detect_byte_mapping(vocab: dict[str, int]) -> list[int] | None:
205204
still_missing = [b for b in range(256) if bytes_maps[b] < 0]
206205
if still_missing:
207206
# Collect all single-char tokens not yet assigned
208-
assigned_ids = set(bytes_maps[b] for b in range(256) if bytes_maps[b] >= 0)
207+
assigned_ids = {bytes_maps[b] for b in range(256) if bytes_maps[b] >= 0}
209208
unassigned = [
210209
(ord(c), tid)
211210
for c, tid in char_to_id.items()

tests/test_fuzz.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_stream_decode_random(self):
9393

9494
# Streaming decode
9595
parts: list[str] = []
96-
decoder = tok.stream_decode(lambda s: parts.append(s))
96+
decoder = tok.stream_decode(parts.append)
9797
for tid in ids:
9898
decoder(tid)
9999
streamed = "".join(parts)

tests/test_hf_models.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_streaming_decode(self, model_key, hf_id, desc, pattern):
119119
continue
120120
ids = tok.encode(text)
121121
parts: list[str] = []
122-
decoder = tok.stream_decode(lambda s: parts.append(s))
122+
decoder = tok.stream_decode(parts.append)
123123
for tid in ids:
124124
decoder(tid)
125125
assert "".join(parts) == text
@@ -165,8 +165,7 @@ class TestQwen25Specific:
165165

166166
def test_chinese_text(self):
167167
"""Chinese text should encode and decode correctly."""
168-
pattern = r"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"
169-
tok = Tokenizer.from_file("models/qwen25.tbm", pat_str=pattern)
168+
tok = Tokenizer.from_file("models/qwen25.tbm", pat_str=PAT_GPT2)
170169

171170
cn_texts = [
172171
"他是一个独自一人划着小船在墨西哥湾大海流打鱼的老人",
@@ -181,8 +180,7 @@ def test_chinese_text(self):
181180

182181
def test_code_text(self):
183182
"""Code snippets should round-trip."""
184-
pattern = r"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"
185-
tok = Tokenizer.from_file("models/qwen25.tbm", pat_str=pattern)
183+
tok = Tokenizer.from_file("models/qwen25.tbm", pat_str=PAT_GPT2)
186184

187185
code = '''def fibonacci(n):
188186
if n <= 1:
@@ -199,8 +197,7 @@ class TestPhi2Specific:
199197

200198
def test_code_roundtrip(self):
201199
"""Code snippets should round-trip with Phi-2."""
202-
pattern = r"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"
203-
tok = Tokenizer.from_file("models/phi2.tbm", pat_str=pattern)
200+
tok = Tokenizer.from_file("models/phi2.tbm", pat_str=PAT_GPT2)
204201

205202
code = "def hello(): return 'world'"
206203
ids = tok.encode(code)
@@ -209,8 +206,7 @@ def test_code_roundtrip(self):
209206

210207
def test_english_text(self):
211208
"""English text should round-trip accurately."""
212-
pattern = r"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"
213-
tok = Tokenizer.from_file("models/phi2.tbm", pat_str=pattern)
209+
tok = Tokenizer.from_file("models/phi2.tbm", pat_str=PAT_GPT2)
214210

215211
text = "The quick brown fox jumps over the lazy dog."
216212
ids = tok.encode(text)

tests/test_models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""Tests for pre-built .tbm models — verify correctness against tiktoken."""
22

33
import pytest
4+
45
from tinybpe import Tokenizer
56

67
# Try importing tiktoken — tests are skipped if not available
78
try:
89
import tiktoken
910

10-
HAS_TIKTOKEN = True
11+
HAS_TIKTOKEN = True # noqa: F401
1112
except ImportError:
12-
HAS_TIKTOKEN = False
13+
HAS_TIKTOKEN = False # noqa: F401
1314

1415

1516
# ---------------------------------------------------------------------------
@@ -149,7 +150,7 @@ def test_streaming_decode(self, name, desc):
149150
continue
150151
ids = tok.encode(text)
151152
parts: list[str] = []
152-
decoder = tok.stream_decode(lambda s: parts.append(s))
153+
decoder = tok.stream_decode(parts.append)
153154
for tid in ids:
154155
decoder(tid)
155156
assert "".join(parts) == text

tests/test_tokenizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pathlib import Path
44

5-
from tinybpe import Tokenizer, load_model, save_model, save_vocab, load_vocab
5+
from tinybpe import Tokenizer, load_model, load_vocab, save_model, save_vocab
66

77
TESTS_DIR = Path(__file__).parent
88
FILE_SIMPLE = str(TESTS_DIR / "simple")

tests/test_trainer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Tests for the TinyBPE Trainer."""
22

3-
import regex as re
4-
from tinybpe import Trainer, Tokenizer, save_model, load_model
53
from pathlib import Path
64

5+
from tinybpe import Tokenizer, Trainer, load_model
6+
77
TESTS_DIR = Path(__file__).parent
88

99

@@ -112,7 +112,7 @@ class TestTrainerEdgeCases:
112112
def test_empty_text(self):
113113
"""Empty text should produce a valid but idle trainer."""
114114
try:
115-
trainer = Trainer("")
115+
Trainer("") # should raise ValueError
116116
except ValueError:
117117
# Expected: empty list not allowed
118118
pass

0 commit comments

Comments
 (0)