Skip to content

Commit 4e59a3b

Browse files
nodeeeeeeclaude
andcommitted
Fix note language override: move global declaration to top of main()
The 'global NOTE_LANGUAGE' was inside an 'if args.language:' block, which can cause subtle issues in Python 3.12+. Moved it to the top of main() so the global declaration is unconditional. Also added a confirmation print ("Note language: zh") when --language is explicitly passed, making it visible in the terminal that the language override took effect. Tests added: - Verify "Note language: zh" appears in subprocess output - Verify no language line when --language is not passed - AST check that 'global NOTE_LANGUAGE' is at function top level - Prompt selection returns Chinese when NOTE_LANGUAGE='zh' Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0655099 commit 4e59a3b

2 files changed

Lines changed: 39 additions & 48 deletions

File tree

note_generation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,7 @@ def _discover_lectures(course_dir: Path) -> list[LectureData]:
15641564
# ── CLI ───────────────────────────────────────────────────────────────────────
15651565

15661566
def main() -> None:
1567+
global NOTE_LANGUAGE
15671568
parser = argparse.ArgumentParser(description="Generate course lecture notes")
15681569
parser.add_argument("--course", metavar="ID")
15691570
parser.add_argument("--slides", metavar="PATH")
@@ -1590,8 +1591,8 @@ def main() -> None:
15901591
args = parser.parse_args()
15911592

15921593
if args.language:
1593-
global NOTE_LANGUAGE
15941594
NOTE_LANGUAGE = args.language
1595+
print(f"Note language: {NOTE_LANGUAGE}")
15951596

15961597
if args.course:
15971598
course_dir = COURSE_DATA_DIR / args.course

test/test_language_and_skip.py

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -156,65 +156,55 @@ def test_language_rejects_invalid(self):
156156
assert r.returncode != 0, "Invalid language should cause an error"
157157

158158
def test_language_overrides_constant(self):
159-
"""Verify that --language actually changes the NOTE_LANGUAGE global."""
160-
# Run a small Python snippet that imports note_generation and checks
161-
script = textwrap.dedent("""\
162-
import sys, os
163-
sys.argv = ['note_generation.py', '--course', '99999', '--language', 'zh']
164-
# Patch argparse to not exit on --course 99999
165-
import note_generation
166-
# At module level, NOTE_LANGUAGE is 'en'
167-
assert note_generation.NOTE_LANGUAGE == 'en', (
168-
f"Default should be 'en', got {note_generation.NOTE_LANGUAGE!r}")
169-
# Simulate main() parsing
170-
import argparse
171-
parser = argparse.ArgumentParser()
172-
parser.add_argument('--course')
173-
parser.add_argument('--language', choices=['en', 'zh'], default=None)
174-
args, _ = parser.parse_known_args()
175-
if args.language:
176-
note_generation.NOTE_LANGUAGE = args.language
177-
assert note_generation.NOTE_LANGUAGE == 'zh', (
178-
f"After --language zh, should be 'zh', got {note_generation.NOTE_LANGUAGE!r}")
179-
print('PASS')
180-
""")
181-
r = subprocess.run(
182-
[PYTHON, "-c", script],
183-
capture_output=True, text=True, timeout=15,
184-
cwd=str(PROJECT_DIR),
159+
"""Verify that --language zh is confirmed in subprocess output."""
160+
r = _run("note_generation.py", "--course", "99999",
161+
"--language", "zh", "--course-name", "Test")
162+
assert "Note language: zh" in r.stdout, (
163+
f"Language confirmation not found in output:\n{r.stdout}\n{r.stderr}"
185164
)
186-
assert "PASS" in r.stdout, (
187-
f"Language override test failed:\n{r.stdout}\n{r.stderr}"
165+
166+
def test_language_en_not_printed_when_default(self):
167+
"""When --language is not passed, no language line should be printed."""
168+
r = _run("note_generation.py", "--course", "99999", "--course-name", "Test")
169+
assert "Note language:" not in r.stdout, (
170+
f"Language line should not appear without --language flag:\n{r.stdout}"
188171
)
189172

190173
def test_prompt_selection_respects_language(self):
191174
"""Verify _P() returns Chinese prompts when NOTE_LANGUAGE is 'zh'."""
192-
script = textwrap.dedent("""\
193-
import sys
194-
sys.argv = ['test']
195-
import note_generation
196-
# Default: English
175+
import note_generation
176+
# Default: English
177+
original = note_generation.NOTE_LANGUAGE
178+
try:
197179
note_generation.NOTE_LANGUAGE = 'en'
198180
en_sys = note_generation._P('system')
199-
assert 'diagram' in en_sys.lower() or 'note' in en_sys.lower(), (
181+
assert 'note' in en_sys.lower(), (
200182
'English system prompt should contain English text')
201-
# Switch to Chinese
183+
202184
note_generation.NOTE_LANGUAGE = 'zh'
203185
zh_sys = note_generation._P('system')
204-
# Chinese prompt should contain Chinese characters
205-
import re
206-
cjk = re.compile(r'[\\u4e00-\\u9fff]')
207-
assert cjk.search(zh_sys), 'Chinese system prompt should contain CJK characters'
186+
assert _CJK_RE.search(zh_sys), (
187+
'Chinese system prompt should contain CJK characters')
208188
assert en_sys != zh_sys, 'English and Chinese prompts should differ'
209-
print('PASS')
210-
""")
211-
r = subprocess.run(
212-
[PYTHON, "-c", script],
213-
capture_output=True, text=True, timeout=15,
214-
cwd=str(PROJECT_DIR),
189+
finally:
190+
note_generation.NOTE_LANGUAGE = original
191+
192+
def test_global_declaration_at_function_top(self):
193+
"""Ensure 'global NOTE_LANGUAGE' is at the top of main(), not inside if."""
194+
import inspect, ast
195+
import note_generation
196+
src = inspect.getsource(note_generation.main)
197+
tree = ast.parse(textwrap.dedent(src))
198+
func = tree.body[0]
199+
# First statement in function body should be (or contain) the global
200+
first_stmts = func.body[:3] # check first few statements
201+
has_global = any(
202+
isinstance(s, ast.Global) and 'NOTE_LANGUAGE' in s.names
203+
for s in first_stmts
215204
)
216-
assert "PASS" in r.stdout, (
217-
f"Prompt selection test failed:\n{r.stdout}\n{r.stderr}"
205+
assert has_global, (
206+
"'global NOTE_LANGUAGE' should be at the top of main(), "
207+
"not inside a conditional block"
218208
)
219209

220210

0 commit comments

Comments
 (0)