这是indexloc提供的服务,不要输入任何密码
Skip to content

SIM108 is blocked by annotations #21406

@dscorbett

Description

@dscorbett

Summary

if-else-block-instead-of-if-exp (SIM108) should apply when the assignment in the if block (though not the one in the else block) is annotated. Example:

$ cat >sim108.py <<'# EOF'
def f(x: object) -> int:
    if x:
        y: int = 1
    else:
        y = 2
    return y
# EOF

$ ruff --isolated check sim108.py --select SIM108 --unsafe-fixes --fix
All checks passed!

The ideal fixed version is:

def f(x: object) -> int:
    y: int = 1 if x else 2
    return y

Version

ruff 0.14.4 (c7ff982 2025-11-06)

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions