Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint: Disable Unnecessary "else" after "return" (no-else-return) warning

I'm looking through my RC file and I can't for the life of me, find which one of these variables disables that feature.

I searched for "if", "else" and "return" and I didn't see anything. Unless I've missed it.

Thanks.

More Info

pylint 1.7.2,
astroid 1.5.3
Python 2.7.10 (default, Jul 30 2016, 18:31:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)]

What I'm putting into the terminal

pylint --rcfile=.pylintrc Test.py

Test code

""" Module Docstring """

def IS_POSITIVE(number):
    """ detects positive """
    if number > 0:
        return "+++"
    else:
        return "---"


print IS_POSITIVE(3)

The print out

************* Module Test
R: 27, 4: Unnecessary "else" after "return" (no-else-return)

------------------------------------------------------------------
Your code has been rated at 8.00/10 (previous run: 8.00/10, +0.00)
like image 241
Biclops Avatar asked Jul 27 '17 00:07

Biclops


People also ask

How do I disable Pylint warnings?

This may be done by adding # pylint: disable=some-message,another-one at the desired block level or at the end of the desired line of code.

Do not use else after return?

At the time of writing this, the Mozilla Coding Style guidelines have this recommendation under “General C/C++ Practices”: Don't put an else right after a return. Delete the else, it's unnecessary and increases indentation level.

How do you make a Pylint ignore an error?

you can ignore it by adding a comment in the format # pylint: disable=[problem-code] at the end of the line where [problem-code] is the value inside pylint(...) in the pylint message – for example, abstract-class-instantiated for the problem report listed above.

What happen when we omit the else block in Python?

It's just saying that there's no need for 'else' since the execution of the function already stopped and if the 'if' condition doesn't succeed, it will still run any code underneath it.


3 Answers

In this particular case you could be better off using a ternary operator.

def is_positive(number):
    return "+++" if number > 0 else "---"
like image 175
Mateo de Mayo Avatar answered Oct 05 '22 16:10

Mateo de Mayo


You should add no-else-return to the comma separated list of disabled options in the disable setting in your .pylintrc file.

Also see the Pylint docs:
http://pylint.pycqa.org/en/latest/technical_reference/features.html#messages-control-options

like image 32
PCManticore Avatar answered Oct 05 '22 15:10

PCManticore


You are looking for no-else-return (R1705). Just add these to your .pylintrc:

[REFACTORING]
no-else-return=no
like image 24
Tamas Hegedus Avatar answered Oct 05 '22 14:10

Tamas Hegedus