Skip to content

ruff rule PLC0206 and RUF021#270

Merged
cclauss merged 2 commits into
nodejs:mainfrom
cclauss:ruff-rule-PLC0206
Nov 25, 2024
Merged

ruff rule PLC0206 and RUF021#270
cclauss merged 2 commits into
nodejs:mainfrom
cclauss:ruff-rule-PLC0206

Conversation

@cclauss

@cclauss cclauss commented Nov 25, 2024

Copy link
Copy Markdown
Contributor

% 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

cclauss commented Nov 25, 2024

Copy link
Copy Markdown
Contributor Author

@nodejs/python

@StefanStojanovic StefanStojanovic left a comment

Copy link
Copy Markdown
Contributor

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
@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