Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly construct the arguments of inferred property descriptors #796

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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?
============================
Expand Down
33 changes: 24 additions & 9 deletions astroid/interpreter/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -767,20 +788,14 @@ def infer_call_result(self, caller=None, context=None):

@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
17 changes: 17 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -5846,5 +5846,22 @@ def test(self):
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()