Skip to content

Commit 55c6199

Browse files
Fix inference regression with property setters
Closes pylint-dev/pylint#9811
1 parent 709f991 commit 55c6199

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ What's New in astroid 3.3.3?
1313
============================
1414
Release date: TBA
1515

16+
* Fix inference regression with property setters
17+
18+
Closes pylint-dev/pylint#9811
1619

1720

1821
What's New in astroid 3.3.2?

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2487,6 +2487,16 @@ def igetattr(
24872487
if attr.parent and attr.parent.scope() == first_scope
24882488
]
24892489
functions = [attr for attr in attributes if isinstance(attr, FunctionDef)]
2490+
setter = None
2491+
for function in functions:
2492+
dec_names = function.decoratornames(context=context)
2493+
for dec_name in dec_names:
2494+
if dec_name is util.Uninferable:
2495+
continue
2496+
if dec_name.split(".")[-1] == "setter":
2497+
setter = function
2498+
if setter:
2499+
break
24902500
if functions:
24912501
# Prefer only the last function, unless a property is involved.
24922502
last_function = functions[-1]
@@ -2510,7 +2520,7 @@ def igetattr(
25102520
elif isinstance(inferred, objects.Property):
25112521
function = inferred.function
25122522
if not class_context:
2513-
if not context.callcontext:
2523+
if not context.callcontext and not setter:
25142524
context.callcontext = CallContext(
25152525
args=function.args.arguments, callee=function
25162526
)

tests/test_inference.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4416,6 +4416,23 @@ def func():
44164416
inferred = list(node.inferred())
44174417
assert [const.value for const in inferred] == [42, False]
44184418

4419+
def test_infer_property_setter(self) -> None:
4420+
node = extract_node(
4421+
"""
4422+
class PropertyWithSetter:
4423+
@property
4424+
def host(self):
4425+
return self._host
4426+
4427+
@host.setter
4428+
def host(self, value: str):
4429+
self._host = value
4430+
4431+
PropertyWithSetter().host #@
4432+
"""
4433+
)
4434+
assert not isinstance(next(node.infer()), Instance)
4435+
44194436
def test_delayed_attributes_without_slots(self) -> None:
44204437
ast_node = extract_node(
44214438
"""

0 commit comments

Comments
 (0)