Skip to content

Commit

Permalink
Fix inference regression with property setters
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Sep 19, 2024
1 parent 709f991 commit 55c6199
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ What's New in astroid 3.3.3?
============================
Release date: TBA

* Fix inference regression with property setters

Closes pylint-dev/pylint#9811


What's New in astroid 3.3.2?
Expand Down
12 changes: 11 additions & 1 deletion astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2487,6 +2487,16 @@ def igetattr(
if attr.parent and attr.parent.scope() == first_scope
]
functions = [attr for attr in attributes if isinstance(attr, FunctionDef)]
setter = None
for function in functions:
dec_names = function.decoratornames(context=context)
for dec_name in dec_names:
if dec_name is util.Uninferable:
continue
if dec_name.split(".")[-1] == "setter":
setter = function
if setter:
break
if functions:
# Prefer only the last function, unless a property is involved.
last_function = functions[-1]
Expand All @@ -2510,7 +2520,7 @@ def igetattr(
elif isinstance(inferred, objects.Property):
function = inferred.function
if not class_context:
if not context.callcontext:
if not context.callcontext and not setter:
context.callcontext = CallContext(
args=function.args.arguments, callee=function
)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -4416,6 +4416,23 @@ def func():
inferred = list(node.inferred())
assert [const.value for const in inferred] == [42, False]

def test_infer_property_setter(self) -> None:
node = extract_node(
"""
class PropertyWithSetter:
@property
def host(self):
return self._host
@host.setter
def host(self, value: str):
self._host = value
PropertyWithSetter().host #@
"""
)
assert not isinstance(next(node.infer()), Instance)

def test_delayed_attributes_without_slots(self) -> None:
ast_node = extract_node(
"""
Expand Down

0 comments on commit 55c6199

Please sign in to comment.