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

ruff rule PLC0206 and RUF021 #270

Merged
merged 2 commits into from
Nov 25, 2024
Merged

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Nov 25, 2024

% ruff rule PLC0206

dict-index-missing-items (PLC0206)

Derived from the Pylint linter.

What it does

Checks for dictionary iterations that extract the dictionary value
via explicit indexing, instead of using .items().

Why is this bad?

Iterating over a dictionary with .items() is semantically clearer
and more efficient than extracting the value with the key.

Example

ORCHESTRA = {
    "violin": "strings",
    "oboe": "woodwind",
    "tuba": "brass",
    "gong": "percussion",
}

for instrument in ORCHESTRA:
    print(f"{instrument}: {ORCHESTRA[instrument]}")

Use instead:

ORCHESTRA = {
    "violin": "strings",
    "oboe": "woodwind",
    "tuba": "brass",
    "gong": "percussion",
}

for instrument, section in ORCHESTRA.items():
    print(f"{instrument}: {section}")

% ruff rule RUF021

parenthesize-chained-operators (RUF021)

Derived from the Ruff-specific rules linter.

Fix is always available.

What it does

Checks for chained operators where adding parentheses could improve the
clarity of the code.

Why is this bad?

and always binds more tightly than or when chaining the two together,
but this can be hard to remember (and sometimes surprising).
Adding parentheses in these situations can greatly improve code readability,
with no change to semantics or performance.

For example:

a, b, c = 1, 0, 2
x = a or b and c

d, e, f = 0, 1, 2
y = d and e or f

Use instead:

a, b, c = 1, 0, 2
x = a or (b and c)

d, e, f = 0, 1, 2
y = (d and e) or f

@cclauss cclauss changed the title ruff rule PLC0206 ruff rule PLC0206 and RUF021 Nov 25, 2024
@cclauss
Copy link
Contributor Author

cclauss commented Nov 25, 2024

@nodejs/python

Copy link
Contributor

@StefanStojanovic StefanStojanovic left a comment

Choose a reason for hiding this comment

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

LGTM if all checks pass.

@cclauss cclauss merged commit db89089 into nodejs:main Nov 25, 2024
50 checks passed
@cclauss cclauss deleted the ruff-rule-PLC0206 branch November 25, 2024 16:18
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.

2 participants