Skip to content

[pylint] Fix PLC1802 autofix creating a syntax error and mark autofix as unsafe if there's comments in the len call - #18836

Merged
charliermarsh merged 5 commits into
astral-sh:mainfrom
LaBatata101:fix-PLC1802
Jun 23, 2025
Merged

charliermarsh merged 5 commits into
astral-sh:mainfrom
LaBatata101:fix-PLC1802

Conversation

@LaBatata101

Copy link
Copy Markdown
Contributor

Summary

I've also found another bug while fixing this, where the diagnostic would not trigger if the len call argument variable was shadowed. This fixed a few false negatives in the test cases.
Example:

fruits = []
fruits = []
if len(fruits):  # comment
    ...

Fixes #18811
Fixes #18812

Test Plan

Add regression test

@github-actions

github-actions Bot commented Jun 20, 2025 •

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+7 -0 violations, +0 -2 fixes in 3 projects; 52 projects unchanged)

apache/superset (+1 -0 violations, +0 -2 fixes)

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

- superset/migrations/versions/2018-07-22_11-59_bebcf3fed1fe_convert_dashboard_v1_positions.py:444:61: PLC1802 [*] `len(SEQUENCE)` used as condition without comparison
+ superset/migrations/versions/2018-07-22_11-59_bebcf3fed1fe_convert_dashboard_v1_positions.py:444:61: PLC1802 `len(SEQUENCE)` used as condition without comparison
+ superset/migrations/versions/2018-11-12_13-31_4ce8df208545_migrate_time_range_for_default_filters.py:91:20: PLC1802 [*] `len(keys)` used as condition without comparison

pandas-dev/pandas (+4 -0 violations, +0 -0 fixes)

+ pandas/core/frame.py:7238:14: PLC1802 [*] `len(by)` used as condition without comparison
+ pandas/core/indexes/base.py:7621:12: PLC1802 [*] `len(index_like)` used as condition without comparison
+ pandas/io/parsers/base_parser.py:246:12: PLC1802 [*] `len(ic)` used as condition without comparison
+ pandas/io/parsers/python_parser.py:284:16: PLC1802 [*] `len(content)` used as condition without comparison

astropy/astropy (+2 -0 violations, +0 -0 fixes)

+ astropy/tests/runner.py:547:16: PLC1802 [*] `len(paths)` used as condition without comparison
+ astropy/units/core.py:1328:12: PLC1802 [*] `len(results)` used as condition without comparison

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLC1802 9 7 0 0 2

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+7 -0 violations, +0 -2 fixes in 3 projects; 52 projects unchanged)

apache/superset (+1 -0 violations, +0 -2 fixes)

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

- superset/migrations/versions/2018-07-22_11-59_bebcf3fed1fe_convert_dashboard_v1_positions.py:444:61: PLC1802 [*] `len(SEQUENCE)` used as condition without comparison
+ superset/migrations/versions/2018-07-22_11-59_bebcf3fed1fe_convert_dashboard_v1_positions.py:444:61: PLC1802 `len(SEQUENCE)` used as condition without comparison
+ superset/migrations/versions/2018-11-12_13-31_4ce8df208545_migrate_time_range_for_default_filters.py:91:20: PLC1802 [*] `len(keys)` used as condition without comparison

pandas-dev/pandas (+4 -0 violations, +0 -0 fixes)

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

+ pandas/core/frame.py:7238:14: PLC1802 [*] `len(by)` used as condition without comparison
+ pandas/core/indexes/base.py:7621:12: PLC1802 [*] `len(index_like)` used as condition without comparison
+ pandas/io/parsers/base_parser.py:246:12: PLC1802 [*] `len(ic)` used as condition without comparison
+ pandas/io/parsers/python_parser.py:284:16: PLC1802 [*] `len(content)` used as condition without comparison

astropy/astropy (+2 -0 violations, +0 -0 fixes)

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

+ astropy/tests/runner.py:547:16: PLC1802 [*] `len(paths)` used as condition without comparison
+ astropy/units/core.py:1328:12: PLC1802 [*] `len(results)` used as condition without comparison

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLC1802 9 7 0 0 2

@charliermarsh charliermarsh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

Comment thread crates/ruff_linter/src/rules/pylint/rules/len_test.rs Outdated
Comment thread crates/ruff_linter/src/rules/pylint/rules/len_test.rs Outdated
@charliermarsh
charliermarsh enabled auto-merge (squash) June 23, 2025 00:29
@charliermarsh
charliermarsh merged commit 06a78d0 into astral-sh:main Jun 23, 2025
@LaBatata101
LaBatata101 deleted the fix-PLC1802 branch June 23, 2025 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Pylint] PLC1802 fix can delete comments [Pylint] PLC1802 fix can cause syntax errors from lack of padding

2 participants