python: prevent discarding of the function return value

366 views Asked by At

There's a common error in the code where people write something like:

if (id):
    query.filter(row.id == id)

instead of

if (id):
    query = query.filter(row.id == id)

The code looks "valid" and it's very hard to spot these by hand. In C++ there's the [[nodiscard]] function attribute that effectively prevents this mistake by enforcing the usage of the return value. I wasn't able to find anything similar in Python, does it exist?

UPD A similar question: How to make pylint report unused return values

I've checked pylint and mypy, apparently neither of them catch this as of today.

UPD2 A pylint feature request: https://github.com/PyCQA/pylint/issues/7935

0

There are 0 answers