From 3adccd97c2a66686ec8ccf5ff3361479734eb29c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mai=20Minh=20Ho=C3=A0ng?= <135428813+Hoang130203@users.noreply.github.com> Date: Thu, 26 Mar 2026 14:25:53 +0700 Subject: [PATCH] fix(checkbox): fix NameError Token.Error and variable scoping in _get_choice_tokens Two bugs fixed in checkbox.py: 1. NameError on validation failure: get_prompt_tokens() referenced `Token.Error` from pygments, but Token was never imported in this module (only in __init__.py). This caused a NameError crash whenever ic.answered_correctly was False. Fixed by using a plain style class string ('class:error') consistent with the rest of the prompt_toolkit style tuples in this file. 2. Variable scoping bug in append(): Inside _get_choice_tokens, the inner function append(index, line) was referencing `choice` (the outer for-loop variable) instead of the `line` parameter for indices [2] and [3]. While this happened to work because append() is called immediately in the loop body, it is fragile, misleading, and a latent bug waiting to happen if the call is ever deferred or refactored. Fixed by using `line` consistently throughout. --- PyInquirer/prompts/checkbox.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/PyInquirer/prompts/checkbox.py b/PyInquirer/prompts/checkbox.py index 92f4783..fbcb3b1 100644 --- a/PyInquirer/prompts/checkbox.py +++ b/PyInquirer/prompts/checkbox.py @@ -82,8 +82,8 @@ def select_item(mouse_event): else: tokens.append(('', ' ', select_item)) # 'o ' - FISHEYE - if choice[2]: # disabled - tokens.append(('', '- %s (%s)' % (choice[0], choice[2]))) + if line[2]: # disabled + tokens.append(('', '- %s (%s)' % (line[0], line[2]))) else: if selected: tokens.append(('class:selected', '{} '.format(self.selected_sign), select_item)) @@ -93,8 +93,8 @@ def select_item(mouse_event): if pointed_at: tokens.append(('[SetCursorPosition]', '')) - if choice[3]: # description - tokens.append(('', "%s - %s" % (line_name, choice[3]))) + if line[3]: # description + tokens.append(('', "%s - %s" % (line_name, line[3]))) else: tokens.append(('', line_name, select_item)) tokens.append(('', '\n')) @@ -160,7 +160,10 @@ def get_prompt_tokens(): ' (, to move, to select, ' 'to toggle, to invert)')) if not ic.answered_correctly: - tokens.append((Token.Error, ' Error: %s' % ic.error_message)) + # Use 'class:error' style consistent with prompt_toolkit style tuples. + # Previously referenced Token.Error from pygments which was never + # imported in this module, causing a NameError on validation failure. + tokens.append(('class:error', ' Error: %s' % ic.error_message)) return tokens # assemble layout