PyQt5 returnPressed.connect "Cannot find reference 'connect' in function"
Asked Answered
E

3

13

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()
        ...
Echidna answered 28/1, 2021 at 20:37 Comment(2)
According to the current documentation (and source code), returnPressed is not deprecated, and I doubt it will ever be. Maybe related to this? That said, as far as I can see, it's just a warning, you could just ignore that, especially if it's related to the above report. Simple approach: does the warning appear when your code is run outside the IDE? if not, then it's probably not important.Darees
I am encountering the same. It says that QLineEdit or QTextEdit doesn't have "returnPressed" as an attribute.Theone
A
7

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.

Ataghan answered 12/3, 2021 at 9:30 Comment(0)
M
15

This seems to be an issue with the PyQt5 stub file "QtCore.pyi". Here are 2 alternative solutions to fix it:

Solution 1:

You could ignore any instances of a pyqtBoundSignal's connect method. To do this, go to "Settings > Editor > Inspections > Python > Unresolved references", and add PyQt5.QtCore.pyqtBoundSignal.connect to the list under "Ignore references"

How to ignore annoying warnings

Solution 2:

As explained here, you could modify the stub file named "QtCore.pyi", which can be found in the tree view in PyCharm under "\site-packages\PyQt5".

Location of site-packages directory

After opening QtCore.pyi, make sure you see both of the def connect() lines and both of the def emit() lines below. If they are missing, you can add them, and the relevant PyCharm warnings should disappear.

# Support for new-style signals and slots.
class pyqtSignal:

    signatures = ...    # type: typing.Tuple[str, ...]

    def __init__(self, *types: typing.Any, name: str = ...) -> None: ...

    @typing.overload
    def __get__(self, instance: None, owner: typing.Type['QObject']) -> 'pyqtSignal': ...

    @typing.overload
    def __get__(self, instance: 'QObject', owner: typing.Type['QObject']) -> 'pyqtBoundSignal': ...

    def connect(self, slot: 'PYQT_SLOT') -> 'QMetaObject.Connection': ...
    def emit(self, *args: typing.Any) -> None: ...


class pyqtBoundSignal:

    signal = ...        # type: str

    def __getitem__(self, key: object) -> 'pyqtBoundSignal': ...

    def connect(self, slot: 'PYQT_SLOT') -> 'QMetaObject.Connection': ...

    @typing.overload
    def disconnect(self) -> None: ...

    @typing.overload
    def disconnect(self, slot: typing.Union['PYQT_SLOT', 'QMetaObject.Connection']) -> None: ...

    def emit(self, *args: typing.Any) -> None: ...
Mohl answered 7/1, 2023 at 21:45 Comment(5)
Hello, I have added the two lines but the warning does not disappear. I'm using Qt6.Adsorb
To make sure you're in the correct file, try navigating to QtCore.pyi by doing the following: 1. Find an instance of "QtCore" in your .py file in PyCharm, and click it while holding down ctrl. 2. In the file that opens, look for asterisks to the right of the line numbers, and click one of them. 3. Now, you should be in the correct file, and you can add the appropriate "def connect" and "def emit" lines.Mohl
Regarding Solution 1: For me the Warning disappeared only after adding PyQt5.QtCore.pyqtSignal.connect and PyQt5.QtCore.pyqtBoundSignal.connect to the list of ignored references. (PyCharm 2022.3.2 Community Edition)Sprue
Hi @ScottWeiss, Any chance you would know what the reference would be for PySlide6? The line is timer.timeout.connect() where timer = QTimer. I have added PySlide6.QtCore.pyqtSignal.connect & PySlide6.QtCore.pyqtBoundSignal.connect but to no avail.Cropdusting
Hi @ByteInsight, try ignoring unresolved references to PySide6.QtCore.timeout.connect (or PySide6.QtCore.timeout.*). You could ignore warnings for PySide6.QtCore.* instead, but that might be too broad.Mohl
A
7

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.

Ataghan answered 12/3, 2021 at 9:30 Comment(0)
P
0

i added # type: ignore to the line with the connect call and the issue was resolved.

eg. button.clicked.connect(the_button_was_clicked) # type: ignore

Peaslee answered 27/8, 2024 at 18:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.