Skip to content

Comments

[flake8-simplify] Make example error out-of-the-box (SIM401)#19110

Merged
ntBre merged 1 commit intoastral-sh:mainfrom
MeGaGiGaGon:patch-7
Jul 3, 2025
Merged

[flake8-simplify] Make example error out-of-the-box (SIM401)#19110
ntBre merged 1 commit intoastral-sh:mainfrom
MeGaGiGaGon:patch-7

Conversation

@MeGaGiGaGon
Copy link
Contributor

Summary

Part of #18972

This PR makes [enumerate-for-loop if-else-block-instead-of-dict-get (SIM401)'s example error out-of-the-box

Old example

if "bar" in foo:
    value = foo["bar"]
else:
    value = 0

New example

foo = {}
if "bar" in foo:
    value = foo["bar"]
else:
    value = 0

The "Use instead" section was also updated similarly.

The docs for SIM401 also has another section on the preview ternary version, but it does not seem to check that the variable is a dict (bug?) https://play.ruff.rs/c0feada8-a7fe-43f7-b57e-c10520fdcdca

Test Plan

N/A, no functionality/tests affected

@github-actions
Copy link
Contributor

github-actions bot commented Jul 3, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks!

Yeah, I think the if-expression version is missing this check:

if !test_dict
.as_name_expr()
.is_some_and(|dict_name| is_known_to_be_of_type_dict(checker.semantic(), dict_name))
{
return;
}

Do you want to open a follow-up issue or PR for that? Good catch!

@ntBre ntBre added the documentation Improvements or additions to documentation label Jul 3, 2025
@ntBre ntBre merged commit 26bb8f7 into astral-sh:main Jul 3, 2025
36 checks passed
@MeGaGiGaGon MeGaGiGaGon deleted the patch-7 branch July 3, 2025 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants