3232import os
3333import types
3434from functools import lru_cache
35+ from typing import Optional
3536
3637import astroid
3738from astroid import context as contextmod
3839from astroid import exceptions
3940from astroid import node_classes
41+ from astroid import util
42+ # Prevents circular imports
43+ objects = util .lazy_import ("objects" )
4044
4145
4246IMPL_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