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)
Yogurt
  • 2,913
  • 2
  • 32
  • 63
  • 1
    One option would be to listen and remove the "else" clause and just `return "---"`. There is a [question](https://stackoverflow.com/questions/9191388/it-is-more-efficient-to-use-if-return-return-or-if-else-return) 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. – Jared Goguen Jul 27 '17 at 18:02
  • 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. – Yogurt Jul 27 '17 at 18:19
  • Besides the strangeness of the rule itself, it is badly named: it should be `no-return-else`. – C S Oct 07 '18 at 07:24
  • @Biclops Change the pylint command line to --disable=R1705 – SurpriseDog May 30 '19 at 17:59

5 Answers5

19

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

gitaarik
  • 42,736
  • 12
  • 98
  • 105
PCManticore
  • 1,034
  • 6
  • 7
4

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

[REFACTORING]
no-else-return=no
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
  • 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. – Yogurt Jul 27 '17 at 18:01
  • 3
    @Biclops You can also add `disable=R1705` under `[MESSAGES CONTROL]` in yout `.pylintrc` – Derek 朕會功夫 Nov 19 '17 at 20:31
4

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

def is_positive(number):
    return "+++" if number > 0 else "---"
Mateo de Mayo
  • 819
  • 9
  • 10
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

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
-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