-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcore.py
409 lines (352 loc) · 14.7 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""Provide core features for varname"""
from __future__ import annotations
import ast
import re
import warnings
from typing import List, Union, Tuple, Type, Callable, overload
from executing import Source
from .utils import (
get_node,
get_node_by_frame,
lookfor_parent_assign,
node_name,
get_argument_sources,
get_function_called_argname,
rich_exc_message,
reconstruct_func_node,
ArgSourceType,
VarnameRetrievingError,
ImproperUseError,
MultiTargetAssignmentWarning,
)
from .ignore import IgnoreList, IgnoreType
def varname(
frame: int = 1,
ignore: IgnoreType = None,
multi_vars: bool = False,
raise_exc: bool = True,
strict: bool = True,
) -> Union[str, Tuple[Union[str, Tuple], ...]]:
"""Get the name of the variable(s) that assigned by function call or
class instantiation.
To debug and specify the right frame and ignore arguments, you can set
debug on and see how the frames are ignored or selected:
>>> from varname import config
>>> config.debug = True
Args:
frame: `N`th frame used to retrieve the variable name. This means
`N-1` intermediate frames will be skipped. Note that the frames
match `ignore` will not be counted. See `ignore` for details.
ignore: Frames to be ignored in order to reach the `N`th frame.
These frames will not be counted to skip within that `N-1` frames.
You can specify:
- A module (or filename of a module). Any calls from it and its
submodules will be ignored.
- A function. If it looks like it might be a decorated function,
a `MaybeDecoratedFunctionWarning` will be shown.
- Tuple of a function and a number of additional frames that should
be skipped just before reaching this function in the stack.
This is typically used for functions that have been decorated
with a 'classic' decorator that replaces the function with
a wrapper. In that case each such decorator involved should
be counted in the number that's the second element of the tuple.
- Tuple of a module (or filename) and qualified name (qualname).
You can use Unix shell-style wildcards to match the qualname.
Otherwise the qualname must appear exactly once in the
module/file.
By default, all calls from `varname` package, python standard
libraries and lambda functions are ignored.
multi_vars: Whether allow multiple variables on left-hand side (LHS).
If `True`, this function returns a tuple of the variable names,
even there is only one variable on LHS.
If `False`, and multiple variables on LHS, a
`ImproperUseError` will be raised.
raise_exc: Whether we should raise an exception if failed
to retrieve the ast node.
Note that set this to `False` will NOT supress the exception when
the use of `varname` is improper (i.e. multiple variables on
LHS with `multi_vars` is `False`). See `Raises/ImproperUseError`.
strict: Whether to only return the variable name(s) if the result of
the call is assigned to it/them directly. For example, `a = func()`
rather than `a = [func()]`
Returns:
The variable name, or `None` when `raise_exc` is `False` and
we failed to retrieve the ast node for the variable(s).
A tuple or a hierarchy (tuple of tuples) of variable names
when `multi_vars` is `True`.
Raises:
VarnameRetrievingError: When we are unable to retrieve the ast node
for the variable(s) and `raise_exc` is set to `True`.
ImproperUseError: When the use of `varname()` is improper, including:
- When LHS is not an `ast.Name` or `ast.Attribute` node or not a
list/tuple of them
- When there are multiple variables on LHS but `multi_vars` is False
- When `strict` is True, but the result is not assigned to
variable(s) directly
Note that `raise_exc=False` will NOT suppress this exception.
MultiTargetAssignmentWarning: When there are multiple target
in the assign node. (e.g: `a = b = func()`, in such a case,
`a == 'b'`, may not be the case you want)
"""
# Skip one more frame, as it is supposed to be called
# inside another function
refnode = get_node(frame + 1, ignore, raise_exc=raise_exc)
if not refnode:
if raise_exc:
raise VarnameRetrievingError("Unable to retrieve the ast node.")
return None
node = lookfor_parent_assign(refnode, strict=strict)
if not node: # improper use
if strict:
msg = "Caller doesn't assign the result directly to variable(s)."
else:
msg = "Expression is not part of an assignment."
raise ImproperUseError(rich_exc_message(msg, refnode))
if isinstance(node, ast.Assign):
# Need to actually check that there's just one
# give warnings if: a = b = func()
if len(node.targets) > 1:
warnings.warn(
"Multiple targets in assignment, variable name "
"on the very right is used. ",
MultiTargetAssignmentWarning,
)
target = node.targets[-1]
else:
target = node.target
names = node_name(target)
if not isinstance(names, tuple):
names = (names,)
if multi_vars:
return names
if len(names) > 1:
raise ImproperUseError(
rich_exc_message(
"Expect a single variable on left-hand side, "
f"got {len(names)}.",
refnode,
)
)
return names[0]
def will(frame: int = 1, raise_exc: bool = True) -> str:
"""Detect the attribute name right immediately after a function call.
Examples:
>>> class AwesomeClass:
>>> def __init__(self):
>>> self.will = None
>>> def permit(self):
>>> self.will = will()
>>> if self.will == 'do':
>>> # let self handle do
>>> return self
>>> raise AttributeError(
>>> 'Should do something with AwesomeClass object'
>>> )
>>> def do(self):
>>> if self.will != 'do':
>>> raise AttributeError("You don't have permission to do")
>>> return 'I am doing!'
>>> awesome = AwesomeClass()
>>> # AttributeError: You don't have permission to do
>>> awesome.do()
>>> # AttributeError: Should do something with AwesomeClass object
>>> awesome.permit()
>>> awesome.permit().do() == 'I am doing!'
Args:
frame: At which frame this function is called.
raise_exc: Raise exception we failed to detect the ast node
This will NOT supress the `ImproperUseError`
Returns:
The attribute name right after the function call.
`None` if ast node cannot be retrieved and `raise_exc` is `False`
Raises:
VarnameRetrievingError: When `raise_exc` is `True` and we failed to
detect the attribute name (including not having one)
ImproperUseError: When (the wraper of) this function is not called
inside a method/property of a class instance.
Note that this exception will not be suppressed by `raise_exc=False`
"""
node = get_node(frame + 1, raise_exc=raise_exc)
if not node:
if raise_exc:
raise VarnameRetrievingError("Unable to retrieve the frame.")
return None
# try to get node inst.attr from inst.attr()
node = node.parent
# see test_will_fail
if not isinstance(node, ast.Attribute):
if raise_exc:
raise ImproperUseError(
"Function `will` has to be called within "
"a method/property of a class."
)
return None
# ast.Attribute
return node.attr
@overload
def argname(
arg: str,
*,
func: Callable = None,
dispatch: Type = None,
frame: int = 1,
ignore: IgnoreType = None,
vars_only: bool = True,
) -> ArgSourceType: # pragma: no cover
...
@overload
def argname(
arg: str,
more_arg: str,
/, # introduced in python 3.8
*more_args: str,
func: Callable = None,
dispatch: Type = None,
frame: int = 1,
ignore: IgnoreType = None,
vars_only: bool = True,
) -> Tuple[ArgSourceType, ...]: # pragma: no cover
...
def argname(
arg: str,
*more_args: str,
func: Callable = None,
dispatch: Type = None,
frame: int = 1,
ignore: IgnoreType = None,
vars_only: bool = True,
) -> Union[ArgSourceType, Tuple[ArgSourceType, ...]]:
"""Get the names/sources of arguments passed to a function.
Instead of passing the argument variables themselves to this function
(like `argname()` does), you should pass their names instead.
Args:
arg: and
*more_args: The names of the arguments that you want to retrieve
names/sources of.
You can also use subscripts to get parts of the results.
>>> def func(*args, **kwargs):
>>> return argname('args[0]', 'kwargs[x]') # no quote needed
Star argument is also allowed:
>>> def func(*args, x = 1):
>>> return argname('*args', 'x')
>>> a = b = c = 1
>>> func(a, b, x=c) # ('a', 'b', 'c')
Note the difference:
>>> def func(*args, x = 1):
>>> return argname('args', 'x')
>>> a = b = c = 1
>>> func(a, b, x=c) # (('a', 'b'), 'c')
func: The target function. If not provided, the AST node of the
function call will be used to fetch the function:
- If a variable (ast.Name) used as function, the `node.id` will
be used to get the function from `locals()` or `globals()`.
- If variable (ast.Name), attributes (ast.Attribute),
subscripts (ast.Subscript), and combinations of those and
literals used as function, `pure_eval` will be used to evaluate
the node
- If `pure_eval` is not installed or failed to evaluate, `eval`
will be used. A warning will be shown since unwanted side
effects may happen in this case.
You are very encouraged to always pass the function explicitly.
dispatch: If a function is a single-dispatched function, you can
specify a type for it to dispatch the real function. If this is
specified, expect `func` to be the generic function if provided.
frame: The frame where target function is called from this call.
Calls from python standard libraries are ignored.
ignore: The intermediate calls to be ignored. See `varname.ignore`
vars_only: Require the arguments to be variables only.
If False, `asttokens` is required to retrieve the source.
Returns:
The argument source when no more_args passed, otherwise a tuple of
argument sources
Note that when an argument is an `ast.Constant`, `repr(arg.value)`
is returned, so `argname()` return `'a'` for `func("a")`
Raises:
VarnameRetrievingError: When the ast node where the function is called
cannot be retrieved
ImproperUseError: When frame or func is incorrectly specified.
"""
ignore_list = IgnoreList.create(
ignore,
ignore_lambda=False,
ignore_varname=False,
)
# where func(...) is called, skip the argname() call
func_frame = ignore_list.get_frame(frame + 1)
func_node = get_node_by_frame(func_frame)
# Only do it when func_node are available
if not func_node:
# We can do something at bytecode level, when a single positional
# argument passed to both functions (argname and the target function)
# However, it's hard to ensure that there is only a single positional
# arguments passed to the target function, at bytecode level.
raise VarnameRetrievingError(
"Cannot retrieve the node where the function is called."
)
func_node = reconstruct_func_node(func_node)
if not func:
func = get_function_called_argname(func_frame, func_node)
if dispatch:
func = func.dispatch(dispatch)
# don't pass the target arguments so that we can cache the sources in
# the same call. For example:
# >>> def func(a, b):
# >>> a_name = argname(a)
# >>> b_name = argname(b)
try:
argument_sources = get_argument_sources(
Source.for_frame(func_frame),
func_node,
func,
vars_only=vars_only,
)
except Exception as err:
raise ImproperUseError(
"Have you specified the right `frame` or `func`?"
) from err
out: List[ArgSourceType] = []
farg_star = False
for farg in (arg, *more_args):
farg_name = farg
farg_subscript = None # type: str | int
match = re.match(r"^([\w_]+)\[(.+)\]$", farg)
if match:
farg_name = match.group(1)
farg_subscript = match.group(2)
if farg_subscript.isdigit():
farg_subscript = int(farg_subscript)
else:
match = re.match(r"^\*([\w_]+)$", farg)
if match:
farg_name = match.group(1)
farg_star = True
if farg_name not in argument_sources:
raise ImproperUseError(
f"{farg_name!r} is not a valid argument "
f"of {func.__qualname__!r}."
)
source = argument_sources[farg_name]
if isinstance(source, ast.AST):
raise ImproperUseError(
f"Argument {ast.dump(source)} is not a variable "
"or an attribute."
)
if isinstance(farg_subscript, int) and not isinstance(source, tuple):
raise ImproperUseError(
f"`{farg_name}` is not a positional argument."
)
if isinstance(farg_subscript, str) and not isinstance(source, dict):
raise ImproperUseError(
f"`{farg_name}` is not a keyword argument."
)
if farg_subscript is not None:
out.append(source[farg_subscript]) # type: ignore
elif farg_star:
out.extend(source)
else:
out.append(source)
return (
out[0]
if not more_args and not farg_star
else tuple(out) # type: ignore
)