Skip to content

Commit c4e11cb

Browse files
jacobtylerwallsPierre-Sassoulas
authored andcommitted
Make FunctionDef.implicit_parameters return 1 for methods (#1531)
1 parent b28067e commit c4e11cb

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Release date: TBA
1414

1515
* Fix ``col_offset`` attribute for nodes involving ``with`` on ``PyPy``.
1616

17+
* Made ``FunctionDef.implicit_parameters`` return 1 for methods by making
18+
``FunctionDef.is_bound`` return ``True``, as it does for class methods.
19+
20+
Closes PyCQA/pylint#6464
21+
1722
* Fixed a crash when ``_filter_stmts`` encounters an ``EmptyNode``.
1823

1924
Closes PyCQA/pylint#6438

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,9 @@ def blockstart_tolineno(self):
15871587
"""
15881588
return self.args.tolineno
15891589

1590+
def implicit_parameters(self) -> Literal[0, 1]:
1591+
return 1 if self.is_bound() else 0
1592+
15901593
def block_range(self, lineno):
15911594
"""Get a range from the given line number to where this node ends.
15921595
@@ -1648,7 +1651,7 @@ def is_bound(self):
16481651
False otherwise.
16491652
:rtype: bool
16501653
"""
1651-
return self.type == "classmethod"
1654+
return self.type in {"method", "classmethod"}
16521655

16531656
def is_abstract(self, pass_is_abstract=True, any_raise_is_abstract=False):
16541657
"""Check if the method is abstract.

tests/unittest_brain_qt.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
@pytest.mark.skipif(HAS_PYQT6 is None, reason="This test requires the PyQt6 library.")
1818
class TestBrainQt:
19+
AstroidManager.brain["extension_package_whitelist"] = {"PyQt6"}
20+
1921
@staticmethod
2022
def test_value_of_lambda_instance_attrs_is_list():
2123
"""Regression test for https://github.com/PyCQA/pylint/issues/6221
@@ -28,11 +30,25 @@ def test_value_of_lambda_instance_attrs_is_list():
2830
from PyQt6 import QtPrintSupport as printsupport
2931
printsupport.QPrintPreviewDialog.paintRequested #@
3032
"""
31-
AstroidManager.brain["extension_package_whitelist"] = {"PyQt6.QtPrintSupport"}
3233
node = extract_node(src)
3334
attribute_node = node.inferred()[0]
3435
if attribute_node is Uninferable:
3536
pytest.skip("PyQt6 C bindings may not be installed?")
3637
assert isinstance(attribute_node, UnboundMethod)
3738
# scoped_nodes.Lambda.instance_attrs is typed as Dict[str, List[NodeNG]]
3839
assert isinstance(attribute_node.instance_attrs["connect"][0], FunctionDef)
40+
41+
@staticmethod
42+
def test_implicit_parameters() -> None:
43+
"""Regression test for https://github.com/PyCQA/pylint/issues/6464"""
44+
src = """
45+
from PyQt6.QtCore import QTimer
46+
timer = QTimer()
47+
timer.timeout.connect #@
48+
"""
49+
node = extract_node(src)
50+
attribute_node = node.inferred()[0]
51+
if attribute_node is Uninferable:
52+
pytest.skip("PyQt6 C bindings may not be installed?")
53+
assert isinstance(attribute_node, FunctionDef)
54+
assert attribute_node.implicit_parameters() == 1

tests/unittest_scoped_nodes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,24 @@ def test():
617617
self.assertIsInstance(one, nodes.Const)
618618
self.assertEqual(one.value, 1)
619619

620+
def test_func_is_bound(self) -> None:
621+
data = """
622+
class MyClass:
623+
def bound(): #@
624+
pass
625+
"""
626+
func = builder.extract_node(data)
627+
self.assertIs(func.is_bound(), True)
628+
self.assertEqual(func.implicit_parameters(), 1)
629+
630+
data2 = """
631+
def not_bound(): #@
632+
pass
633+
"""
634+
func2 = builder.extract_node(data2)
635+
self.assertIs(func2.is_bound(), False)
636+
self.assertEqual(func2.implicit_parameters(), 0)
637+
620638
def test_type_builtin_descriptor_subclasses(self) -> None:
621639
astroid = builder.parse(
622640
"""

0 commit comments

Comments
 (0)