diff options
author | hippo91 <guillaume.peillex@gmail.com> | 2021-02-07 16:13:15 +0100 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-02-10 19:30:13 +0100 |
commit | 61a0347315951e48fd18e8f18690af79017c6fe8 (patch) | |
tree | f66b231720b4c7d828addef41033d96c11378245 | |
parent | 86f9fdc583cd7b70b676dc5774381308cb5808ab (diff) | |
download | astroid-git-61a0347315951e48fd18e8f18690af79017c6fe8.tar.gz |
In the attr_fset method, retrieve the FunctionDef corresponding to setter property and then infer the result of a call to this function
-rw-r--r-- | astroid/interpreter/objectmodel.py | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index eb2de21b..f1c25baf 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -32,11 +32,15 @@ import pprint import os import types from functools import lru_cache +from typing import Optional import astroid from astroid import context as contextmod from astroid import exceptions from astroid import node_classes +from astroid import util +# Prevents circular imports +objects = util.lazy_import("objects") IMPL_PREFIX = "attr_" @@ -800,6 +804,16 @@ class PropertyModel(ObjectModel): from astroid.node_classes import Arguments, AssignName func = self._instance + + def find_setter(func: objects.Property) -> Optional[astroid.FunctionDef]: + for target in func.parent.get_children(): + if target.name == func.function.name: + for dec_name in target.decoratornames(): + if dec_name.endswith(func.function.name + ".setter"): + return target + return None + + func_setter = find_setter(func) class PropertyFuncAccessor(FunctionDef): def infer_call_result(self, caller=None, context=None): nonlocal func @@ -808,23 +822,16 @@ class PropertyModel(ObjectModel): "fset() needs two arguments", target=self, context=context ) - yield from func.function.infer_call_result( + func_setter = find_setter(func) + if not func_setter: + raise exceptions.InferenceError( + f"Unable to find the setter of property {func.function.name}") + yield from func_setter.infer_call_result( caller=caller, context=context ) property_accessor = PropertyFuncAccessor(name="fset", parent=self._instance) - l_args = Arguments() - l_args.postinit( - args=[AssignName(name="self"), AssignName(name="value")], - defaults=[], - kwonlyargs=[], - kw_defaults=[], - annotations=[], - posonlyargs=[], - posonlyargs_annotations=[], - kwonlyargs_annotations=[], - ) - property_accessor.postinit(args=l_args, body=func.body) + property_accessor.postinit(args=func_setter.args, body=func_setter.body) return property_accessor @property |