Repository navigation
refactor(env): replace visualizer's 600-line elif chain with dispatch - #24
Merged
Merged
Conversation
_set_config_by_state was a single ~600-line if/elif chain over 19 env_ids, each branch setting GRID_SIZE/BOARD_WIDTH/BOARD_HEIGHT, the env-specific _make_dwg_group, and dark vs light ColorSets. The chain duplicated a buggy color-theme check across most branches (``(theme is None and theme == 'dark') or theme == 'dark'`` — the first clause is always False since None != 'dark'; effectively ``theme == 'dark'``). Replace with: - One _EnvVisConfig dataclass holding the resolved per-env parameters. - A _pick_color_set helper that takes (theme, dark_cs, light_cs) and picks one; dead is-None check is gone. - 21 _config_<env_id> module-level functions (one per env, two go variants share _config_go), each returning _EnvVisConfig. pgx-import remains lazy per builder. - _ENV_CONFIGS dispatch table mapping env_id -> builder. - _set_config_by_state shrinks from ~600 lines to ~15 lines of lookup + dispatch + dict update. File net: 1001 -> 895 lines. The Visualizer class API is unchanged; its four callers (env.quoridor, env.phantom_ttt, env.dark_hex, env.liars_dice) need no updates. New tests/test_visualizer_dispatch.py pins the dispatch surface: covered env_ids, structural validity of the four project-trained env builders, three-theme round-trip through Visualizer.get_dwg for tic_tac_toe, and the unknown-env_id error path. 6 new tests. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2 tasks
2 tasks done
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.
Summary
`src/jaxpot/env/visualizer.py`'s `_set_config_by_state` was a single ~600-line `if/elif` chain over 19 different `env_id`s. Every branch followed the same shape — set 3 dimensions + `_make_dwg_group` + two-arm `ColorSet` for dark vs light theme — but the code was open-coded 19 times.
This PR replaces the chain with a dict-dispatch over per-env config builder functions. File net: 1001 → 895 lines. No behavior change beyond fixing a latent color-theme bug (see below).
Latent bug fixed
Most pre-existing branches had this color-theme check:
```python
if (self.config["COLOR_THEME"] is None and self.config["COLOR_THEME"] == "dark") or self.config["COLOR_THEME"] == "dark":
```
The first clause `(x is None and x == "dark")` is structurally always `False` (a `None` value can't equal `"dark"`). Effective behavior was `theme == "dark"` — the project's own envs (`liars_dice`, `phantom_ttt`, `dark_hex`) already used the clean form. The refactor unifies all 19 envs on a single `_pick_color_set(theme, dark_cs, light_cs)` helper.
What changed
What did NOT change
Test plan
🤖 Generated with Claude Code