@@ -40,6 +40,11 @@ def instrument(f: T_CallableOrType) -> FunctionType | str:
4040 return "__module__ attribute is not set"
4141 elif f .__code__ .co_filename == "<stdin>" :
4242 return "cannot instrument functions defined in a REPL"
43+ elif hasattr (f , "__wrapped__" ):
44+ return (
45+ "@typechecked only supports instrumenting functions wrapped with "
46+ "@classmethod, @staticmethod or @property"
47+ )
4348
4449 target_path = [item for item in f .__qualname__ .split ("." ) if item != "<locals>" ]
4550 module_source = inspect .getsource (sys .modules [f .__module__ ])
@@ -162,29 +167,15 @@ def typechecked(target: T_CallableOrType | None = None) -> Any:
162167 return target
163168
164169 # Find either the first Python wrapper or the actual function
165- func : FunctionType
166170 wrapper_class : type [classmethod [Any ]] | type [staticmethod [Any ]] | None = None
167171 if isinstance (target , (classmethod , staticmethod )):
168172 wrapper_class = target .__class__
169173 target = target .__func__
170174
171- if hasattr (target , "__wrapped__" ):
172- warn (
173- f"Cannot instrument { function_name (target )} -- @typechecked only supports "
174- f"instrumenting functions wrapped with @classmethod, @staticmethod or "
175- f"@property" ,
176- InstrumentationWarning ,
177- )
178- return target
179- elif isfunction (target ):
180- func = target
181- else :
182- raise TypeError ("target is not a function or a supported wrapper" )
183-
184- retval = instrument (func )
175+ retval = instrument (target )
185176 if isinstance (retval , str ):
186177 warn (
187- f"{ retval } -- not typechecking { function_name (func )} " ,
178+ f"{ retval } -- not typechecking { function_name (target )} " ,
188179 InstrumentationWarning ,
189180 )
190181 return target
0 commit comments