[codex] reduce command validation duplication#42
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
track/synth/effect の各コマンドで、
track・device・parameterの入力検証や範囲チェックがコマンドごとに重複していました。この重複により、同じ意味の検証ロジックが複数箇所に散らばり、将来の機能追加時にメッセージ不一致や修正漏れが起きやすい状態でした。
ユーザー影響
今回の変更は外部仕様を変えるものではなく、同じ不正入力に対して同じエラーコード (
INVALID_ARGUMENT) と一貫したヒントを返しやすくするための内部改善です。結果として、CLI 利用者が受け取るエラーメッセージの揺れを抑え、コマンド追加後も挙動の安定性を維持しやすくなります。
根本原因
コマンド実装ごとに
require_non_negative(...)と個別の条件分岐が直接書かれており、バリデーションの責務が分散していました。同じ検証規則(例: track は 0 以上)が複製されたため、修正コストと見落としリスクが増えていました。
対応内容
src/ableton_cli/commands/_validation.pyに再利用用のバリデータを追加しました。require_track_indexrequire_device_indexrequire_parameter_indexrequire_float_in_rangeあわせて、以下のコマンド実装を共通ヘルパー経由にリファクタしました。
src/ableton_cli/commands/track.pysrc/ableton_cli/commands/synth.pysrc/ableton_cli/commands/effect.pyこれにより、track/device/parameter の検証と value 範囲検証の重複を削減し、単一の変更点から各コマンドへ反映できる構造に寄せています。
テストと検証
TDD として先に検証ユーティリティのテストを追加し、実装後に全チェックを通しています。
追加テスト:
tests/commands/test_validation.py実行したチェック:
uv run python -m ableton_cli.dev_checksuv run ruff check .uv run ruff format --check .uv run pytestいずれも成功しています(
pytest: 353 passed)。