Skip to content

Commit 28f8624

Browse files
authored
Add argname to retrieve argument names/sources passed to a function (#42)
* Add argname to get names/sources of arguments * Update docs for argname * Fix linting * Fix tests * Support individual access to positional and keyword arguments for argname * Update docs * Update changelog * Add pure_eval to evalute the function that calls argname * Take suggestions by Alex
1 parent de05e95 commit 28f8624

File tree

13 files changed

+781
-232
lines changed

13 files changed

+781
-232
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pip install -U varname
2020
- Retrieving names of variables a function/class call is assigned to from inside it, using `varname`.
2121
- Retrieving variable names directly, using `nameof`
2222
- Detecting next immediate attribute name, using `will`
23+
- Fetching argument names/sources passed to a function using `argname`
2324

2425
- Other helper APIs (built based on core features):
2526

@@ -256,6 +257,31 @@ awesome.permit() # AttributeError: Should do something with AwesomeClass object
256257
awesome.permit().do() == 'I am doing!'
257258
```
258259

260+
### Fetching argument names/sources using `argname`
261+
```python
262+
from varname import argname
263+
264+
def func(a, b=1):
265+
print(argname(a))
266+
267+
x = y = z = 2
268+
func(x) # prints: x
269+
270+
def func2(a, b=1):
271+
print(argname(a, b))
272+
func2(y, b=x) # prints: ('y', 'x')
273+
274+
# allow expressions
275+
def func3(a, b=1):
276+
print(argname(a, b, vars_only=False))
277+
func3(x+y, y+x) # prints: ('x+y', 'y+x')
278+
279+
# positional and keyword arguments
280+
def func4(*args, **kwargs):
281+
print(argname(args[1], kwargs['c']))
282+
func4(y, x, c=z) # prints: ('x', 'z')
283+
```
284+
259285
### Value wrapper
260286

261287
```python

README.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Features
6363
* Retrieving names of variables a function/class call is assigned to from inside it, using ``varname``.
6464
* Retrieving variable names directly, using ``nameof``
6565
* Detecting next immediate attribute name, using ``will``
66+
* Fetching argument names/sources passed to a function using ``argname``
6667

6768
*
6869
Other helper APIs (built based on core features):
@@ -323,6 +324,33 @@ Detecting next immediate attribute name
323324
awesome.permit() # AttributeError: Should do something with AwesomeClass object
324325
awesome.permit().do() == 'I am doing!'
325326
327+
Fetching argument names/sources using ``argname``
328+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
329+
330+
.. code-block:: python
331+
332+
from varname import argname
333+
334+
def func(a, b=1):
335+
print(argname(a))
336+
337+
x = y = z = 2
338+
func(x) # prints: x
339+
340+
def func2(a, b=1):
341+
print(argname(a, b))
342+
func2(y, b=x) # prints: ('y', 'x')
343+
344+
# allow expressions
345+
def func3(a, b=1):
346+
print(argname(a, b, vars_only=False))
347+
func3(x+y, y+x) # prints: ('x+y', 'y+x')
348+
349+
# positional and keyword arguments
350+
def func4(*args, **kwargs):
351+
print(argname(args[1], kwargs['c']))
352+
func4(y, x, c=z) # prints: ('x', 'z')
353+
326354
Value wrapper
327355
^^^^^^^^^^^^^
328356

docs/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.6.1
2+
- Add `argname` to retrieve argument names/sources passed to a function
3+
14
## v0.6.0
25
- Changed:
36
- `Wrapper`, `register` and `debug` moved to `varname.helpers`

0 commit comments

Comments
 (0)