Skip to content

Commit 3840ff6

Browse files
Fix inference regression with property setters (#2567) (#2568)
Closes pylint-dev/pylint#9811 (cherry picked from commit 5a93a9f)
1 parent 5eae215 commit 3840ff6

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

ChangeLog

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ 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
19+
1620
* Add annotation-only instance attributes to attrs classes to fix `no-member` false positives.
1721

1822
Closes #2514
1923

2024

21-
2225
What's New in astroid 3.3.2?
2326
============================
2427
Release date: 2024-08-11

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2506,6 +2506,16 @@ def igetattr(
25062506
if attr.parent and attr.parent.scope() == first_scope
25072507
]
25082508
functions = [attr for attr in attributes if isinstance(attr, FunctionDef)]
2509+
setter = None
2510+
for function in functions:
2511+
dec_names = function.decoratornames(context=context)
2512+
for dec_name in dec_names:
2513+
if dec_name is util.Uninferable:
2514+
continue
2515+
if dec_name.split(".")[-1] == "setter":
2516+
setter = function
2517+
if setter:
2518+
break
25092519
if functions:
25102520
# Prefer only the last function, unless a property is involved.
25112521
last_function = functions[-1]
@@ -2529,7 +2539,7 @@ def igetattr(
25292539
elif isinstance(inferred, objects.Property):
25302540
function = inferred.function
25312541
if not class_context:
2532-
if not context.callcontext:
2542+
if not context.callcontext and not setter:
25332543
context.callcontext = CallContext(
25342544
args=function.args.arguments, callee=function
25352545
)

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)