Debian Codegraph @

tests/brain/test_qt.py

raw
1 # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2 # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
3 # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
4
5 from importlib.util import find_spec
6
7 import pytest
8
9 from astroid import Uninferable, extract_node
10 from astroid.bases import UnboundMethod
11 from astroid.const import PY312_PLUS
12 from astroid.manager import AstroidManager
13 from astroid.nodes import FunctionDef
14
15 HAS_PYQT6 = find_spec("PyQt6")
16
17
18 @pytest.mark.skipif(HAS_PYQT6 is None, reason="These tests require the PyQt6 library.")
19 # TODO: enable for Python 3.12 as soon as PyQt6 release is compatible
20 @pytest.mark.skipif(PY312_PLUS, reason="This test was segfaulting with Python 3.12.")
21 class TestBrainQt:
22 AstroidManager.brain["extension_package_whitelist"] = {"PyQt6"} # noqa: RUF012
23
24 @staticmethod
25 def test_value_of_lambda_instance_attrs_is_list():
26 """Regression test for https://github.com/pylint-dev/pylint/issues/6221.
27
28 A crash occurred in pylint when a nodes.FunctionDef was iterated directly,
29 giving items like "self" instead of iterating a one-element list containing
30 the wanted nodes.FunctionDef.
31 """
32 src = """
33 from PyQt6 import QtPrintSupport as printsupport
34 printsupport.QPrintPreviewDialog.paintRequested #@
35 """
36 node = extract_node(src)
37 attribute_node = node.inferred()[0]
38 if attribute_node is Uninferable:
39 pytest.skip("PyQt6 C bindings may not be installed?")
40 assert isinstance(attribute_node, UnboundMethod)
41 # scoped_nodes.Lambda.instance_attrs is typed as Dict[str, List[NodeNG]]
42 assert isinstance(attribute_node.instance_attrs["connect"][0], FunctionDef)
43
44 @staticmethod
45 def test_implicit_parameters() -> None:
46 """Regression test for https://github.com/pylint-dev/pylint/issues/6464."""
47 src = """
48 from PyQt6.QtCore import QTimer
49 timer = QTimer()
50 timer.timeout.connect #@
51 """
52 node = extract_node(src)
53 attribute_node = node.inferred()[0]
54 if attribute_node is Uninferable:
55 pytest.skip("PyQt6 C bindings may not be installed?")
56 assert isinstance(attribute_node, FunctionDef)
57 assert attribute_node.implicit_parameters() == 1
58
59 @staticmethod
60 def test_slot_disconnect_no_args() -> None:
61 """Test calling .disconnect() on a signal.
62
63 See https://github.com/pylint-dev/astroid/pull/1531#issuecomment-1111963792
64 """
65 src = """
66 from PyQt6.QtCore import QTimer
67 timer = QTimer()
68 timer.timeout.disconnect #@
69 """
70 node = extract_node(src)
71 attribute_node = node.inferred()[0]
72 if attribute_node is Uninferable:
73 pytest.skip("PyQt6 C bindings may not be installed?")
74 assert isinstance(attribute_node, FunctionDef)
75 assert attribute_node.args.defaults