Skip to content

Commit e63db96

Browse files
authored
Using argname to reimplement nameof (#44)
* Use argname to implement nameof * Allow variable positional arguments (*args) for argname * Update changelog * Parse the arguments only if needed * Update readme
1 parent bfad7f6 commit e63db96

15 files changed

+507
-201
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ debug(a, b)
315315
debug(a, b, merge=True)
316316
# DEBUG: a='value', b=<object object at 0x2b70580e5f20>
317317
debug(a, repr=False, prefix='') # a=value
318+
# also debug an expression
319+
debug(a+a) # DEBUG: a+a='valuevalue'
320+
# If you want to disable it:
321+
debug(a+a, vars_only=True) # error
318322
```
319323

320324
## Reliability and limitations
@@ -331,15 +335,13 @@ For example:
331335
- This will not work with `pytest`:
332336
```python
333337
a = 1
334-
assert nameof(a) == 'a'
338+
assert nameof(a) == 'a' # pytest manipulated the ast here
335339

336340
# do this instead
337341
name_a = nameof(a)
338342
assert name_a == 'a'
339343
```
340344

341-
- `R` with `reticulate`.
342-
343345
[1]: https://github.com/pwwang/python-varname
344346
[2]: https://github.com/HanyuuLu
345347
[3]: https://img.shields.io/pypi/v/varname?style=flat-square

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ Debugging with ``debug``
388388
debug(a, b, merge=True)
389389
# DEBUG: a='value', b=<object object at 0x2b70580e5f20>
390390
debug(a, repr=False, prefix='') # a=value
391+
# also debug an expression
392+
debug(a+a, vars_only=False) # DEBUG: a+a='valuevalue'
391393
392394
Reliability and limitations
393395
---------------------------

docs/CHANGELOG.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,71 @@
1+
## v0.6.2
2+
- Remove argument `full` for `nameof`, use `vars_only` instead. When `vars_only=False`, source of the argument returned.
3+
```python
4+
# before:
5+
nameof(a.b, full=True) # 'a.b'
6+
nameof(x[0], full=True) # unable to fetch
7+
# after (requires asttoken):
8+
nameof(a.b, vars_only=False) # 'a.b'
9+
nameof(x[0], vars_only=False) # 'x[0]'
10+
```
11+
- Add argument `frame` to `argname`, so that it can be wrapped.
12+
```python
13+
def argname2(arg, *more_args):
14+
return argname(arg, *more_args, frame=2)
15+
```
16+
- Allow `argname` to fetch the source of variable keyword arguments (`**kwargs`), which will be an empty dict (`{}`) when no keyword arguments passed.
17+
```python
18+
def func(a, **kwargs):
19+
return argname(a, kwargs)
20+
# before:
21+
func(x) # raises error
22+
# after:
23+
func(x) # returns ('x', {})
24+
```
25+
- Add argument `pos_only` to `argname` to only match the positional arguments
26+
```python
27+
# before
28+
def func(a, b=1):
29+
return argname(a)
30+
func(x) # 'x'
31+
func(x, b=2) # error since 2 is not ast.Name
32+
33+
# after
34+
def func(a, b=1):
35+
return argname(a, pos_only=True)
36+
func(x) # 'x'
37+
func(x, b=2) # 'x'
38+
```
39+
- Parse the arguments only if needed
40+
```python
41+
# before
42+
def func(a, b):
43+
return argname(a)
44+
func(x, 1) # NonVariableArgumentError
45+
46+
# after
47+
func(x, 1) # 'x'
48+
```
49+
- Allow variable positional arguments for `argname` so that `argname(*args)` is allowed
50+
```python
51+
# before
52+
def func(arg, *args):
53+
return argname(arg, args) # *args not allowed
54+
x = y = 1
55+
func(x, y) # ('x', ('y', 1))
56+
57+
# after
58+
def func(arg, *args):
59+
return argname(arg, *args)
60+
x = y = 1
61+
func(x, y) # ('x', 'y')
62+
```
63+
- Add `vars_only` (defaults to `False`) argument to `helpers.debug` so source of expression becomes available
64+
```python
65+
a=1
66+
debug(a+a) # DEBUG: a+a=2
67+
```
68+
169
## v0.6.1
270
- Add `argname` to retrieve argument names/sources passed to a function
371

0 commit comments

Comments
 (0)