Skip to content

Allow # noqa: SIM103 on last return #16250

@JKE-be

Description

@JKE-be

Description

searched: SIM103 Ignore

snippet:

def func(a, b):
    if a == 0:
        return False

    a = a + b / a
    if a != 1:
        return False

    a = a * b
    if a > 1:
        return False

    return True  # noqa SIM103

Command: ruff check /tmp/ruff.py

Output:

/tmp/ruff.py:10:5: SIM103 Return the negated condition directly
   |
 9 |       a = a * b
10 | /     if a > 1:
11 | |         return False
12 | |
13 | |     return True  # noqa SIM103
   | |_______________^ SIM103
   |
   = help: Inline condition

version: ruff 0.9.6

config:

fix = false
show-fixes = true
line-length = 120
output-format = "full"
target-version = "py38"

[lint]
ignore = [
    "E501",
    "E731",
    "RUF012",   # mutable-class-default
]
select = [
    "B",   # flake8-bugbear
    "E",   # pycodestyle
    "F",   # Pyflakes
    "G",   # flake8-logging-format
    "ISC", # flake8-implicit-str-concat
    "PERF",# perflint
    "RUF", # ruff specific rules
    "SIM", # flake8-simplify
    "W",   # pycodestyle
]

Expected (Nice to have feature)

Allow ignoring SIM103 without requiring the # noqa comment on the last if statement itself. The goal is to enable other developers to add another if after the last one without modifying the previous condition.

This is why I don’t want to refactor the last condition as suggested by Ruff. It’s a special case, but I don’t want to ignore SIM103 for the entire file or all files, as I still find it useful in 99% of cases.

Currently, I need to add # noqa on the last ìf to get All checks passed!

def func(a, b):
    if a == 0:
        return False

    a = a + b / a
    if a != 1:
        return False

    a = a * b
-   if a > 1:  # noqa SIM103 
+   if a > 1:
        return False

-   return True
+   return True  # noqa SIM103 

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-decisionAwaiting a decision from a maintainerneeds-designNeeds further design before implementationsuppressionRelated to supression of violations e.g. noqa

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions