Search code examples
python-3.xpycharmpyqt5

PyQt5 returnPressed.connect "Cannot find reference 'connect' in function"


Just a bit of a pedantic question, but I'm getting a "Cannot find reference 'connect' in function" warning in PyCharm. (Relating to my returnPressed.connect) Is it just a PyCharm bug or the function is deprecated, I couldn't find information on this online.

I am only getting this .connect error on "returnPressed" all the rest are perfectly fine. It is my only warnings left and it bugs the hell out of me.

class Login(QWidget):
    switch_window = QtCore.pyqtSignal()

    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        ...

        self.password = QLineEdit(self)
        self.password.setGeometry(QRect(50, 368, 200, 25))
        self.password.setFont(QtGui.QFont("Times", 13))
        self.password.setAlignment(QtCore.Qt.AlignCenter)
        self.password.setEchoMode(QLineEdit.Password)
        self.password.setStyleSheet("QLineEdit {border-radius: 10px}")
        self.password.setPlaceholderText("Password")
        self.password.returnPressed.connect(self.authenticate)
        ...

    def authenticate(self):
        ...
        self.switch_window.emit()
        ...

Solution

  • I also have this kind of weird warning. It bugs me as well, since my code works perfectly fine (also in exported version using pyinstaller, so its validity is not IDE dependant). Therefore, I ignore it completely, and I think that you could do so. I totally agree with musicamante's comment.

    However, for completeness sake, here is my more thorough analysis.

    Here are two examples of mine where connect yields the following warning in PyCharm Community 2020.3.3 (Python 3.9 and PyQt5) :

    def setupConfirmPID(self):
        r"""Build the MessageBox instance that will confirm default PID values setting."""
        self.msgConfirmDefaultPID = QMessageBox()
        ...
        self.msgConfirmDefaultPID.setStandardButtons(QMessageBox.Cancel | QMessageBox.Ok)
        self.msgConfirmDefaultPID.buttonClicked.connect(self.defaultPIDConfirmation)
    
    # ----------------------
    # Other example within an other class of the same package
    
        # Declare rxWorker and rxThread then assign RxWorker instance to rxThread
        self.rxWorker = RxWorker(...)
        self.rxThread = QThread()
        self.rxWorker.moveToThread(self.rxThread)
    
        # Signal/Slot connections for inter-thread communication
        self.rxThread.started.connect(self.rxWorker.run)
    

    Both of the above trigger the following warning message :

    Cannot find reference 'connect' in 'function | pyqtBoundSignal'
    

    I have found after some research that such a warning is shown because the signal object is actually a pyQtBoundSignal, not a pyQtSignal. Maybe this subtelty is only shown in PyQt5 ? The only difference that I could find is that a "bound" pyqtSignal is created when the signal is a parameter of an instance (created from within init()) and to keep it as a "regular" signal (which will connect without warning), I need to implement it as a class attribute (before init()).

    So, to get completely get rid of your warning, the only thing I see - apart from issuing a bug report to JetBrains (PyCharm editor) - is to override the classes whose signals you want to connect to and make sure the signals are declared there as class attributes, not instance attributes. That would be waaay overkill though.