Skip to content

[pylint] Implement consider-using-assignment-expr (R6103)#13196

Closed
vincevannoort wants to merge 17 commits intoastral-sh:mainfrom
vincevannoort:consider-using-assignment-expr
Closed

[pylint] Implement consider-using-assignment-expr (R6103)#13196
vincevannoort wants to merge 17 commits intoastral-sh:mainfrom
vincevannoort:consider-using-assignment-expr

Conversation

@vincevannoort
Copy link
Contributor

@vincevannoort vincevannoort commented Sep 1, 2024

Summary

This pull request implements the R6103 pylint rule: pylint documentation.

It checks assignments which are directly followed by if statements using that expression:

test1 = "example"
if test1:
    print(test1)

And suggest to use the := operator to

if test1 := "example":
    print(test1)

Test Plan

I have added test cases, and checked some of the ruff ecosystem results.

@codspeed-hq
Copy link

codspeed-hq bot commented Sep 1, 2024

CodSpeed Performance Report

Merging #13196 will not alter performance

Comparing vincevannoort:consider-using-assignment-expr (59a04d8) with main (f8093b6)

Summary

✅ 32 untouched benchmarks

@vincevannoort vincevannoort changed the title [WIP] Pylint R6103 [pylint][WIP] Implement R6103 Sep 1, 2024
@vincevannoort vincevannoort changed the title [pylint][WIP] Implement R6103 [WIP] [pylint] Implement R6103 Sep 1, 2024
@vincevannoort vincevannoort force-pushed the consider-using-assignment-expr branch from b422d5c to dbefe63 Compare September 1, 2024 20:12
@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+4612 -0 violations, +0 -0 fixes in 19 projects; 36 projects unchanged)

aiven/aiven-client (+21 -0 violations, +0 -0 fixes)

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

+ aiven/client/argx.py:158:16: PLR6103 Use walrus operator `(arg_list := getattr(func, ARG_LIST_PROP, None))`.
+ aiven/client/argx.py:251:16: PLR6103 Use walrus operator `(cat := tuple(cats[:level + 1]))`.
+ aiven/client/cli.py:1453:12: PLR6103 Use walrus operator `(route := self.args.route)`.
+ aiven/client/cli.py:1470:12: PLR6103 Use walrus operator `(privatelink_connection_id := self.args.privatelink_connection_id)`.
+ aiven/client/cli.py:1828:16: PLR6103 Use walrus operator `(value := arg_vars[key])`.
+ aiven/client/cli.py:1849:12: PLR6103 Use walrus operator `(access_control := self._parse_access_control())`.
+ aiven/client/cli.py:280:12: PLR6103 Use walrus operator `(password := os.environ.get(var))`.
+ aiven/client/cli.py:329:20: PLR6103 Use walrus operator `(arg_list := getattr(prop, argx.ARG_LIST_PROP, None))`.
+ aiven/client/cli.py:3581:20: PLR6103 Use walrus operator `(user_input := input("Re-enter service name {!r} for immediate termination: ".format(name)))`.
+ aiven/client/cli.py:3733:20: PLR6103 Use walrus operator `(user_peer_network_cidrs := peering_connection.get("user_peer_network_cidrs", []))`.
... 11 additional changes omitted for project

PlasmaPy/PlasmaPy (+38 -0 violations, +0 -0 fixes)

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

+ docs/notebooks/langmuir_samples/_generate_noisy.ipynb:cell 6:2:4: PLR6103 Use walrus operator `(save := False)`.
+ noxfile.py:482:18: PLR6103 Use walrus operator `(extraneous_files := source_directory.glob("changelog/*[0-9]*.*.rst?*"))`.
+ src/plasmapy/analysis/nullpoint.py:1453:24: PLR6103 Use walrus operator `(loc := _locate_null_point(vspace, [i, j, k], maxiter, err))`.
+ src/plasmapy/analysis/nullpoint.py:1456:28: PLR6103 Use walrus operator `(p := NullPoint(loc, null_type))`.
+ src/plasmapy/analysis/nullpoint.py:706:40: PLR6103 Use walrus operator `(z_close := np.isclose(root[2], r[2], atol=_EQUALITY_ATOL))`.
+ src/plasmapy/analysis/swept_langmuir/floating_potential.py:280:12: PLR6103 Use walrus operator `(isl_window := np.abs(np.r_[rtn_extras["islands"][-1]][-1] - np.r_[rtn_extras["islands"][0]][0]) + 1)`.
+ src/plasmapy/analysis/swept_langmuir/floating_potential.py:299:12: PLR6103 Use walrus operator `(iadd := istop - istart + 1 - min_points)`.
+ src/plasmapy/diagnostics/charged_particle_radiography/synthetic_radiography.py:1240:8: PLR6103 Use walrus operator `(percentage := np.sum(intensity) / results_dict["num_particles"])`.
+ src/plasmapy/diagnostics/charged_particle_radiography/synthetic_radiography.py:754:12: PLR6103 Use walrus operator `(n_wrong_way := np.sum(np.where(self.theta > np.pi / 2, 1, 0)))`.
+ src/plasmapy/diagnostics/thomson.py:889:16: PLR6103 Use walrus operator `(key := f"{p}_{num!s}")`.
... 28 additional changes omitted for project

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

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

+ airflow/api/client/__init__.py:32:12: PLR6103 Use walrus operator `(session_factory := getattr(backend, "create_client_session", None))`.
+ airflow/api/client/local_client.py:49:12: PLR6103 Use walrus operator `(dag_run := trigger_dag.trigger_dag(dag_id=dag_id, triggered_by=DagRunTriggeredByType.CLI, run_id=run_id, conf=conf, logical_date=logical_date, replace_microseconds=replace_microseconds))`.
+ airflow/api/common/airflow_health.py:43:12: PLR6103 Use walrus operator `(latest_scheduler_job := SchedulerJobRunner.most_recent_job())`.
+ airflow/api/common/delete_dag.py:60:8: PLR6103 Use walrus operator `(running_tis := session.scalar(select(models.TaskInstance.state).where(models.TaskInstance.dag_id == dag_id).where(models.TaskInstance.state == TaskInstanceState.RUNNING).limit(1)))`.
+ airflow/api/common/delete_dag.py:63:8: PLR6103 Use walrus operator `(dag := session.scalar(select(DagModel).where(DagModel.dag_id == dag_id).limit(1)))`.
+ airflow/api/common/mark_tasks.py:81:8: PLR6103 Use walrus operator `(dag := next(iter(task_dags)))`.
+ airflow/api/common/trigger_dag.py:150:8: PLR6103 Use walrus operator `(dag_model := DagModel.get_current(dag_id, session=session))`.
+ airflow/api/common/trigger_dag.py:79:16: PLR6103 Use walrus operator `(min_dag_start_date := dag.default_args["start_date"])`.
+ airflow/api_connexion/endpoints/asset_endpoint.py:197:8: PLR6103 Use walrus operator `(adrq := session.scalar(select(AssetDagRunQueue).join(AssetModel, AssetDagRunQueue.asset_id == AssetModel.id).where(*where_clause)))`.
+ airflow/api_connexion/endpoints/connection_endpoint.py:139:8: PLR6103 Use walrus operator `(connection := session.scalar(select(Connection).filter_by(conn_id=connection_id).limit(1)))`.
+ airflow/api_connexion/endpoints/connection_endpoint.py:69:8: PLR6103 Use walrus operator `(connection := session.scalar(select(Connection).filter_by(conn_id=connection_id)))`.
+ airflow/api_connexion/endpoints/connection_endpoint.py:84:8: PLR6103 Use walrus operator `(connection := session.scalar(select(Connection).where(Connection.conn_id == connection_id)))`.
... 1111 additional changes omitted for project

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

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

+ RELEASING/changelog.py:112:12: PLR6103 Use walrus operator `(github_login := self._github_login_cache.get(author_name))`.
+ RELEASING/changelog.py:132:12: PLR6103 Use walrus operator `(pr_number := git_log.pr_number)`.
+ RELEASING/changelog.py:134:16: PLR6103 Use walrus operator `(detail := self._pr_logs_with_details.get(pr_number))`.
+ RELEASING/changelog.py:141:12: PLR6103 Use walrus operator `(pr_type := re.match(SUPERSET_PULL_REQUEST_TYPES, title))`.
+ RELEASING/changelog.py:163:16: PLR6103 Use walrus operator `(risk_label := re.match(SUPERSET_RISKY_LABELS, label.name))`.
+ RELEASING/changelog.py:284:12: PLR6103 Use walrus operator `(current_head := self._git_get_current_head())`.
+ RELEASING/changelog.py:307:12: PLR6103 Use walrus operator `(match := re.match(r".*\(\#(\d*)\)", split_log_item[4]))`.
+ scripts/benchmark_migration.py:115:20: PLR6103 Use walrus operator `(table := foreign_key.column.table.name)`.
+ scripts/benchmark_migration.py:194:16: PLR6103 Use walrus operator `(missing := min_entities - model_rows[model])`.
+ scripts/benchmark_migration.py:47:8: PLR6103 Use walrus operator `(spec := importlib.util.spec_from_file_location(filepath.stem, filepath))`.
... 299 additional changes omitted for project

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

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

+ samcli/cli/cli_config_file.py:148:20: PLR6103 Use walrus operator `(allow_multiple := options_map[config_name].multiple)`.
+ samcli/cli/cli_config_file.py:319:12: PLR6103 Use walrus operator `(param_value := ctx.params.get(param_name, None))`.
+ samcli/cli/command.py:111:16: PLR6103 Use walrus operator `(row := param.get_help_record(ctx))`.
+ samcli/cli/context.py:145:12: PLR6103 Use walrus operator `(click_core_ctx := click.get_current_context())`.
+ samcli/cli/context.py:197:12: PLR6103 Use walrus operator `(click_core_ctx := click.get_current_context())`.
+ samcli/cli/types.py:322:12: PLR6103 Use walrus operator `(equals_count := tag_value.count("="))`.
+ samcli/cli/types.py:431:12: PLR6103 Use walrus operator `(equals_count := signing_profile.count(":"))`.
+ samcli/commands/_utils/click_mutex.py:76:20: PLR6103 Use walrus operator `(has_all_required_params := False not in [required_param in opts for required_param in required_params])`.
+ samcli/commands/_utils/command_exception_handler.py:75:20: PLR6103 Use walrus operator `(exception_handler := (additional_mapping or {}).get(exception_type))`.
+ samcli/commands/_utils/command_exception_handler.py:85:24: PLR6103 Use walrus operator `(handler := exception_handler.get_handler(exception_type))`.
... 193 additional changes omitted for project

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

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

+ examples/basic/data/ajax_source.py:41:12: PLR6103 Use walrus operator `(requested_headers := request.headers.get('Access-Control-Request-Headers'))`.
+ examples/basic/data/server_sent_events_source.py:43:12: PLR6103 Use walrus operator `(requested_headers := request.headers.get('Access-Control-Request-Headers'))`.
+ examples/basic/scatters/markertypes.py:35:12: PLR6103 Use walrus operator `(name := f"{base}_{kind}" if kind else base)`.
+ examples/output/jupyter/push_notebook/Numba Image Example.ipynb:cell 13:12:8: PLR6103 Use walrus operator `(ksum := np.sum(kernel))`.
+ examples/server/app/gapminder/main.py:60:8: PLR6103 Use walrus operator `(year := slider.value + 1)`.
+ scripts/milestone.py:142:8: PLR6103 Use walrus operator `(num_types := sum(1 for label in labels if label.startswith("type:")))`.
+ setup.py:84:8: PLR6103 Use walrus operator `(missing := [fn for fn in JS_FILES if not (BUILD_JS / fn).exists()])`.
+ src/bokeh/application/handlers/code.py:148:12: PLR6103 Use walrus operator `(module := self._runner.new_module())`.
+ src/bokeh/client/connection.py:354:20: PLR6103 Use walrus operator `(message := await self._receiver.consume(fragment))`.
+ src/bokeh/client/connection.py:388:12: PLR6103 Use walrus operator `(reply := self._send_message_wait_for_reply(msg))`.
... 125 additional changes omitted for project

freedomofpress/securedrop (+53 -0 violations, +0 -0 fixes)

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

+ admin/bootstrap.py:63:8: PLR6103 Use walrus operator `(return_code := popen.wait())`.
+ admin/securedrop_admin/__init__.py:1231:12: PLR6103 Use walrus operator `(return_code := args.func(args))`.
+ admin/securedrop_admin/__init__.py:139:16: PLR6103 Use walrus operator `(text := document.text)`.
+ admin/securedrop_admin/__init__.py:196:16: PLR6103 Use walrus operator `(text := document.text.lower())`.
+ admin/securedrop_admin/__init__.py:203:16: PLR6103 Use walrus operator `(text := document.text.replace(" ", ""))`.
+ admin/securedrop_admin/__init__.py:253:16: PLR6103 Use walrus operator `(text := document.text)`.
+ admin/securedrop_admin/__init__.py:267:16: PLR6103 Use walrus operator `(text := document.text)`.
+ admin/securedrop_admin/__init__.py:277:16: PLR6103 Use walrus operator `(text := document.text)`.
+ install_files/ansible-base/roles/restore/files/compare_torrc.py:24:16: PLR6103 Use walrus operator `(m := service_re.match(line))`.
+ molecule/testinfra/common/test_automatic_updates.py:70:8: PLR6103 Use walrus operator `(distro := host.system_info.codename)`.
... 43 additional changes omitted for project

fronzbot/blinkpy (+20 -0 violations, +0 -0 fixes)

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

+ blinkpy/blinkpy.py:188:20: PLR6103 Use walrus operator `(network_id := str(owl["network_id"]))`.
+ blinkpy/blinkpy.py:212:20: PLR6103 Use walrus operator `(network_id := str(lotus["network_id"]))`.
+ blinkpy/blinkpy.py:242:20: PLR6103 Use walrus operator `(camera_network := str(network["network_id"]))`.
+ blinkpy/blinkpy.py:306:12: PLR6103 Use walrus operator `(last_refresh := self.last_refresh)`.
+ blinkpy/camera.py:140:12: PLR6103 Use walrus operator `(res := await api.request_get_config(self.sync.blink, self.network_id, self.camera_id, product_type=self.product_type))`.
+ blinkpy/camera.py:169:12: PLR6103 Use walrus operator `(res := await api.request_update_config(self.sync.blink, self.network_id, self.camera_id, product_type=self.product_type, data=data))`.
+ blinkpy/camera.py:221:12: PLR6103 Use walrus operator `(response := await self.get_media())`.
+ blinkpy/camera.py:342:28: PLR6103 Use walrus operator `(recent := {"time": self.last_record, "clip": self.clip})`.
+ blinkpy/camera.py:374:16: PLR6103 Use walrus operator `(response := await self.get_media())`.
+ blinkpy/camera.py:379:16: PLR6103 Use walrus operator `(response := await self.get_media(media_type="video"))`.
... 10 additional changes omitted for project

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

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLR6103 4612 4612 0 0 0

@vincevannoort vincevannoort force-pushed the consider-using-assignment-expr branch from 84c2180 to 4d891db Compare September 6, 2024 06:22
Comment on lines +1134 to +1086
if checker.enabled(Rule::UnnecessaryAssignment) {
pylint::rules::unnecessary_assignment(checker, if_);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I applied this rule by checking whether the previous statement from a IfStmt is an AssignStmt, however I am wondering if it is more desirable to do check if the next statement of a AssignStmt is an IfStmt?

Copy link
Member

Choose a reason for hiding this comment

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

What benefits do you see in testing the next statement after an AssignStmt.

My intuition here is that there are probably more assignment than if statements. Therefore, running the rules on if nodes might overall be faster?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that assignments are far more common than if statements.

I think the answer depends on the costs for checking previous_statement and next_statement.

My thought here is that retrieving the previous statement using the newly added previous_statement might be more expensive because it has to iterate over all previous statements using previous_statements to find the previous statement (if there is a better way, please let me know).

While checking an assignment, then checking the next_statement might be a cheap operation.

Do you have any idea? If they have equal cost I think the current implementation is fine. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can change it, to the suggested approach if you want 😄

@vincevannoort vincevannoort changed the title [WIP] [pylint] Implement R6103 [WIP] [pylint] Implement consider-using-assignment-expr (R6103) Sep 7, 2024
@vincevannoort vincevannoort changed the title [WIP] [pylint] Implement consider-using-assignment-expr (R6103) [pylint] Implement consider-using-assignment-expr (R6103) Sep 7, 2024
Comment on lines 1250 to 1439
pub fn previous_statement(&self, stmt: &'a Stmt) -> Option<&Stmt> {
self.previous_statements(stmt)?.next()
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed something like this function, not sure if there is an existing better way?

Copy link
Member

Choose a reason for hiding this comment

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

That makes sense. I think we can make it more efficient with #13895
which reduces an extra find and collect

Copy link
Contributor Author

@vincevannoort vincevannoort Feb 13, 2025

Choose a reason for hiding this comment

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

Nice, I did that just now in 9e176d1

@vincevannoort vincevannoort marked this pull request as ready for review September 7, 2024 12:12
@vincevannoort vincevannoort force-pushed the consider-using-assignment-expr branch from 07d2997 to 964c3d6 Compare September 7, 2024 12:33
@MichaReiser MichaReiser added rule Implementing or modifying a lint rule preview Related to preview mode features labels Sep 8, 2024
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 this contribution. My plane is about to land. I've to finish the review at a later time.

I only had enough time to quickly glance over the Rust code. We should look into removing the many node.clone() calls because it is fairly expensive to clone nodes and probably unnecessary. Let me know if you need some guidance on how to remove the clone calls (It probably requires adding some lifetimes)

Regarding the rule's naming.

  • I did a quick search to see how we referred to := in other rules. There are not many usages but named expression (walrus operator) is the most common form.
  • The rule name seems too generic to me and its name is very similar to unnecessary-assign (which we should rename to `unnecessary-assignment). Reading through the examples the rule mainly is about assigning a value that is then only used in an if condition. I need to think a bit more about what a good rule name could be. Maybe you have an idea?

Comment on lines +19 to +29
bad5 = (
'example',
'example',
'example',
'example',
'example',
'example',
'example',
'example',
'example',
'example',
)
Copy link
Member

Choose a reason for hiding this comment

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

I like the example because it shows a potentially controversial use case. I would probably prefer the assignment to keep the if smaller.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, should we check the complexity of the assignment to determine whether to apply the rule? If so, how do we determine where to draw the line?

Comment on lines +1134 to +1086
if checker.enabled(Rule::UnnecessaryAssignment) {
pylint::rules::unnecessary_assignment(checker, if_);
}
Copy link
Member

Choose a reason for hiding this comment

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

What benefits do you see in testing the next statement after an AssignStmt.

My intuition here is that there are probably more assignment than if statements. Therefore, running the rules on if nodes might overall be faster?

@vincevannoort
Copy link
Contributor Author

Thanks for this contribution. My plane is about to land. I've to finish the review at a later time.

Thanks for the review, much appreciated! 👍

I only had enough time to quickly glance over the Rust code. We should look into removing the many node.clone() calls because it is fairly expensive to clone nodes and probably unnecessary. Let me know if you need some guidance on how to remove the clone calls (It probably requires adding some lifetimes)

I have tried and was able to remove almost all clone calls, except a few which I think are needed for returning the diagnostic. Could you take a look at the remaining ones and see if any of the 3 can still be removed?

Regarding the rule's naming.

  • I did a quick search to see how we referred to := in other rules. There are not many usages but named expression (walrus operator) is the most common form.
  • The rule name seems too generic to me and its name is very similar to unnecessary-assign (which we should rename to `unnecessary-assignment). Reading through the examples the rule mainly is about assigning a value that is then only used in an if condition. I need to think a bit more about what a good rule name could be. Maybe you have an idea?

I agree, here are some possible options:

  1. unnecessary_assignment_before_if_stmt
  2. redundant_assignment_before_if _stmt
  3. standalone_assignment_before_if_stmt

These seem close to what the lint is trying to prevent, do you have other ideas in mind?

@vincevannoort
Copy link
Contributor Author

Hey @MichaReiser, once you have the time, would you mind giving this pull request another look? 😄

@MichaReiser MichaReiser self-assigned this Sep 20, 2024
@vincevannoort
Copy link
Contributor Author

Hey @MichaReiser, could you or someone else from the team have a look? 😄

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 the ping and sorry for the late review. I pushed a few smaller refactors to avoid unnecessary collects.

I created a PR that should allow us to implement a more efficient previous_statement here.

I think the rule has to become cleverer, at least when we want to support handling elif cases because today's implementation can result in changes that fail at runtime.

};
}

// case - elif else clauses (`elif test1:`)
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why compare expressions aren't handled inside elif_else clauses?

Comment on lines 111 to 119
errors.extend(
stmt.elif_else_clauses
.iter()
.filter(|elif_else_clause| elif_else_clause.test.is_some())
.filter_map(|elif_else_clause| {
let elif_check = elif_else_clause.test.as_ref().unwrap();
find_assignment_before_if_stmt(semantic, elif_check, elif_check)
})
.collect::<Vec<AssignmentBeforeIfStmt>>(),
Copy link
Member

Choose a reason for hiding this comment

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

Changing assignments to walrus operators in elif else branches is semantically incorrect if the variable is used afterwards

>>> if True: ...
... elif x :=10: ...
... 
Ellipsis
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think you are right. Due to that I decided to only apply the rule to simple if statements, where simple means without elif or else attached to the if. That should prevent cases like this.

error: AssignmentBeforeIfStmt,
) -> Diagnostic {
let (origin, expr_name, assignment) = error;
let assignment_expr = generator.expr(&assignment.value);
Copy link
Member

Choose a reason for hiding this comment

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

What's the motivation for calling into the generator here? We try to avoid using the generator because it removes comments. Could we instead take the assignment value as it is in the source (using locator)? Note: We have to be careful about parenthesized expressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you help me out here? I am not that familiar in how to do this properly.

Comment on lines 1250 to 1439
pub fn previous_statement(&self, stmt: &'a Stmt) -> Option<&Stmt> {
self.previous_statements(stmt)?.next()
}
Copy link
Member

Choose a reason for hiding this comment

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

That makes sense. I think we can make it more efficient with #13895
which reduces an extra find and collect

Comment on lines 38 to 42
bad7 = 'example'
if bad7 == 'something': # [consider-using-assignment-expr]
pass
elif bad7 == 'something else':
pass
Copy link
Member

Choose a reason for hiding this comment

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

This is an interesting example and possibly controversial. I would prefer the existing solution because the assignment in the if is very subtle and I can see how it can be confusing when trying to figure out what the value of bad7 is in the elif branch

Copy link
Contributor Author

@vincevannoort vincevannoort Feb 13, 2025

Choose a reason for hiding this comment

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

Yeah, I think you are right. Due to that I decided to only apply the rule to simple if statements, where simple means without elif or else attached to the if.

@vincevannoort
Copy link
Contributor Author

Thanks for the review @MichaReiser 👍 , just a heads up: I will be travelling without my laptop for the coming 5 weeks, so will only get back to pull request this after.

@nickdrozd
Copy link
Contributor

Hello, is this still being worked on? It is nice check, would be great to have. Especially if possible autofix would amazing.

@vincevannoort vincevannoort force-pushed the consider-using-assignment-expr branch from 04f310d to d11a686 Compare February 13, 2025 06:41
@vincevannoort
Copy link
Contributor Author

vincevannoort commented Feb 13, 2025

@MichaReiser sorry for getting back so late to this PR, and thanks for all the previous input.

I have rebased to the latest changes on main, and fixed some issues mentioned in earlier comments. Additionally, I changed the rule to only apply to simple if statements, that do not have an elif or else attached. This should hopefully resolve most of the more controversial cases.

Coul you take another look?

Comment on lines +97 to +116
match &*stmt.test {
// case - bool operations (`if test1 and test2:`)
Expr::BoolOp(expr) => diagnostics.extend(expr.values.iter().filter_map(|bool_test| {
Some(create_diagnostic(
checker,
find_assignment_before_if_stmt(&previous_assignment, if_test, bool_test)?,
))
})),

// case - compare (`if test1 is not None:`)
Expr::Compare(compare) => {
if let Some(error) =
find_assignment_before_if_stmt(&previous_assignment, if_test, &compare.left)
{
diagnostics.push(create_diagnostic(checker, error));
};
}

_ => {}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am curious if we should actually do this part. If I look at the ecosystem checks, these also produce some possible controversial results. 🤔

Without it, I think most of the more controversial results would disappear.

@MichaReiser
Copy link
Member

Hmm, I just noticed that this is a pylint extensions rule which we don't want to add right now because it would require a new rule group.

@vincevannoort
Copy link
Contributor Author

Hmm, I just noticed that this is a pylint extensions rule which we don't want to add right now because it would require a new rule group.

What would you recommend in this case? Just close the pull request or make it a new ruff rule instead of pylint rule?

@MichaReiser
Copy link
Member

MichaReiser commented Feb 20, 2025

Yeah, I think we have to close this for now because there's no clear path to landing this PR before #1774 is complete. I'm very sorry for wasting your time here. I should have noticed this way sooner. But thank you anyway and we can come back to this PR once we're at a point where we accept pylint extension rules.

@vincevannoort
Copy link
Contributor Author

Yeah, I think we have to close this for now because there's no clear path to landing this PR before #1774 is complete. I'm very sorry for wasting your time here. I should have noticed this way sooner. But thank you anyway and we can come back to this PR once we're at a point where we accept pylint extension rules.

No worries 😄 , I still learned quite a lot from the implementation so it was still worth it for me personally. Thanks for the effort in the feedback rounds. 👍

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.

4 participants