Skip to content

Commit e476d0c

Browse files
authored
Properly construct the arguments of infered property descriptors (#796)
Close pylint-dev/pylint#3648
1 parent 8a961d2 commit e476d0c

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Release Date: TBA
1010

1111
Close PyCQA/pylint#3519
1212

13+
* Properly construct the arguments of infered property descriptors
14+
15+
Close PyCQA/pylint#3648
16+
1317

1418
What's New in astroid 2.4.1?
1519
============================

astroid/interpreter/objectmodel.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,27 @@ class PropertyModel(ObjectModel):
743743
"""Model for a builtin property"""
744744

745745
# pylint: disable=import-outside-toplevel
746+
def _init_function(self, name):
747+
from astroid.node_classes import Arguments
748+
from astroid.scoped_nodes import FunctionDef
749+
750+
args = Arguments()
751+
args.postinit(
752+
args=[],
753+
defaults=[],
754+
kwonlyargs=[],
755+
kw_defaults=[],
756+
annotations=[],
757+
posonlyargs=[],
758+
posonlyargs_annotations=[],
759+
kwonlyargs_annotations=[],
760+
)
761+
762+
function = FunctionDef(name=name, parent=self._instance)
763+
764+
function.postinit(args=args, body=[])
765+
return function
766+
746767
@property
747768
def attr_fget(self):
748769
from astroid.scoped_nodes import FunctionDef
@@ -767,20 +788,14 @@ def infer_call_result(self, caller=None, context=None):
767788

768789
@property
769790
def attr_setter(self):
770-
from astroid.scoped_nodes import FunctionDef
771-
772-
return FunctionDef(name="setter", parent=self._instance)
791+
return self._init_function("setter")
773792

774793
@property
775794
def attr_deleter(self):
776-
from astroid.scoped_nodes import FunctionDef
777-
778-
return FunctionDef(name="deleter", parent=self._instance)
795+
return self._init_function("deleter")
779796

780797
@property
781798
def attr_getter(self):
782-
from astroid.scoped_nodes import FunctionDef
783-
784-
return FunctionDef(name="getter", parent=self._instance)
799+
return self._init_function("getter")
785800

786801
# pylint: enable=import-outside-toplevel

tests/unittest_inference.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5846,5 +5846,22 @@ def test(self):
58465846
assert len(test) == 2
58475847

58485848

5849+
def test_infer_generated_setter():
5850+
code = """
5851+
class A:
5852+
@property
5853+
def test(self):
5854+
pass
5855+
A.test.setter
5856+
"""
5857+
node = extract_node(code)
5858+
inferred = next(node.infer())
5859+
assert isinstance(inferred, nodes.FunctionDef)
5860+
assert isinstance(inferred.args, nodes.Arguments)
5861+
# This line used to crash because property generated functions
5862+
# did not have args properly set
5863+
assert list(inferred.nodes_of_class(nodes.Const)) == []
5864+
5865+
58495866
if __name__ == "__main__":
58505867
unittest.main()

0 commit comments

Comments
 (0)