Skip to content

Commit 61a0347

Browse files
hippo91Pierre-Sassoulas
authored andcommitted
In the attr_fset method, retrieve the FunctionDef corresponding to setter property and then infer the result of a call to this function
1 parent 86f9fdc commit 61a0347

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

astroid/interpreter/objectmodel.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@
3232
import os
3333
import types
3434
from functools import lru_cache
35+
from typing import Optional
3536

3637
import astroid
3738
from astroid import context as contextmod
3839
from astroid import exceptions
3940
from astroid import node_classes
41+
from astroid import util
42+
# Prevents circular imports
43+
objects = util.lazy_import("objects")
4044

4145

4246
IMPL_PREFIX = "attr_"
@@ -800,6 +804,16 @@ def attr_fset(self):
800804
from astroid.node_classes import Arguments, AssignName
801805

802806
func = self._instance
807+
808+
def find_setter(func: objects.Property) -> Optional[astroid.FunctionDef]:
809+
for target in func.parent.get_children():
810+
if target.name == func.function.name:
811+
for dec_name in target.decoratornames():
812+
if dec_name.endswith(func.function.name + ".setter"):
813+
return target
814+
return None
815+
816+
func_setter = find_setter(func)
803817
class PropertyFuncAccessor(FunctionDef):
804818
def infer_call_result(self, caller=None, context=None):
805819
nonlocal func
@@ -808,23 +822,16 @@ def infer_call_result(self, caller=None, context=None):
808822
"fset() needs two arguments", target=self, context=context
809823
)
810824

811-
yield from func.function.infer_call_result(
825+
func_setter = find_setter(func)
826+
if not func_setter:
827+
raise exceptions.InferenceError(
828+
f"Unable to find the setter of property {func.function.name}")
829+
yield from func_setter.infer_call_result(
812830
caller=caller, context=context
813831
)
814832

815833
property_accessor = PropertyFuncAccessor(name="fset", parent=self._instance)
816-
l_args = Arguments()
817-
l_args.postinit(
818-
args=[AssignName(name="self"), AssignName(name="value")],
819-
defaults=[],
820-
kwonlyargs=[],
821-
kw_defaults=[],
822-
annotations=[],
823-
posonlyargs=[],
824-
posonlyargs_annotations=[],
825-
kwonlyargs_annotations=[],
826-
)
827-
property_accessor.postinit(args=l_args, body=func.body)
834+
property_accessor.postinit(args=func_setter.args, body=func_setter.body)
828835
return property_accessor
829836

830837
@property

0 commit comments

Comments
 (0)