Skip to content

if-else-block-instead-of-if-exp autofix predates the desired if-else-block-instead-of-dict-get autofix #4932

@tjkuson

Description

@tjkuson

Expected behaviour

ruff check --fix --select SIM . should autofix

obj: dict = ...
key = ...

if key in obj:
    value = obj[key]
else:
    value = "Not found"

to this:

obj: dict = ...
key = ...

value = obj.get(key, "Not found")

Actual behaviour

ruff check --fix --select SIM . autofixes the above code to

obj: dict = ...
key = ...

value = obj[key] if key in obj else "Not found"

This happens because if-else-block-instead-of-if-exp (SIM108) autofixes before if-else-block-instead-of-dict-get (SIM401), and SIM401 does not flag violations in ternary operations.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions