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

Fix inference regression with property setters #2567

Merged
merged 1 commit into from
Sep 19, 2024
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
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
Loading