Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pylint protection against self-assignment

Tags:

python

pylint

I have this test file:

"""module docstring"""


class Aclass:
    """class docstring"""

    def __init__(self, attr=None, attr2=None):
        self.attr = attr
        self.attr2 = attr2

    def __repr__(self):
        return 'instance_of the Aclass {self.attr}.'

    def __str__(self):
        return 'The A with: {self.attr}.'


def init_a():
    """function docstring"""
    a_inst = Aclass()
    attr = 1
    attr2 = 2
    a_inst.attr2 = attr2
    # should be: a_inst.attr = attr, but have a typo
    attr = attr

and I inspect it using pylint, and the output shows everything is alright.

$ pylint test.py 

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

Based on the linting, i expect the flag about suspicious usage in software language, because i don't know when this code a=1; a=a can be useful. And I want to see some warning, for example: unused variable or self-assignment etc. Is there way using the pylint? (I know about Pycharm and sonarqube). Example of the sonar rules.

public void foo() {
    int x = 3;
    x = x;
}

Such assignments are useless, and may indicate a logic error or typo.

details about the pylint

pylint 2.3.1
astroid 2.2.5
Python 3.6.5 (default, May  5 2019, 22:05:54) 
[GCC 6.3.0 20170516]

UPDATE Has added to the version pylint 2.4

like image 879
Brown Bear Avatar asked May 13 '19 12:05

Brown Bear


1 Answers

I've took a look at Pylint rules and I did not find anything to help you solve this issue. What I did find is that you can write your own checker and make pylint using it :

$ pylint yourpieceofcode.py --load-plugins=checker

checker.py :

from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker


class SelfAssignChecker(BaseChecker):
    __implements__ = IAstroidChecker

    name = 'self-assign-returns'
    priority = -1
    msgs = {
        'W5555': (
            'Self assignment (%s).',
            'self-assign',
            'useless assignment.'
        ),
    }

    def visit_assign(self, node):
        names = []
        for child in node.get_children():
            if not hasattr(child, 'name'):
                return
            if child.name not in names:
                names.append(child.name)
            else:
                self.add_message("self-assign", node=node, args=child.name)


def register(linter):
    linter.register_checker(SelfAssignChecker(linter))

Doc here ! :)

Tested on your file test.py. Output :

$ pylint --load-plugins=checker test.py
************* Module test
test.py:25:0: C0304: Final newline missing (missing-final-newline)
test.py:25:4: W5555: Self assignment (attr). (self-assign)

------------------------------------------------------------------
Your code has been rated at 8.57/10 (previous run: 9.29/10, -0.71)

Pylint version :

$ pylint --version
pylint 2.3.1
astroid 2.2.5
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0]
like image 177
Chocorean Avatar answered Nov 07 '22 00:11

Chocorean