@@ -56,6 +56,16 @@ def _validate_deprecation_args(date, instructions):
5656 raise ValueError ('Don\' t deprecate things without conversion instructions!' )
5757
5858
59+ def _call_location (level = 2 ):
60+ """Returns call location given level up from current call."""
61+ stack = inspect .stack ()
62+ # Check that stack has enough elements.
63+ if len (stack ) > level :
64+ location = stack [level ]
65+ return '%s:%d in %s.' % (location [1 ], location [2 ], location [3 ])
66+ return '<unknown>'
67+
68+
5969def deprecated (date , instructions ):
6070 """Decorator for marking functions or methods deprecated.
6171
@@ -92,10 +102,11 @@ def deprecated_wrapper(func):
92102 @functools .wraps (func )
93103 def new_func (* args , ** kwargs ):
94104 logging .warning (
95- '%s (from %s) is deprecated and will be removed after %s.\n '
105+ 'From %s: %s (from %s) is deprecated and will be removed '
106+ 'after %s.\n '
96107 'Instructions for updating:\n %s' ,
97- decorator_utils .get_qualified_name (func ), func . __module__ , date ,
98- instructions )
108+ _call_location (), decorator_utils .get_qualified_name (func ),
109+ func . __module__ , date , instructions )
99110 return func (* args , ** kwargs )
100111 new_func .__doc__ = _add_deprecated_function_notice_to_docstring (
101112 func .__doc__ , date , instructions )
@@ -154,7 +165,8 @@ def deprecated_wrapper(func):
154165 missing_args = [arg_name for arg_name in deprecated_arg_names
155166 if arg_name not in known_args ]
156167 raise ValueError ('The following deprecated arguments are not present '
157- 'in the function signature: %s' % missing_args )
168+ 'in the function signature: %s. '
169+ 'Found next arguments: %s.' % (missing_args , known_args ))
158170
159171 @functools .wraps (func )
160172 def new_func (* args , ** kwargs ):
@@ -172,10 +184,10 @@ def new_func(*args, **kwargs):
172184 invalid_args .append (arg_name )
173185 for arg_name in invalid_args :
174186 logging .warning (
175- 'Calling %s (from %s) with %s is deprecated and will be removed '
176- 'after %s.\n Instructions for updating:\n %s' ,
177- decorator_utils .get_qualified_name (func ), func . __module__ ,
178- arg_name , date , instructions )
187+ 'From %s: calling %s (from %s) with %s is deprecated and will '
188+ 'be removed after %s.\n Instructions for updating:\n %s' ,
189+ _call_location (), decorator_utils .get_qualified_name (func ),
190+ func . __module__ , arg_name , date , instructions )
179191 return func (* args , ** kwargs )
180192 new_func .__doc__ = _add_deprecated_arg_notice_to_docstring (
181193 func .__doc__ , date , instructions )
@@ -226,10 +238,10 @@ def new_func(*args, **kwargs):
226238 for arg_name , arg_value in deprecated_kwargs .items ():
227239 if arg_name in named_args and named_args [arg_name ] == arg_value :
228240 logging .warning (
229- 'Calling %s (from %s) with %s=%s is deprecated and will be '
230- 'removed after %s.\n Instructions for updating:\n %s' ,
231- decorator_utils .get_qualified_name (func ), func . __module__ ,
232- arg_name , arg_value , date , instructions )
241+ 'From %s: calling %s (from %s) with %s=%s is deprecated and will '
242+ 'be removed after %s.\n Instructions for updating:\n %s' ,
243+ _call_location (), decorator_utils .get_qualified_name (func ),
244+ func . __module__ , arg_name , arg_value , date , instructions )
233245 return func (* args , ** kwargs )
234246 new_func .__doc__ = _add_deprecated_arg_notice_to_docstring (
235247 func .__doc__ , date , instructions )
0 commit comments