Skip to content

new rule - suggest changing function parameter type to readonly alternative if parameter is not mutated inside the function #24196

@DetachHead

Description

@DetachHead

it's very common when designing an API to accidentally use list or set as a type annotation for an input parameter even though the function never actually mutates the value:

def print_values(values: list[int]):
    for value in values:
        print(value)

it would be nice if ruff had a rule to suggest using one of the wider immutable collection types instead:

`values` is not mutated in the function body. consider a wider type such as `Sequence[int]` instead

since the builtin types are far more commonly used than the ABCs, it's easy to forget about them when designing an API. i've seen many third party libraries do this, which often causes unnecessary variance issues for downstream users (for example microsoft/playwright-python#1953)

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-decisionAwaiting a decision from a maintainerruleImplementing 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