Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new rule RUF059: Unused unpacked assignment #16449

Merged
merged 4 commits into from
Mar 3, 2025

Conversation

JelleZijlstra
Copy link
Contributor

Split from F841 following discussion in #8884.

Fixes #8884.

Summary

Add a new rule for unused assignments in tuples. Remove similar behavior from F841.

Test Plan

Adapt F841 tests and move them over to the new rule.

@@ -48,6 +48,7 @@ pub(crate) fn deferred_scopes(checker: &Checker) {
Rule::UnusedPrivateTypedDict,
Rule::UnusedPrivateTypeVar,
Rule::UnusedStaticMethodArgument,
Rule::UnusedUnpackedVariable,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

feedback/rant: took me forever to figure out that missing this line was why my tests weren't producing any lints. It makes sense in retrospect and I don't have a better idea for how to do it, but it was a bit frustrating.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for that. Yeah, not sure what a good short-term fix for this is. My plan for Red Knot is to remove those manual checks entirely and instead have a more "formal" rule API that allows pre-computing in which phase a rule must run. This will hopefully help prevent such surprises

Copy link
Contributor

github-actions bot commented Mar 1, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+833 -1093 violations, +0 -0 fixes in 37 projects; 18 projects unchanged)

RasaHQ/rasa (+39 -39 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

- rasa/telemetry.py:594:30: F841 Local variable `tb` is assigned to but never used
+ rasa/telemetry.py:594:30: RUF059 Unpacked variable `tb` is never used
- rasa/telemetry.py:594:9: F841 Local variable `exc_type` is assigned to but never used
+ rasa/telemetry.py:594:9: RUF059 Unpacked variable `exc_type` is never used
- tests/cli/test_rasa_export.py:278:5: F841 Local variable `events` is assigned to but never used
+ tests/cli/test_rasa_export.py:278:5: RUF059 Unpacked variable `events` is never used
- tests/core/evaluation/test_marker.py:287:13: F841 Local variable `expected` is assigned to but never used
+ tests/core/evaluation/test_marker.py:287:13: RUF059 Unpacked variable `expected` is never used
- tests/core/evaluation/test_marker.py:307:13: F841 Local variable `expected` is assigned to but never used
+ tests/core/evaluation/test_marker.py:307:13: RUF059 Unpacked variable `expected` is never used
... 68 additional changes omitted for project

alteryx/featuretools (+0 -20 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

- featuretools/computational_backends/feature_set.py:113:9: F841 Local variable `node_needs_full_dataframe` is assigned to but never used
- featuretools/primitives/utils.py:122:9: F841 Local variable `trans_valid_inputs` is assigned to but never used
- featuretools/primitives/utils.py:123:9: F841 Local variable `trans_return_type` is assigned to but never used
- featuretools/primitives/utils.py:129:9: F841 Local variable `agg_valid_inputs` is assigned to but never used
- featuretools/primitives/utils.py:130:9: F841 Local variable `agg_return_type` is assigned to but never used
- featuretools/tests/computational_backend/test_calculate_feature_matrix.py:1562:9: F841 Local variable `client` is assigned to but never used
- featuretools/tests/computational_backend/test_calculate_feature_matrix.py:1601:9: F841 Local variable `client` is assigned to but never used
- featuretools/tests/primitive_tests/test_agg_feats.py:596:9: F841 Local variable `features` is assigned to but never used
- featuretools/tests/primitive_tests/test_agg_feats.py:668:9: F841 Local variable `features` is assigned to but never used
- featuretools/tests/primitive_tests/test_agg_feats.py:793:9: F841 Local variable `features` is assigned to but never used
... 10 additional changes omitted for project

PlasmaPy/PlasmaPy (+1 -81 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

- docs/notebooks/diagnostics/charged_particle_radiography_particle_tracing_wire_mesh.ipynb:cell 16:11:5: F841 Local variable `fig` is assigned to but never used
- src/plasmapy/analysis/fit_functions.py:887:22: F841 Local variable `b` is assigned to but never used
- src/plasmapy/analysis/nullpoint.py:1384:17: F841 Local variable `eigen_vectors` is assigned to but never used
- src/plasmapy/analysis/nullpoint.py:1384:5: F841 Local variable `eigen_vals` is assigned to but never used
- src/plasmapy/analysis/nullpoint.py:461:5: F841 Local variable `ax` is assigned to but never used
- src/plasmapy/analysis/nullpoint.py:462:5: F841 Local variable `ay` is assigned to but never used
... 75 additional changes omitted for rule F841
+ tests/diagnostics/test_thomson.py:745:5: PLR0914 Too many local variables (26/15)
- tests/diagnostics/test_thomson.py:745:5: PLR0914 Too many local variables (26/15)
... 74 additional changes omitted for project

apache/airflow (+86 -86 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

- airflow/configuration.py:1576:22: F841 Local variable `source` is assigned to but never used
+ airflow/configuration.py:1576:22: RUF059 Unpacked variable `source` is never used
- airflow/configuration.py:623:25: F841 Local variable `should_continue` is assigned to but never used
+ airflow/configuration.py:623:25: RUF059 Unpacked variable `should_continue` is never used
- airflow/executors/local_executor.py:215:25: F841 Local variable `exc` is assigned to but never used
+ airflow/executors/local_executor.py:215:25: RUF059 Unpacked variable `exc` is never used
- airflow/jobs/scheduler_job_runner.py:909:9: F841 Local variable `executor_class` is assigned to but never used
+ airflow/jobs/scheduler_job_runner.py:909:9: RUF059 Unpacked variable `executor_class` is never used
- airflow/providers_manager.py:934:13: F841 Local variable `module` is assigned to but never used
+ airflow/providers_manager.py:934:13: RUF059 Unpacked variable `module` is never used
... 162 additional changes omitted for project

apache/superset (+11 -11 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

- scripts/check-env.py:67:24: F841 Local variable `supported_max` is assigned to but never used
+ scripts/check-env.py:67:24: RUF059 Unpacked variable `supported_max` is never used
- superset/commands/report/alert.py:172:13: F841 Local variable `executor` is assigned to but never used
+ superset/commands/report/alert.py:172:13: RUF059 Unpacked variable `executor` is never used
- tests/integration_tests/core_tests.py:299:21: F841 Local variable `tests` is assigned to but never used
+ tests/integration_tests/core_tests.py:299:21: RUF059 Unpacked variable `tests` is never used
- tests/integration_tests/dict_import_export_tests.py:168:9: F841 Local variable `table` is assigned to but never used
+ tests/integration_tests/dict_import_export_tests.py:168:9: RUF059 Unpacked variable `table` is never used
- tests/integration_tests/dict_import_export_tests.py:173:9: F841 Local variable `table_over` is assigned to but never used
+ tests/integration_tests/dict_import_export_tests.py:173:9: RUF059 Unpacked variable `table_over` is never used
... 12 additional changes omitted for project

aws/aws-sam-cli (+0 -46 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

- tests/integration/init/test_init_command.py:1005:13: F841 Local variable `stdout_data` is assigned to but never used
- tests/integration/init/test_init_command.py:1045:17: F841 Local variable `stdout_data` is assigned to but never used
- tests/integration/init/test_init_command.py:1080:17: F841 Local variable `stdout_data` is assigned to but never used
- tests/integration/init/test_init_command.py:118:17: F841 Local variable `stdout_data` is assigned to but never used
- tests/integration/init/test_init_command.py:150:17: F841 Local variable `stdout_data` is assigned to but never used
- tests/integration/init/test_init_command.py:182:17: F841 Local variable `stdout_data` is assigned to but never used
- tests/integration/init/test_init_command.py:214:17: F841 Local variable `stdout_data` is assigned to but never used
- tests/integration/init/test_init_command.py:248:17: F841 Local variable `stdout_data` is assigned to but never used
- tests/integration/init/test_init_command.py:282:17: F841 Local variable `stdout_data` is assigned to but never used
- tests/integration/init/test_init_command.py:316:17: F841 Local variable `stdout_data` is assigned to but never used
... 36 additional changes omitted for project

binary-husky/gpt_academic (+52 -52 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

- crazy_functions/Conversation_To_File.py:78:9: F841 Local variable `html` is assigned to but never used
+ crazy_functions/Conversation_To_File.py:78:9: RUF059 Unpacked variable `html` is never used
- crazy_functions/Latex_Function.py:382:18: F841 Local variable `downloaded` is assigned to but never used
+ crazy_functions/Latex_Function.py:382:18: RUF059 Unpacked variable `downloaded` is never used
- crazy_functions/Social_Helper.py:70:17: F841 Local variable `err_msg` is assigned to but never used
+ crazy_functions/Social_Helper.py:70:17: RUF059 Unpacked variable `err_msg` is never used
- crazy_functions/agent_fns/python_comment_agent.py:216:9: F841 Local variable `begin` is assigned to but never used
+ crazy_functions/agent_fns/python_comment_agent.py:216:9: RUF059 Unpacked variable `begin` is never used
- crazy_functions/crazy_utils.py:122:21: F841 Local variable `p_ratio` is assigned to but never used
+ crazy_functions/crazy_utils.py:122:21: RUF059 Unpacked variable `p_ratio` is never used
... 94 additional changes omitted for project

bokeh/bokeh (+25 -25 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

- src/bokeh/sphinxext/bokeh_model.py:121:34: F841 Local variable `arglist` is assigned to but never used
+ src/bokeh/sphinxext/bokeh_model.py:121:34: RUF059 Unpacked variable `arglist` is never used
- src/bokeh/sphinxext/bokeh_model.py:121:43: F841 Local variable `retann` is assigned to but never used
+ src/bokeh/sphinxext/bokeh_model.py:121:43: RUF059 Unpacked variable `retann` is never used
- src/bokeh/sphinxext/bokeh_model.py:121:9: F841 Local variable `name_prefix` is assigned to but never used
+ src/bokeh/sphinxext/bokeh_model.py:121:9: RUF059 Unpacked variable `name_prefix` is never used
- src/bokeh/sphinxext/bokeh_options.py:108:36: F841 Local variable `arglist` is assigned to but never used
+ src/bokeh/sphinxext/bokeh_options.py:108:36: RUF059 Unpacked variable `arglist` is never used
- src/bokeh/sphinxext/bokeh_options.py:108:45: F841 Local variable `retann` is assigned to but never used
+ src/bokeh/sphinxext/bokeh_options.py:108:45: RUF059 Unpacked variable `retann` is never used
... 40 additional changes omitted for project

docker/docker-py (+0 -31 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

- docker/api/image.py:401:19: F841 Local variable `repo_name` is assigned to but never used
- docker/api/image.py:476:19: F841 Local variable `repo_name` is assigned to but never used
- docker/api/plugin.py:132:19: F841 Local variable `repo_name` is assigned to but never used
- docker/api/plugin.py:173:19: F841 Local variable `repo_name` is assigned to but never used
- docker/api/plugin.py:199:19: F841 Local variable `repo_name` is assigned to but never used
- docker/api/plugin.py:252:19: F841 Local variable `repo_name` is assigned to but never used
- docker/api/service.py:166:19: F841 Local variable `repo_name` is assigned to but never used
- docker/api/service.py:447:23: F841 Local variable `repo_name` is assigned to but never used
- docker/transport/npipesocket.py:121:9: F841 Local variable `err` is assigned to but never used
- docker/transport/npipesocket.py:143:13: F841 Local variable `err` is assigned to but never used
... 21 additional changes omitted for project

... Truncated remaining completed project reports due to GitHub comment length restrictions

Changes by rule (4 rules affected)

code total + violation - violation + fix - fix
F841 1091 0 1091 0 0
RUF059 831 831 0 0 0
PLR0914 2 1 1 0 0
DOC201 2 1 1 0 0

@MichaReiser MichaReiser added preview Related to preview mode features rule Implementing or modifying a lint rule labels Mar 3, 2025
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Thanks for pushing this over the line. I'll make a few small nit improvements but this looks good.

Comment on lines 19 to 21
/// Under [preview mode](https://docs.astral.sh/ruff/preview), this rule also
/// triggers on unused unpacked assignments (for example, `x, y = foo()`).
///
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Under [preview mode](https://docs.astral.sh/ruff/preview), this rule also
/// triggers on unused unpacked assignments (for example, `x, y = foo()`).
///

@@ -48,6 +48,7 @@ pub(crate) fn deferred_scopes(checker: &Checker) {
Rule::UnusedPrivateTypedDict,
Rule::UnusedPrivateTypeVar,
Rule::UnusedStaticMethodArgument,
Rule::UnusedUnpackedVariable,
Copy link
Member

Choose a reason for hiding this comment

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

Sorry for that. Yeah, not sure what a good short-term fix for this is. My plan for Red Knot is to remove those manual checks entirely and instead have a more "formal" rule API that allows pre-computing in which phase a rule must run. This will hopefully help prevent such surprises

Comment on lines 86 to 100
for (name, binding) in scope
.bindings()
.map(|(name, binding_id)| (name, checker.semantic().binding(binding_id)))
.filter_map(|(name, binding)| {
if checker.settings.preview.is_enabled()
&& binding.is_unpacked_assignment()
&& binding.is_unused()
&& !binding.is_nonlocal()
&& !binding.is_global()
&& !checker.settings.dummy_variable_rgx.is_match(name)
{
return Some((name, binding));
}

None
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit unfortunate that this repeats the entire check from F841 with only the unpack assignment being different. Let me see if we can make this more obvious

@MichaReiser MichaReiser merged commit c80678a into astral-sh:main Mar 3, 2025
21 checks passed
@JelleZijlstra
Copy link
Contributor Author

Thanks for the review and merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

F841 preview changes to tuple unpacking
2 participants