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

[pylint] Implement len-test (PLC1802) #14309

Merged
merged 10 commits into from
Nov 26, 2024

Conversation

Lokejoke
Copy link
Contributor

@Lokejoke Lokejoke commented Nov 13, 2024

Summary

This PR implements use-implicit-booleaness-not-len / C1802

For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

Test Plan

Checked against pylint tests:

  • I would argue some of the tests (commented out) are not very intuitive and thus not helpful

Notes

  • Naming: Rule name and its error message (adopted from pylint) do not follow conventions used in Ruff (len-as-condition might be solution)

References

PEP 8 on empty sequences and implicit booleaness

@Lokejoke Lokejoke marked this pull request as draft November 13, 2024 08:47
Copy link
Contributor

github-actions bot commented Nov 13, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+21 -0 violations, +0 -0 fixes in 6 projects; 49 projects unchanged)

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

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

+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:1639:8: PLC1802 [*] `len(success_entries)` used as condition without comparison
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:1646:8: PLC1802 [*] `len(skipped_entries)` used as condition without comparison
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:1751:12: PLC1802 [*] `len(success_entries)` used as condition without comparison
+ dev/breeze/src/airflow_breeze/commands/release_management_commands.py:1758:12: PLC1802 [*] `len(skipped_entries)` used as condition without comparison
+ providers/src/airflow/providers/amazon/aws/hooks/glue.py:264:16: PLC1802 [*] `len(fetched_logs)` used as condition without comparison
+ providers/src/airflow/providers/sftp/sensors/sftp.py:129:16: PLC1802 [*] `len(files_found)` used as condition without comparison
+ providers/src/airflow/providers/snowflake/operators/snowflake.py:560:20: PLC1802 [*] `len(queries_in_progress)` used as condition without comparison

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

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

+ superset/databases/filters.py:98:16: PLC1802 [*] `len(allowed_schemas)` 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-07-22_11-59_bebcf3fed1fe_convert_dashboard_v1_positions.py:546:11: PLC1802 [*] `len(ordered_raw_positions)` used as condition without comparison
+ superset/migrations/versions/2018-07-22_11-59_bebcf3fed1fe_convert_dashboard_v1_positions.py:559:16: PLC1802 [*] `len(available_columns_index)` used as condition without comparison
+ superset/migrations/versions/2018-11-12_13-31_4ce8df208545_migrate_time_range_for_default_filters.py:68:24: PLC1802 [*] `len(keys)` used as condition without comparison

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

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

+ src/bokeh/core/validation/check.py:216:27: PLC1802 [*] `len(warnings)` used as condition without comparison

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

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

+ zerver/views/invite.py:127:8: PLC1802 [*] `len(streams)` used as condition without comparison
+ zerver/views/invite.py:243:8: PLC1802 [*] `len(streams)` used as condition without comparison

pytest-dev/pytest (+1 -0 violations, +0 -0 fixes)

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

+ testing/_py/test_local.py:218:16: PLC1802 [*] `len(lst)` used as condition without comparison

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

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

+ astropy/io/ascii/misc.py:83:12: PLC1802 [*] `len(first)` used as condition without comparison
+ astropy/units/core.py:1341:16: PLC1802 [*] `len(subresults)` used as condition without comparison
+ astropy/units/core.py:2501:12: PLC1802 [*] `len(names)` used as condition without comparison
+ astropy/wcs/wcs.py:2950:16: PLC1802 [*] `len(missing_keys)` used as condition without comparison
+ astropy/wcs/wcs.py:3765:20: PLC1802 [*] `len(content)` used as condition without comparison

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLC1802 21 21 0 0 0

@Lokejoke Lokejoke marked this pull request as ready for review November 13, 2024 10:55
@Lokejoke Lokejoke changed the title [pylint] use-implicit-booleaness-not-len (C1802) [pylint] Implement use-implicit-booleaness-not-len (C1802) Nov 14, 2024
@Lokejoke Lokejoke changed the title [pylint] Implement use-implicit-booleaness-not-len (C1802) [pylint] Implement use-implicit-booleaness-not-len (C1802) Nov 14, 2024
@Lokejoke Lokejoke changed the title [pylint] Implement use-implicit-booleaness-not-len (C1802) [pylint] Implement use-implicit-booleaness-not-len (C1802) Nov 14, 2024
@Lokejoke Lokejoke changed the title [pylint] Implement use-implicit-booleaness-not-len (C1802) [pylint] Implement use-implicit-booleaness-not-len (PLC1802) Nov 14, 2024
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

Thanks so much for this contribution! Some minor nits/requests for a few more tests. I also didn't get a chance to review the implementation carefully, but I'll hold off for now in case the new tests inspire any changes.

@dylwil3 dylwil3 added rule Implementing or modifying a lint rule preview Related to preview mode features labels Nov 14, 2024
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

This looks really great, thank you! I think even with the possible misses around control flow, this makes sense to implement. You would have to be pretty pathological to create a custom class C that implements __len__ and __bool__ where bool(x) != bool(len(x)) for x: C... so I think it's okay.

@Lokejoke Lokejoke changed the title [pylint] Implement use-implicit-booleaness-not-len (PLC1802) [pylint] Implement len-as-condition (PLC1802) Nov 25, 2024
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

Almost there! Looking good!

@MichaReiser
Copy link
Member

Naming: Maybe len-test to align with https://docs.astral.sh/ruff/rules/not-in-test/#not-in-test-e713

@Lokejoke Lokejoke changed the title [pylint] Implement len-as-condition (PLC1802) [pylint] Implement len-test (PLC1802`) Nov 26, 2024
@Lokejoke Lokejoke changed the title [pylint] Implement len-test (PLC1802`) [pylint] Implement len-test (PLC1802) Nov 26, 2024
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for the great contribution and your patience through all the reviews!

@dylwil3 dylwil3 merged commit 82c01aa into astral-sh:main Nov 26, 2024
20 checks passed
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