Search code examples
pythonpyqt5pylintpep8

how to deal with R0915: Too many statements (69/50) (too-many-statements) in pylint?


I have a Python code (GUI with with multiple graphical items) in PyQt5 . How can I shorten the code to suit PEP8 50 statements per function recommendation? Is it suitable to break the definitions of the graphical items to multiple functions?

    def __init__(self, pulse_number, on_time, off_time, x, y, offset):
        self.offset = offset
        self.laser_pulse_n = pulse_number
        self.laser_on_time = on_time
        self.laser_off_time = off_time
        self.laser_x_loc = x
        self.laser_y_loc = y
        # set window properties
        self.setMinimumSize(QSize(250, 300))
        self.setWindowTitle("Laser settings")

        self.int_validator = QIntValidator()

        # LASER
        # Create pulse number label
        self.pulse_number_label = QLabel(self)
        self.pulse_number_label.setGeometry(QRect(10, 0, 80, 20))
        self.pulse_number_label.setText("Pulse n.:")

        # Create pulse number input box
        self.pulse_number_input = QLineEdit(self)
        self.pulse_number_input.setGeometry(QRect(60, 0, 40, 20))
        self.pulse_number_input.setText(str(self.laser_pulse_n))
        self.pulse_number_input.setValidator(self.int_validator)

        # Create laser on label...
        
        ...

        # Apply button
        self.validate_button = QPushButton(self)
        self.validate_button.setGeometry(QRect(10, 240, 230, 40))
        self.validate_button.setToolTip("Click to save settings")
        self.validate_button.setFont(QFont('Times', 20))
        self.validate_button.setText("Apply")
        self.validate_button.clicked.connect(self.validate_settings)


Solution

  • In my opinion there are two solutions : you can disable this warning ecause PyQT is kinda forcing you to code it like this. It's clearer to have every attribute definition in the constructor and if you create sub-function you'll get a attribute-defined-outside-init warning anyway. Another solution would be to refactor a little and create a property setter or a function for setting up the object.

    Ie for example, going from this:

            ...
            self.validate_button = QPushButton(self)
            self.validate_button.setGeometry(QRect(10, 240, 230, 40))
            self.validate_button.setToolTip("Click to save settings")
            self.validate_button.setFont(QFont('Times', 20))
            self.validate_button.setText("Apply")
            self.validate_button.clicked.connect(self.validate_settings)
    

    To this:

            ...
            self.validate_button = self.validate_settings
    
    
        @property
        def validate_button(self):
            return self.__validate_button
    
        @validate_button.setter
        def validate_button(self, validate_settings):
            self.__validate_button = QPushButton(self)
            self.__validate_button.setGeometry(QRect(10, 240, 230, 40))
            self.__validate_button.setToolTip("Click to save settings")
            self.__validate_button.setFont(QFont('Times', 20))
            self.__validate_button.setText("Apply")
            self.__validate_button.clicked.connect(validate_settings)
    

    This has the advantages to permit to change the attributes later if you need to.

    Or this:

            ...
            self.validate_button = QPushButton(self)
            self.__init_validate_button()
    
        def __init_validate_button(self):
            self.validate_button.setGeometry(QRect(10, 240, 230, 40))
            self.validate_button.setToolTip("Click to save settings")
            self.validate_button.setFont(QFont('Times', 20))
            self.validate_button.setText("Apply")
            self.validate_button.clicked.connect(self.validate_settings)
    

    That permit to keeps the class immutable