Skip to content

False negative - assign list.reverse() instead of reversed(list) #10383

Open
@orSolocate

Description

@orSolocate

Current problem

Parallel issue (being 'brought back from the grave') - #5722.

when you use the reverse() method on a list object, it modifies (reverses) the list and returns None.
A common mistake is to assign the return value of this assignment (i.e. None) to a variable thinking you assigned the sorted list instead.

lst=[3,2,1]
A=lst.reverse()

Results with:
None in A and [1,2,3] in lst.

The problem is that Pylint does not detect that, running this snippet shows no messages.

Desired solution

When this case is detected, a message should be emitted explaining the user to use the built-in reversed() function instead.
Issue is a parallel case of issue #5722, using the assignment-from-none message looks proper to me.

Additional context

There might be a compatibility issue with detecting the list.sort() and list.reverse() methods in newer version of python, where pylint sees the stub function (that contains only pass) and thinks it is a function with no return, emitting the assignment-from-no-return error message. When running pylint on the following snippet on Python3.8 and Python3.10 I got:

Snippet:

lst = [1, 2, 3]
rev_lst = lst.reverse()
sort_lst_err = lst.sort()

Python3.8: got assignment-from-none for the "lst.sort()" line.
Python3.10: got assignment-from-no-return for both lst.reverse() and lst.sort() lines.

Is it a false-positive? or did I do something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement ✨Improvement to a componentNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions