diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2020-05-28 08:49:13 +0200 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2020-05-28 08:49:44 +0200 |
| commit | 340649c52488c858c592a17f680992efe16dc41b (patch) | |
| tree | df88baf51fa90d23e718e117973acc4d1ce72f1d | |
| parent | b339a0ee17c15f7be23dee0c981edf64f0e65f33 (diff) | |
| download | astroid-git-340649c52488c858c592a17f680992efe16dc41b.tar.gz | |
Properly construct the arguments of infered property descriptors (#796)
Close PyCQA/pylint#3648
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | astroid/interpreter/objectmodel.py | 33 | ||||
| -rw-r--r-- | tests/unittest_inference.py | 17 |
3 files changed, 45 insertions, 9 deletions
@@ -24,6 +24,10 @@ Release Date: TBA Close PyCQA/pylint#3519 +* Properly construct the arguments of infered property descriptors + + Close PyCQA/pylint#3648 + What's New in astroid 2.4.1? ============================ diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index 55665a4a..277c8250 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -743,6 +743,27 @@ class PropertyModel(ObjectModel): """Model for a builtin property""" # pylint: disable=import-outside-toplevel + def _init_function(self, name): + from astroid.node_classes import Arguments + from astroid.scoped_nodes import FunctionDef + + args = Arguments() + args.postinit( + args=[], + defaults=[], + kwonlyargs=[], + kw_defaults=[], + annotations=[], + posonlyargs=[], + posonlyargs_annotations=[], + kwonlyargs_annotations=[], + ) + + function = FunctionDef(name=name, parent=self._instance) + + function.postinit(args=args, body=[]) + return function + @property def attr_fget(self): from astroid.scoped_nodes import FunctionDef @@ -767,20 +788,14 @@ class PropertyModel(ObjectModel): @property def attr_setter(self): - from astroid.scoped_nodes import FunctionDef - - return FunctionDef(name="setter", parent=self._instance) + return self._init_function("setter") @property def attr_deleter(self): - from astroid.scoped_nodes import FunctionDef - - return FunctionDef(name="deleter", parent=self._instance) + return self._init_function("deleter") @property def attr_getter(self): - from astroid.scoped_nodes import FunctionDef - - return FunctionDef(name="getter", parent=self._instance) + return self._init_function("getter") # pylint: enable=import-outside-toplevel diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index cfbcd6f8..d99298bc 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -5846,5 +5846,22 @@ def test_super_inference_of_abstract_property(): assert len(test) == 2 +def test_infer_generated_setter(): + code = """ + class A: + @property + def test(self): + pass + A.test.setter + """ + node = extract_node(code) + inferred = next(node.infer()) + assert isinstance(inferred, nodes.FunctionDef) + assert isinstance(inferred.args, nodes.Arguments) + # This line used to crash because property generated functions + # did not have args properly set + assert list(inferred.nodes_of_class(nodes.Const)) == [] + + if __name__ == "__main__": unittest.main() |
