summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2022-05-12 15:21:56 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2022-06-13 16:25:36 +0200
commitb30977e6a13810a16d027bccded05336b9bd2ad5 (patch)
tree322b94365612ef0bace1132f0abcf4f41dc3b867
parent218ed2bb53ce90518e128ab34abc4b087255745e (diff)
downloadastroid-git-b30977e6a13810a16d027bccded05336b9bd2ad5.tar.gz
qt brain: Make slot argument optional for disconnect() (#1550)
* qt brain: Make slot argument optional for disconnect() Discussed here: https://github.com/PyCQA/astroid/pull/1531#issuecomment-1111963792 PyQt supports calling .disconnect() without any arguments in order to disconnect all slots: https://www.riverbankcomputing.com/static/Docs/PyQt6/signals_slots.html#disconnect Strictly speaking, slot=None is a wrong call, as actually passing None does not work: https://github.com/python-qt-tools/PyQt5-stubs/pull/103 However, pylint/astroid does not support overloads needed to properly express this: https://github.com/PyCQA/pylint/issues/5264 So, while this is "wrong", it's less wrong than before - without this change, pylint expects a mandatory argument, thus raising a false-positive here: from PyQt5.QtCore import QTimer t = QTimer() t.timeout.connect(lambda: None) t.timeout.disconnect() despite running fine, pylint complains: test.py:4:0: E1120: No value for argument 'slot' in method call (no-value-for-parameter) while with this change, things work fine.
-rw-r--r--ChangeLog2
-rw-r--r--astroid/brain/brain_qt.py4
-rw-r--r--tests/unittest_brain_qt.py18
3 files changed, 23 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index b322afd1..e1a27561 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -13,6 +13,8 @@ What's New in astroid 2.11.6?
=============================
Release date: TBA
+* The Qt brain now correctly treats calling ``.disconnect()`` (with no
+ arguments) on a slot as valid.
What's New in astroid 2.11.5?
diff --git a/astroid/brain/brain_qt.py b/astroid/brain/brain_qt.py
index c0250847..6b97bf67 100644
--- a/astroid/brain/brain_qt.py
+++ b/astroid/brain/brain_qt.py
@@ -24,10 +24,12 @@ def _looks_like_signal(node, signal_name="pyqtSignal"):
def transform_pyqt_signal(node: nodes.FunctionDef) -> None:
module = parse(
"""
+ _UNSET = object()
+
class pyqtSignal(object):
def connect(self, slot, type=None, no_receiver_check=False):
pass
- def disconnect(self, slot):
+ def disconnect(self, slot=_UNSET):
pass
def emit(self, *args):
pass
diff --git a/tests/unittest_brain_qt.py b/tests/unittest_brain_qt.py
index f9805bce..93ef4dd1 100644
--- a/tests/unittest_brain_qt.py
+++ b/tests/unittest_brain_qt.py
@@ -52,3 +52,21 @@ class TestBrainQt:
pytest.skip("PyQt6 C bindings may not be installed?")
assert isinstance(attribute_node, FunctionDef)
assert attribute_node.implicit_parameters() == 1
+
+ @staticmethod
+ def test_slot_disconnect_no_args() -> None:
+ """Test calling .disconnect() on a signal.
+
+ See https://github.com/PyCQA/astroid/pull/1531#issuecomment-1111963792
+ """
+ src = """
+ from PyQt6.QtCore import QTimer
+ timer = QTimer()
+ timer.timeout.disconnect #@
+ """
+ node = extract_node(src)
+ attribute_node = node.inferred()[0]
+ if attribute_node is Uninferable:
+ pytest.skip("PyQt6 C bindings may not be installed?")
+ assert isinstance(attribute_node, FunctionDef)
+ assert attribute_node.args.defaults