Skip to content

RET504 false-positive when nested functions reference the variable #14052

@Enegg

Description

@Enegg

I've searched for RET504 before opening the issue.

def outer() -> list[object]:
    @register
    async def inner() -> None:
        print(layout)

    layout = [...]
    return layout  # noqa: RET504

The rule reports the assignment to layout as unnecessary, and provides a fix by rewriting it as return [...], which will break the inner function.

The rule doesn't trigger when layout is assigned to above the definition of the inner function, however in my use-case a decorator replaces the function with an object, which is then used to define layout, mandating the assignment to layout happens below the function definition.


[tool.ruff.lint]
preview = true
explicit-preview-rules = true
select = [ "ALL" ]

VSCode extension (v2024.54.0), ruff==0.7.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions