5

To the following code:

"""Test with unused return value"""

from typing import List

def sorted_int_list(values: List[int]) -> List[int]:
    """Returns a new, sorted list"""
    return sorted(values)

def main() -> None:
    """Ignoring value returned by sorted_int_list"""
    values: List[int] = [3, 1, 2]
    sorted_int_list(values) # Should create some kind or error
    print(values)

if __name__ == "__main__":
    main()

pylint says:

Your code has been rated at 10.00/10

However I would like to have it report the unused return value of sorted_int_list. Can it be done?

Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134

1 Answers1

1

Python is a dynamic language, static analysis tools can not know whether the function has a return value. such as:

def foo(a):
    if a>0 return 42
    else: pass #do nothing, and not return if a <= 0

static analysis tools cant help you solve the logic problem.

Related discussion:https://github.com/PyCQA/pylint/issues/647

MatejMecka
  • 1,448
  • 2
  • 24
  • 37
veekxt
  • 372
  • 1
  • 9
  • Thanks a lot. Makes sense of course. In my actual code base, everything is annotated with (return) types, so I just updated the code in my question accordingly. – Tobias Hermann Aug 15 '18 at 13:33
  • 5
    This answer isn't valid since late 2017; the example `foo()` function is statically caught by pylint's `inconsistent-return-statements` checker nowadays: . I'd still like to get return-value-not-used warnings for relevant functions! – Johan Walles Apr 03 '20 at 04:29
  • @JohanWalles FYI I've created a pylint featuer request for this: https://github.com/PyCQA/pylint/issues/7935 . See also: https://stackoverflow.com/questions/74785720/python-prevent-discarding-of-the-function-return-value – salmin Dec 13 '22 at 15:40