Skip to content

[pylint] Improve repeated-equality-comparison fix to use a set when all elements are hashable (PLR1714) #16685

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

Merged
merged 2 commits into from
Mar 13, 2025

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Mar 12, 2025

Summary

This PR promotes the fix improvements for PLR1714 that were introduced in #14372 to stable.

The improvement is that the fix now proposes to use a set if all elements are hashable:

foo == "bar" or foo == "baz" or foo == "qux"

Gets fixed to

foo in {"bar", "baz", "qux"}

where it previously always got fixed to a tuple.

The new fix was first released in ruff 0.8.0 (Nov last year). This is not a breaking change. The change was preview gated only to get some extra test coverage.

There are no open issues or PRs related to this changed fix behavior.

@MichaReiser MichaReiser added the fixes Related to suggested fixes for violations label Mar 12, 2025
@MichaReiser MichaReiser added this to the v0.10 milestone Mar 12, 2025
@MichaReiser MichaReiser requested a review from ntBre March 12, 2025 16:37
Copy link

codspeed-hq bot commented Mar 12, 2025

CodSpeed Performance Report

Merging #16685 will degrade performances by 4.61%

Comparing micha/repeated-equality-fix (b13b4d6) with micha/ruff-0.10 (5a40aee)

Summary

❌ 1 regressions
✅ 31 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
red_knot_check_file[incremental] 5.2 ms 5.5 ms -4.61%

@MichaReiser MichaReiser force-pushed the micha/repeated-equality-fix branch from 31df500 to c9457ea Compare March 12, 2025 16:47
@@ -28,7 +28,7 @@ use crate::Locator;
/// If the items are hashable, use a `set` for efficiency; otherwise, use a
/// `tuple`.
///
/// In [preview], this rule will try to determine if the values are hashable
/// This rule will try to determine if the values are hashable
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the part below this "and continue to suggest the use of a set" was also part of the preview behavior.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch, yeah. I should have removed the continue

Copy link
Contributor

github-actions bot commented Mar 12, 2025

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+31 -31 violations, +0 -0 fixes in 5 projects; 50 projects unchanged)

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

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

- airflow/api_fastapi/logging/decorators.py:66:27: PLR1714 Consider merging multiple comparisons: `k in ("val", "value")`. Use a `set` if the elements are hashable.
+ airflow/api_fastapi/logging/decorators.py:66:27: PLR1714 Consider merging multiple comparisons: `k in {"val", "value"}`.
- dev/breeze/src/airflow_breeze/commands/release_management_commands.py:3070:8: PLR1714 Consider merging multiple comparisons: `package_format in ("sdist", "both")`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:3070:8: PLR1714 Consider merging multiple comparisons: `package_format in {"sdist", "both"}`.
- dev/breeze/src/airflow_breeze/commands/release_management_commands.py:3072:8: PLR1714 Consider merging multiple comparisons: `package_format in ("wheel", "both")`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:3072:8: PLR1714 Consider merging multiple comparisons: `package_format in {"wheel", "both"}`.
- dev/breeze/src/airflow_breeze/commands/release_management_commands.py:3088:8: PLR1714 Consider merging multiple comparisons: `package_format in ("sdist", "both")`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:3088:8: PLR1714 Consider merging multiple comparisons: `package_format in {"sdist", "both"}`.
- dev/breeze/src/airflow_breeze/commands/release_management_commands.py:3090:8: PLR1714 Consider merging multiple comparisons: `package_format in ("wheel", "both")`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:3090:8: PLR1714 Consider merging multiple comparisons: `package_format in {"wheel", "both"}`.
- dev/breeze/src/airflow_breeze/commands/release_management_commands.py:659:12: PLR1714 Consider merging multiple comparisons: `package_format in ("sdist", "both")`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:659:12: PLR1714 Consider merging multiple comparisons: `package_format in {"sdist", "both"}`.
- dev/breeze/src/airflow_breeze/commands/release_management_commands.py:661:12: PLR1714 Consider merging multiple comparisons: `package_format in ("wheel", "both")`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:661:12: PLR1714 Consider merging multiple comparisons: `package_format in {"wheel", "both"}`.
- dev/breeze/src/airflow_breeze/commands/release_management_commands.py:676:12: PLR1714 Consider merging multiple comparisons: `package_format in ("sdist", "both")`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:676:12: PLR1714 Consider merging multiple comparisons: `package_format in {"sdist", "both"}`.
- dev/breeze/src/airflow_breeze/commands/release_management_commands.py:678:12: PLR1714 Consider merging multiple comparisons: `package_format in ("wheel", "both")`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:678:12: PLR1714 Consider merging multiple comparisons: `package_format in {"wheel", "both"}`.
- dev/breeze/src/airflow_breeze/utils/host_info_utils.py:39:8: PLR1714 Consider merging multiple comparisons: `os in ("linux", "darwin")`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/utils/host_info_utils.py:39:8: PLR1714 Consider merging multiple comparisons: `os in {"linux", "darwin"}`.
- dev/breeze/src/airflow_breeze/utils/host_info_utils.py:49:8: PLR1714 Consider merging multiple comparisons: `os in ("linux", "darwin")`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/utils/host_info_utils.py:49:8: PLR1714 Consider merging multiple comparisons: `os in {"linux", "darwin"}`.
- dev/breeze/src/airflow_breeze/utils/packages.py:737:12: PLR1714 Consider merging multiple comparisons: `ex.returncode in (128, 2)`. Use a `set` if the elements are hashable.
+ dev/breeze/src/airflow_breeze/utils/packages.py:737:12: PLR1714 Consider merging multiple comparisons: `ex.returncode in {128, 2}`.
+ dev/breeze/src/airflow_breeze/utils/selective_checks.py:994:16: PLR1714 Consider merging multiple comparisons.
- dev/breeze/src/airflow_breeze/utils/selective_checks.py:994:16: PLR1714 Consider merging multiple comparisons. Use a `set` if the elements are hashable.
- helm_tests/airflow_aux/test_basic_helm_chart.py:82:12: PLR1714 Consider merging multiple comparisons: `version in ("2.3.2", "default")`. Use a `set` if the elements are hashable.
+ helm_tests/airflow_aux/test_basic_helm_chart.py:82:12: PLR1714 Consider merging multiple comparisons: `version in {"2.3.2", "default"}`.
- providers/fab/src/airflow/providers/fab/auth_manager/security_manager/override.py:2015:12: PLR1714 Consider merging multiple comparisons: `provider in ("github", "githublocal")`. Use a `set` if the elements are hashable.
+ providers/fab/src/airflow/providers/fab/auth_manager/security_manager/override.py:2015:12: PLR1714 Consider merging multiple comparisons: `provider in {"github", "githublocal"}`.
- providers/google/src/airflow/providers/google/common/hooks/base_google.py:114:16: PLR1714 Consider merging multiple comparisons: `exception.resp.status in (429, 409)`. Use a `set` if the elements are hashable.
+ providers/google/src/airflow/providers/google/common/hooks/base_google.py:114:16: PLR1714 Consider merging multiple comparisons: `exception.resp.status in {429, 409}`.
... 8 additional changes omitted for project

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

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

- superset/migrations/versions/2023-03-17_13-24_b5ea9d343307_bar_chart_stack_options.py:82:16: PLR1714 Consider merging multiple comparisons: `stack in ("Stack", "Stream")`. Use a `set` if the elements are hashable.
+ superset/migrations/versions/2023-03-17_13-24_b5ea9d343307_bar_chart_stack_options.py:82:16: PLR1714 Consider merging multiple comparisons: `stack in {"Stack", "Stream"}`.
- superset/migrations/versions/2024-05-10_18-02_f84fde59123a_update_charts_with_old_time_comparison.py:204:13: PLR1714 Consider merging multiple comparisons: `Slice.viz_type in ("pop_kpi", "table")`. Use a `set` if the elements are hashable.
+ superset/migrations/versions/2024-05-10_18-02_f84fde59123a_update_charts_with_old_time_comparison.py:204:13: PLR1714 Consider merging multiple comparisons: `Slice.viz_type in {"pop_kpi", "table"}`.
- superset/utils/core.py:314:13: PLR1714 Consider merging multiple comparisons: `standalone_param not in ("false", "0")`. Use a `set` if the elements are hashable.
+ superset/utils/core.py:314:13: PLR1714 Consider merging multiple comparisons: `standalone_param not in {"false", "0"}`.
- tests/integration_tests/databases/commands_tests.py:1187:12: PLR1714 Consider merging multiple comparisons: `database.backend in ("postgresql", "mysql")`. Use a `set` if the elements are hashable.
+ tests/integration_tests/databases/commands_tests.py:1187:12: PLR1714 Consider merging multiple comparisons: `database.backend in {"postgresql", "mysql"}`.

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

- tests/integration/publish/publish_app_integ_base.py:60:16: PLR1714 Consider merging multiple comparisons: `f.suffix in (".yaml", ".json")`. Use a `set` if the elements are hashable.
+ tests/integration/publish/publish_app_integ_base.py:60:16: PLR1714 Consider merging multiple comparisons: `f.suffix in {".yaml", ".json"}`.

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

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

- src/bokeh/core/has_props.py:280:24: PLR1714 Consider merging multiple comparisons: `head in ("bokeh", "__main__")`. Use a `set` if the elements are hashable.
+ src/bokeh/core/has_props.py:280:24: PLR1714 Consider merging multiple comparisons: `head in {"bokeh", "__main__"}`.
- src/bokeh/server/tornado.py:622:12: PLR1714 Consider merging multiple comparisons: `mode in ("server", "server-dev")`. Use a `set` if the elements are hashable.
+ src/bokeh/server/tornado.py:622:12: PLR1714 Consider merging multiple comparisons: `mode in {"server", "server-dev"}`.
- tests/unit/bokeh/embed/test_server__embed.py:215:16: PLR1714 Consider merging multiple comparisons: `r in ("&foo=10&bar=baz", "&bar=baz&foo=10")`. Use a `set` if the elements are hashable.
+ tests/unit/bokeh/embed/test_server__embed.py:215:16: PLR1714 Consider merging multiple comparisons: `r in {"&foo=10&bar=baz", "&bar=baz&foo=10"}`.
- tests/unit/bokeh/embed/test_server__embed.py:222:16: PLR1714 Consider merging multiple comparisons: `r in ("&foo=10&bar=baz", "&bar=baz&foo=10")`. Use a `set` if the elements are hashable.
+ tests/unit/bokeh/embed/test_server__embed.py:222:16: PLR1714 Consider merging multiple comparisons: `r in {"&foo=10&bar=baz", "&bar=baz&foo=10"}`.

latchbio/latch (+2 -2 violations, +0 -0 fixes)

- src/latch_cli/services/stop_pod.py:22:8: PLR1714 Consider merging multiple comparisons: `res.status_code in (403, 404)`. Use a `set` if the elements are hashable.
+ src/latch_cli/services/stop_pod.py:22:8: PLR1714 Consider merging multiple comparisons: `res.status_code in {403, 404}`.
- src/latch_cli/snakemake/single_task_snakemake.py:362:8: PLR1714 Consider merging multiple comparisons: `parsed.scheme not in ("", "docker")`. Use a `set` if the elements are hashable.
+ src/latch_cli/snakemake/single_task_snakemake.py:362:8: PLR1714 Consider merging multiple comparisons: `parsed.scheme not in {"", "docker"}`.

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLR1714 62 31 31 0 0

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser mentioned this pull request Mar 13, 2025
2 tasks
…when all elements are hashable (`PLR1714`)
@MichaReiser MichaReiser force-pushed the micha/repeated-equality-fix branch from 8043a60 to a2521a4 Compare March 13, 2025 08:01
@MichaReiser MichaReiser force-pushed the micha/repeated-equality-fix branch from a2521a4 to b13b4d6 Compare March 13, 2025 08:13
@MichaReiser MichaReiser merged commit 6fc3434 into micha/ruff-0.10 Mar 13, 2025
20 of 21 checks passed
@MichaReiser MichaReiser deleted the micha/repeated-equality-fix branch March 13, 2025 08:22
MichaReiser added a commit that referenced this pull request Mar 13, 2025
…when all elements are hashable (`PLR1714`) (#16685)

## Summary

This PR promotes the fix improvements for `PLR1714` that were introduced
in #14372 to stable.

The improvement is that the fix now proposes to use a set if all
elements are hashable:

```
foo == "bar" or foo == "baz" or foo == "qux"
```

Gets fixed to 

```py
foo in {"bar", "baz", "qux"}
```

where it previously always got fixed to a tuple.

The new fix was first released in ruff 0.8.0 (Nov last year). This is
not a breaking change. The change was preview gated only to get some
extra test coverage.


There are no open issues or PRs related to this changed fix behavior.
MichaReiser added a commit that referenced this pull request Mar 13, 2025
…when all elements are hashable (`PLR1714`) (#16685)

## Summary

This PR promotes the fix improvements for `PLR1714` that were introduced
in #14372 to stable.

The improvement is that the fix now proposes to use a set if all
elements are hashable:

```
foo == "bar" or foo == "baz" or foo == "qux"
```

Gets fixed to 

```py
foo in {"bar", "baz", "qux"}
```

where it previously always got fixed to a tuple.

The new fix was first released in ruff 0.8.0 (Nov last year). This is
not a breaking change. The change was preview gated only to get some
extra test coverage.


There are no open issues or PRs related to this changed fix behavior.
dcreager added a commit that referenced this pull request Mar 14, 2025
* main: (53 commits)
  [syntax-errors] Tuple unpacking in `for` statement iterator clause before Python 3.9 (#16558)
  Ruff v0.10 Release (#16708)
  Add new `noqa` specification to the docs (#16703)
  describe requires-python fallback in docs (#16704)
  [red-knot] handle cycles in MRO/bases resolution (#16693)
  [red-knot] Auto generate statement nodes (#16645)
  [`pylint`] Better inference for `str.strip` (`PLE310`) (#16671)
  [`pylint`] Improve `repeated-equality-comparison` fix to use a `set` when all elements are hashable (`PLR1714`) (#16685)
  [`pylint`/`pep8-naming`] Check `__new__` argument name in `bad-staticmethod-argument` and not `invalid-first-argument-name-for-class-method` (`PLW0211`/`N804`) (#16676)
  [`flake8-pyi`] Stabilize fix for `unused-private-type-var` (`PYI018`) (#16682)
  [`flake8-bandit`] Deprecate `suspicious-xmle-tree-usage` (`S320`) (#16680)
  [`flake8-simplify`] Avoid double negation in fixes (`SIM103`) (#16684)
  [`pyupgrade`]: Improve diagnostic range for `redundant-open-mode` (`UP015`) (#16672)
  Consider all `TYPE_CHECKING` symbols for type-checking blocks (#16669)
  [`pep8-naming`]: Ignore methods decorated with `@typing.override` (`invalid-argument-name`) (#16667)
  Stabilize FURB169 preview behavior (#16666)
  [`pylint`] Detect invalid default value type for `os.environ.get` (`PLW1508`) (#16674)
  [`flake8-pytest-style`] Allow for loops with empty bodies (`PT012`, `PT031`) (#16678)
  [`pyupgrade`]: Deprecate `non-pep604-isinstance` (`UP038`) (#16681)
  [`flake8-type-checking`] Stabilize `runtime-cast-value` (`TC006`) (#16637)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants