You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ruff's B023 rule incorrectly flags loop variables in cases where closures intentionally and correctly capture loop variables. This is a false positive as the loop variable binding works as expected in Python's scoping rules.
Minimal Reproduction
# Example 1: Dynamic callback creation (common in UI frameworks, event handlers, etc.)defcreate_callbacks(items: dict) ->list:
callbacks= []
forname, datainitems.items():
defcallback(): # Ruff[B023] incorrectly flags 'name' hereprint(f"Processing {name}: {data}") # This is the flagged linecallbacks.append(callback)
returncallbacks# Example 2: Real-world usage with Click (common CLI pattern)defcreate_cli(command_map: dict) ->click.Group:
cli_group=click.Group()
forname, commandincommand_map.items():
defcommand_fn(**kwargs): # Ruff[B023] incorrectly flags 'name' herecontext=CommandContext(
command=name, # This is the flagged linequery=kwargs.get("query"),
options=parse_options(kwargs),
)
exit_code=asyncio.run(run_command(command_map, context))
ifexit_code!=0:
click.get_current_context().exit(exit_code)
exceptExceptionase:
click.echo(f"Error: {e!s}", err=True)
click.get_current_context().exit(1)
cmd=click.Command(
name=name,
callback=command_fn,
help=getattr(command, "help", None)
)
cli_group.add_command(cmd)
returncli_group
Command Invoked
ruff check . --isolated
B023 Function definition does not bind loop variable `name`|
139 | try:
140 | context = CommandContext(
141 | command=name,
| ^^^^ B023
142 | query=kwargs.get("query"),
143 | options=parse_options(kwargs),
|
Found 1 error.
This pattern is common in many Python applications where dynamic function creation is needed:
Event handlers and callbacks
CLI command creation
UI framework event bindings
Plugin systems
Decorator factories
The loop variable is correctly captured in the closure according to Python's scoping rules
The code works correctly at runtime - each function correctly captures and uses its corresponding loop variable
This is a legitimate and common Python pattern, not a bug or anti-pattern
Similar to the confirmed false positive with : #7847#15716
Expected Behavior
Ruff should not flag B023 for loop variables that are intentionally captured by closures, as this is a valid and common Python pattern that works correctly according to Python's scoping rules.
The text was updated successfully, but these errors were encountered:
Description
Keywords Searched
Bug Description
Ruff's B023 rule incorrectly flags loop variables in cases where closures intentionally and correctly capture loop variables. This is a false positive as the loop variable binding works as expected in Python's scoping rules.
Minimal Reproduction
Command Invoked
ruff check . --isolated
Ruff Settings (pyproject.toml)
Ruff Version
Note
This pattern is common in many Python applications where dynamic function creation is needed:
The loop variable is correctly captured in the closure according to Python's scoping rules
The code works correctly at runtime - each function correctly captures and uses its corresponding loop variable
This is a legitimate and common Python pattern, not a bug or anti-pattern
Similar to the confirmed false positive with :
#7847 #15716
Expected Behavior
Ruff should not flag B023 for loop variables that are intentionally captured by closures, as this is a valid and common Python pattern that works correctly according to Python's scoping rules.
The text was updated successfully, but these errors were encountered: