Skip to content

fix: resolve NameError in settings Appearance tab by defining select_widget#51

Merged
AkshajSinghal merged 6 commits into
TruFoundation:mainfrom
zza-830:fix/settings-select-widget-nameerror
Jun 10, 2026
Merged

fix: resolve NameError in settings Appearance tab by defining select_widget#51
AkshajSinghal merged 6 commits into
TruFoundation:mainfrom
zza-830:fix/settings-select-widget-nameerror

Conversation

@zza-830

@zza-830 zza-830 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Problem

The _refresh_form() method in settings.py creates a Select widget inline inside add_field() but then references an undefined select_widget variable to call watch_value() and add a duplicate field. This causes a NameError crash whenever the Appearance category is selected in the settings TUI.

Closes #42

Fix

  • Store the Select widget in a local select_widget variable before passing it to add_field()
  • Remove the duplicate add_field("Theme:", select_widget) call (theme field was being added twice)
  • Remove the unnecessary watch_value call (changes are already tracked via on_select_changed event handler)

Testing

tests/test_data.py::test_run_csv_view_file_not_found PASSED
tests/test_data.py::test_run_csv_view_empty_file PASSED
tests/test_data.py::test_run_csv_view_shows_limited_rows PASSED
tests/test_cli_argv.py::test_app_with_lower_does_not_mutate_original_argv PASSED

Summary by CodeRabbit

  • Refactor

    • Streamlined theme selector implementation in settings by consolidating widget construction and event handling.
  • Style

    • Removed unnecessary inline comments.

…widget

The  method created a Select widget inline inside
 but then referenced an undefined  variable
to call  and add a duplicate field, causing a NameError
crash whenever the Appearance category was selected.

Fix: store the Select widget in a local variable before passing it to
, and remove the duplicate field addition.

Closes TruFoundation#42
@AkshajSinghal

Copy link
Copy Markdown
Collaborator

Swapping typer.secho for a plain print() might hide important errors in a busy terminal. Use the project logger or a Rich warning instead so users actually see if state loading fails.

Fix that, add a quick regression test, and it’s good to go.

Ziang Zhang added 3 commits June 8, 2026 14:32
The test_run_help_prints_docstring_for_known_command test was failing
because the fake kernel lacked a _import_module method, causing the
module import to fail silently and fall through to the error message.

Add a working _fake_import_module that uses importlib to resolve
the module path, allowing run_help to read and print the docstring.
The  function referenced a bare name  that was
never imported or defined, causing a NameError crash on every invocation.

Fix: move  before the conditional block so it is
always defined when reached by the comparison at the end of the function.

Closes TruFoundation#40
@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 595b8475-0ff7-4778-88af-c2dc34dbf112

📥 Commits

Reviewing files that changed from the base of the PR and between 5e0c4ca and 5ffd623.

📒 Files selected for processing (1)
  • trushell/cli.py

📝 Walkthrough

Walkthrough

The PR fixes a NameError in the settings theme selector by assigning the Select widget to a variable and passing it to add_field, then removes inline event handling in favor of the existing on_select_changed handler. A non-functional CLI comment is also removed.

Changes

Settings Theme Widget and Minor Cleanup

Layer / File(s) Summary
Theme Select widget variable assignment
trushell/commands/settings.py
The Theme Select widget (with id="theme_selector") is created into a select_widget variable and passed to add_field. The prior per-widget watch_value(...) call is removed, delegating theme changes to the app's existing on_select_changed handler.
CLI comment cleanup
trushell/cli.py
Removes a three-line explanatory comment from app_with_lower() describing command normalization; surrounding logic is unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A widget unshackled, now properly born,
With select_widget named in the morn.
Watch-hooks retired, let handlers reign—
The settings bloom brightly once again! 🐰✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR includes out-of-scope changes: (1) modifications to tests/test_help_docs.py with a trailing comma that are unrelated to the select_widget fix, and (2) removal of an explanatory comment in trushell/cli.py unrelated to the issue. Revert trailing comma change in test_help_docs.py and comment removal in cli.py; keep only the select_widget fix in settings.py to stay focused on issue #42.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: resolve NameError in settings Appearance tab by defining select_widget' accurately describes the main fix applied to resolve the undefined select_widget variable issue.
Linked Issues check ✅ Passed The PR successfully addresses issue #42 by defining select_widget before use, removing duplicate add_field calls, and eliminating unnecessary watch_value calls as required.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@zza-830

zza-830 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Regarding the print() concern — this PR only changes settings.py (defining select_widget before passing to add_field). It doesn't introduce any new print() calls. The merge conflict with upstream/main has also been resolved.

Ready for another look!

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
trushell/commands/settings.py (1)

77-80: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Critical: duplicate initialization overwrites dirty_settings.

Line 77 initializes dirty_settings as a copy of settings, but line 80 immediately overwrites it with an empty dict. This causes the initial copy to be discarded, and dirty_settings will always start empty rather than preserving the loaded settings as a baseline.

Remove one of these assignments depending on the intended behavior:

  • Keep line 77 if dirty_settings should start as a copy of loaded settings
  • Keep line 80 if dirty_settings should only track user edits (empty initially)
🔧 Proposed fix

If the intent is to track only changes (empty initially), remove line 77:

     def __init__(self, **kwargs) -> None:
         super().__init__(**kwargs)
         self.manager = SettingsManager()
         self.settings = self.manager.load()
-        self.dirty_settings = dict(self.settings)
         self.selected_category = "General"
         # Track changes to settings without losing data when switching categories
         self.dirty_settings: dict = {}

If the intent is to start with a copy of settings, remove line 80 and fix the type annotation:

     def __init__(self, **kwargs) -> None:
         super().__init__(**kwargs)
         self.manager = SettingsManager()
         self.settings = self.manager.load()
         self.dirty_settings = dict(self.settings)
         self.selected_category = "General"
         # Track changes to settings without losing data when switching categories
-        self.dirty_settings: dict = {}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@trushell/commands/settings.py` around lines 77 - 80, The code initializes
self.dirty_settings twice so the initial copy (self.dirty_settings =
dict(self.settings)) is immediately overwritten by an empty dict; decide
intended behavior and remove the conflicting line: if dirty_settings should
start as a copy of loaded settings, delete the second assignment
(self.dirty_settings: dict = {}) and keep the copy (ensure type annotation is
removed or applied correctly), otherwise delete the first copy assignment and
keep the empty initialization so dirty_settings only tracks user edits; update
only the assignments around self.dirty_settings near selected_category to
preserve the correct initial state.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_help_docs.py`:
- Around line 10-12: The helper function _fake_import_module is dead because
fake_kernel._import_module is later overwritten by a lambda returning
fake_module; remove the unused _fake_import_module definition and the
intermediate assignment to fake_kernel._import_module, and keep only the final
lambda that returns fake_module (ensure references to _fake_import_module are
removed and tests still import fake_module via the final
fake_kernel._import_module lambda).

---

Outside diff comments:
In `@trushell/commands/settings.py`:
- Around line 77-80: The code initializes self.dirty_settings twice so the
initial copy (self.dirty_settings = dict(self.settings)) is immediately
overwritten by an empty dict; decide intended behavior and remove the
conflicting line: if dirty_settings should start as a copy of loaded settings,
delete the second assignment (self.dirty_settings: dict = {}) and keep the copy
(ensure type annotation is removed or applied correctly), otherwise delete the
first copy assignment and keep the empty initialization so dirty_settings only
tracks user edits; update only the assignments around self.dirty_settings near
selected_category to preserve the correct initial state.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 0d7d41d7-964c-4095-8973-967d64e63481

📥 Commits

Reviewing files that changed from the base of the PR and between 7ab2e53 and 9d2eefb.

📒 Files selected for processing (3)
  • tests/test_help_docs.py
  • trushell/cli.py
  • trushell/commands/settings.py
💤 Files with no reviewable changes (1)
  • trushell/cli.py

Comment thread tests/test_help_docs.py Outdated
@AkshajSinghal

AkshajSinghal commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

@zza-830 It's perfect, just remove the dead code in test_help_docs.py, as CodeRabbit suggested. The _fake_import_module function is defined but immediately overwritten by the lambda on line 31, and it'll be ready to be merged

@zza-830

zza-830 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

Done! Removed the dead function and the import. The test now uses a simple lambda override for . CI is pending — may need maintainer approval for the fork PR to run.

@AkshajSinghal AkshajSinghal merged commit 2540859 into TruFoundation:main Jun 10, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

settingsApp._refresh_form uses undefined select_widget

2 participants