@@ -140,17 +140,8 @@ def __get_keyword(self, keyword_name):
140140 def __get_typing_hints (self , method ):
141141 if PY2 :
142142 return {}
143- try :
144- hints = typing .get_type_hints (method )
145- except Exception :
146- hints = method .__annotations__
147- hints .pop ('return' , None )
148143 spec = ArgumentSpec .from_function (method )
149- for arg in hints .copy ():
150- # Drop self
151- if arg not in spec .positional and arg not in spec .kwonlyargs :
152- hints .pop (arg )
153- return hints
144+ return spec .get_typing_hints ()
154145
155146 def __join_defaults_with_types (self , method , types ):
156147 spec = ArgumentSpec .from_function (method )
@@ -199,6 +190,8 @@ def __init__(self):
199190
200191class ArgumentSpec (object ):
201192
193+ _function = None
194+
202195 def __init__ (self , positional = None , defaults = None , varargs = None , kwonlyargs = None ,
203196 kwonlydefaults = None , kwargs = None ):
204197 self .positional = positional or []
@@ -208,6 +201,17 @@ def __init__(self, positional=None, defaults=None, varargs=None, kwonlyargs=None
208201 self .kwonlydefaults = kwonlydefaults or []
209202 self .kwargs = kwargs
210203
204+ def __contains__ (self , item ):
205+ if item in self .positional :
206+ return True
207+ if self .varargs and item in self .varargs :
208+ return True
209+ if item in self .kwonlyargs :
210+ return True
211+ if self .kwargs and item in self .kwargs :
212+ return True
213+ return False
214+
211215 def get_arguments (self ):
212216 args = self ._format_positional (self .positional , self .defaults )
213217 args += self ._format_default (self .defaults )
@@ -219,6 +223,23 @@ def get_arguments(self):
219223 args .append ('**%s' % self .kwargs )
220224 return args
221225
226+ def get_typing_hints (self ):
227+ try :
228+ hints = typing .get_type_hints (self ._function )
229+ except Exception :
230+ hints = self ._function .__annotations__
231+ for arg in list (hints ):
232+ # remove return and self statements
233+ if arg not in self :
234+ hints .pop (arg )
235+ if self .varargs and arg in self .varargs :
236+ hints ['*%s' % arg ] = hints [arg ]
237+ del hints [arg ]
238+ if self .kwargs and arg in self .kwargs :
239+ hints ['**%s' % arg ] = hints [arg ]
240+ del hints [arg ]
241+ return hints
242+
222243 def _format_positional (self , positional , defaults ):
223244 for argument , _ in defaults :
224245 positional .remove (argument )
@@ -231,6 +252,7 @@ def _format_default(self, defaults):
231252
232253 @classmethod
233254 def from_function (cls , function ):
255+ cls ._function = function
234256 if PY2 :
235257 spec = inspect .getargspec (function )
236258 else :
0 commit comments