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 consider-dict-items (C0206) #11688

Merged
merged 19 commits into from
Jun 6, 2024

Conversation

max-muoto
Copy link
Contributor

@max-muoto max-muoto commented Jun 2, 2024

Summary

This PR implements the consider dict items rule from Pylint. Enabling this rule flags:

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


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

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

for instrument in (inline_dict := {"foo": "bar"}): 
    print(f"{instrument}: {inline_dict[instrument]}")

For not using items() to extract the value out of the dict. We ignore the case of an assignment, as you can't modify the underlying representation with the value in the list of tuples returned.

Test Plan

cargo test.

@max-muoto max-muoto changed the title implement-consider-dict-items [pylint] Implement Consider Dict Items (C0206) Jun 2, 2024
@max-muoto max-muoto marked this pull request as ready for review June 2, 2024 16:34
Copy link
Contributor

github-actions bot commented Jun 2, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+35 -0 violations, +0 -0 fixes in 8 projects; 42 projects unchanged)

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

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

+ src/plasmapy/particles/_special_particles.py:260:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ src/plasmapy/particles/decorators.py:677:9: PLC0206 Extracting value from dictionary without calling `.items()`
+ src/plasmapy/particles/ionization_state_collection.py:477:13: PLC0206 Extracting value from dictionary without calling `.items()`
+ src/plasmapy/plasma/grids.py:766:9: PLC0206 Extracting value from dictionary without calling `.items()`

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

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

+ airflow/models/taskinstance.py:3917:9: PLC0206 Extracting value from dictionary without calling `.items()`
+ airflow/www/views.py:2287:13: PLC0206 Extracting value from dictionary without calling `.items()`
+ docs/conf.py:471:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ tests/providers/amazon/aws/executors/batch/test_batch_executor.py:677:9: PLC0206 Extracting value from dictionary without calling `.items()`
+ tests/providers/amazon/aws/executors/ecs/test_ecs_executor.py:1234:9: PLC0206 Extracting value from dictionary without calling `.items()`
+ tests/serialization/test_dag_serialization.py:550:9: PLC0206 Extracting value from dictionary without calling `.items()`

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

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

+ tests/integration/pipeline/test_bootstrap_command.py:229:9: PLC0206 Extracting value from dictionary without calling `.items()`

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

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

+ src/bokeh/application/handlers/code.py:201:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ src/bokeh/core/property/wrappers.py:470:9: PLC0206 Extracting value from dictionary without calling `.items()`

milvus-io/pymilvus (+1 -0 violations, +0 -0 fixes)

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

+ pymilvus/bulk_writer/buffer.py:91:9: PLC0206 Extracting value from dictionary without calling `.items()`

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

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

+ pandas/io/stata.py:2131:5: PLC0206 Extracting value from dictionary without calling `.items()`

rotki/rotki (+3 -0 violations, +0 -0 fixes)

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

+ rotkehlchen/chain/evm/decoding/hop/balances.py:66:9: PLC0206 Extracting value from dictionary without calling `.items()`
+ rotkehlchen/data_import/importers/cryptocom.py:446:13: PLC0206 Extracting value from dictionary without calling `.items()`
+ rotkehlchen/tests/data_migrations/test_migrations.py:150:5: PLC0206 Extracting value from dictionary without calling `.items()`

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

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

+ corporate/views/installation_activity.py:81:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ tools/setup/emoji/emoji_setup_utils.py:90:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/actions/realm_settings.py:237:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/data_import/mattermost.py:135:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/data_import/mattermost.py:165:9: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/data_import/mattermost.py:849:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/data_import/rocketchat.py:166:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/data_import/rocketchat.py:214:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/data_import/rocketchat.py:249:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/data_import/rocketchat.py:65:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/lib/cache.py:254:5: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/lib/markdown/api_return_values_table_generator.py:114:9: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/migrations/0382_create_role_based_system_groups.py:59:13: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/migrations/0403_create_role_based_groups_for_internal_realms.py:68:9: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/tests/test_events.py:451:13: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/tests/test_users.py:723:9: PLC0206 Extracting value from dictionary without calling `.items()`
+ zerver/worker/missedmessage_emails.py:231:13: PLC0206 Extracting value from dictionary without calling `.items()`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLC0206 35 35 0 0 0

@max-muoto
Copy link
Contributor Author

@charliermarsh Any chance you might be able to take a look?

@charliermarsh
Copy link
Member

Interesting, I swear we have a rule for this... But maybe we don't? I see:

@charliermarsh charliermarsh added rule Implementing or modifying a lint rule preview Related to preview mode features labels Jun 6, 2024
@charliermarsh charliermarsh self-assigned this Jun 6, 2024
@max-muoto
Copy link
Contributor Author

Interesting, I swear we have a rule for this... But maybe we don't? I see:

Yeah, a little different since this one doesn't rely on you using items before.

@charliermarsh charliermarsh changed the title [pylint] Implement Consider Dict Items (C0206) [pylint] Implement dict-iter-missing-items (C0206) Jun 6, 2024
Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Thanks!

@charliermarsh charliermarsh enabled auto-merge (squash) June 6, 2024 04:25
@charliermarsh charliermarsh merged commit 5a5a588 into astral-sh:main Jun 6, 2024
19 checks passed
@charliermarsh
Copy link
Member

I reviewed the ecosystem checks and they look right to me.

@max-muoto
Copy link
Contributor Author

Thanks!

Thanks for the clean up! Still getting used to writing more Rust code haha

@charliermarsh
Copy link
Member

No prob! One thing I did in a few places was change x.is_none() followed by x.unwrap() to if let Some(x) = x, which couples "checking if the condition is met" with "getting access to the value".

Comment on lines +4 to +11
dict_index_missing_items.py:9:1: PLC0206 Extracting value from dictionary without calling `.items()`
|
8 | # Errors
9 | / for instrument in ORCHESTRA:
10 | | print(f"{instrument}: {ORCHESTRA[instrument]}")
| |___________________________________________________^ PLC0206
11 |
12 | for instrument in ORCHESTRA:
Copy link
Member

Choose a reason for hiding this comment

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

It would be quite useful to have a infrastructure to provide additional details here similar to Biome. That would avoid highlighting the entire for statement and instead we could highlight individual pieces of code with details.

@max-muoto max-muoto changed the title [pylint] Implement dict-iter-missing-items (C0206) [pylint] Implement consider-dict-items (C0206) Jun 9, 2024
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.

3 participants