False positive when returning a single element tuple

When returning a tuple with a single element we get a false positive:

def tupling(value: tuple[int, ...] | int) -> tuple[int, ...]:
    if isinstance(value, int):
        return (value,)
    return value

With sonar cloud and sonarlint we get python:S1721 Remove the parentheses after this "return" keyword.

This should be allowed as it is a valid use case since this is a tuple declaration and not parentheses uses for visual grouping. If we do return value,, which is less readable, our auto formatter brings the parentheses back. Wrapping with tuple() is overkill and actually slower.

Hello @sodul,
Indeed, the rule issue fix reduces the readability in the case of a single element tuple, and the Black formatter also turns it back. Thank you for reporting it!

I created the following ticket to change this.

Maksim Grebeniuk

1 Like