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

5

15

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)
Mosher answered 27/7, 2017 at 0:49 Comment(4)
One option would be to listen and remove the "else" clause and just return "---". There is a question about which of these is preferred in Python and I don't think there is a clear answer, but it appears that pylint weighs in on the "no else" side.Bernardinebernardo
Yeah I know, I've resorted to changing the code to do that, but it bugs me still. Tams Hegedus helped me out by pointing out that I should simply add "no-else-return=no" to turn off this option, but it still doesn't work. Which goads me to no end.Mosher
Besides the strangeness of the rule itself, it is badly named: it should be no-return-else.Equation
@Biclops Change the pylint command line to --disable=R1705Communard
G
20

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

Gorgon answered 29/7, 2017 at 0:33 Comment(1)
Thank you. Of course, I should have searched the manual for disable. Thank you again.Mosher
L
4

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

[REFACTORING]
no-else-return=no
Lotson answered 27/7, 2017 at 1:4 Comment(2)
Unfortunately that didn't do it. I added more information in my question. My RC file is in my work directory, and it is picking up my changes since I've been modifying the regular expressions for what is an allowed variable/function/class name. I don't know if this is important but the no-else-return variable was not in the default RC file. Thanks.Mosher
@Biclops You can also add disable=R1705 under [MESSAGES CONTROL] in yout .pylintrcTirzah
S
4

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

def is_positive(number):
    return "+++" if number > 0 else "---"
Simson answered 22/7, 2019 at 14:46 Comment(0)
A
2

I would not disable it. Instead, OP can change the code to

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

Similar issue has been addressed in other questions, like

Alunite answered 13/9, 2022 at 11:27 Comment(0)
M
-3

to make pylint happy, resolve like below

    (1)
    jobdone = False
    if (not fdb) and (source.lower() in space.SOURCE):
        _ = space.SOURCE[source.lower()]
        _().dojob()
        jobdone = True
    elif fdb and (source.lower() in space.SOURCE):
        _ = space.SOURCE[source.lower()]
        _().dojob()
        jobdone = True

    if jobdone:
        break
    (2)
    retval = None
    if (not fdb) and (source.lower() in space.SOURCE):
        _ = space.SOURCE[source.lower()]
        retval = _().getter()
        jobdone = True
    elif fdb and (source.lower() in space.SOURCE):
        _ = space.SOURCE[source.lower()]
        retval = _().getter()
        jobdone = True

    if jobdone:
        return retval
Machinist answered 16/3, 2020 at 4:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.