Skip to content

Commit 219a18d

Browse files
authored
0.9.0 (#84)
* - Upgrade executing to 0.9 - Support `__getattr__/__setattr__` etc for `argname` - Support constants for `argname` when `vars_only=True` * Add poetry.lock to the repo * Remove deprecated `argname2` * Update playground * CI: add missing typing_extensions for mypy * CI: use mypy 0.910 * CI: Fix deps install * Update poetry.lock * Remove end_lineno and end_col_offset when reconstruct node for argname * Remove end_lineno and end_col_offset when reconstruct node for argname * Fix for lower python versions * 🔖 0.9.0 * Update README * Update README
1 parent 8ec7a86 commit 219a18d

File tree

13 files changed

+797
-135
lines changed

13 files changed

+797
-135
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ jobs:
2424
- name: Install dependencies
2525
run: |
2626
python -m pip install --upgrade pip
27-
python -m pip install flake8
28-
python -m pip install mypy
2927
python -m pip install poetry
3028
poetry config virtualenvs.create false
31-
poetry install -v
29+
poetry install -E all -v
30+
python -m pip install flake8
31+
python -m pip install mypy
3232
- name: Run mypy check
3333
run: mypy -p varname
3434
- name: Run flake8

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export/
101101
site/
102102

103103
# poetry
104-
poetry.lock
104+
# poetry.lock
105105

106106
# backup files
107107
*.bak
@@ -114,3 +114,4 @@ docs/api/
114114
.history/
115115

116116
playground/playground.nbconvert.ipynb
117+
test.py

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pip install -U varname
2828
- A decorator to register `__varname__` to functions/classes, using `register`
2929
- A `debug` function to print variables with their names and values
3030

31-
3231
## Credits
3332

3433
Thanks goes to these awesome people/projects:
@@ -308,6 +307,16 @@ def func4(*args, **kwargs):
308307
print(argname('args[1]', 'kwargs[c]'))
309308
func4(y, x, c=z) # prints: ('x', 'z')
310309

310+
311+
# As of 0.9.0 (see: https://pwwang.github.io/python-varname/CHANGELOG/#v090)
312+
# Can also fetch the source of the argument for
313+
# __getattr__/__getitem__/__setattr/__setitem__/__add__/__lt__, etc.
314+
class Foo:
315+
def __setattr__(self, name, value):
316+
print(argname("name", "value"))
317+
318+
Foo().a = 1 # prints: ("'a'", '1')
319+
311320
```
312321

313322
### Value wrapper

README.raw.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Dark magics about variable names in python
88

9-
[Change Log][16] | [API][15] | [Playground][11]
9+
[CHANGELOG][16] | [API][15] | [Playground][11] | :fire: [StackOverflow answer][20]
1010

1111
## Installation
1212
```shell
@@ -34,6 +34,12 @@ Thanks goes to these awesome people/projects:
3434

3535
<table>
3636
<tr>
37+
<td align="center" style="min-width: 75px">
38+
<a href="https://github.com/alexmojaki/executing">
39+
<img src="https://ui-avatars.com/api/?color=3333ff&background=ffffff&bold=true&name=e&size=400" width="50px;" alt=""/>
40+
<br /><sub><b>executing</b></sub>
41+
</a>
42+
</td>
3743
<td align="center" style="min-width: 75px">
3844
<a href="https://github.com/alexmojaki">
3945
<img src="https://avatars0.githubusercontent.com/u/3627481?s=400&v=4" width="50px;" alt=""/>
@@ -47,9 +53,21 @@ Thanks goes to these awesome people/projects:
4753
</a>
4854
</td>
4955
<td align="center" style="min-width: 75px">
50-
<a href="https://github.com/alexmojaki/executing">
51-
<img src="https://ui-avatars.com/api/?color=3333ff&background=ffffff&bold=true&name=e&size=400" width="50px;" alt=""/>
52-
<br /><sub><b>executing</b></sub>
56+
<a href="https://github.com/ElCuboNegro">
57+
<img src="https://avatars.githubusercontent.com/u/5524219?s=400&v=4" width="50px;" alt=""/>
58+
<br /><sub><b>@ElCuboNegro</b></sub>
59+
</a>
60+
</td>
61+
<td align="center" style="min-width: 75px">
62+
<a href="https://github.com/thewchan">
63+
<img src="https://avatars.githubusercontent.com/u/49702524?s=400&v=4" width="50px;" alt=""/>
64+
<br /><sub><b>@thewchan</b></sub>
65+
</a>
66+
</td>
67+
<td align="center" style="min-width: 75px">
68+
<a href="https://github.com/LawsOfSympathy">
69+
<img src="https://avatars.githubusercontent.com/u/96355982?s=400&v=4" width="50px;" alt=""/>
70+
<br /><sub><b>@LawsOfSympathy</b></sub>
5371
</a>
5472
</td>
5573
</tr>
@@ -285,6 +303,15 @@ func3(x+y, y+x) # prints: {_out}
285303
def func4(*args, **kwargs):
286304
print(argname('args[1]', 'kwargs[c]'))
287305
func4(y, x, c=z) # prints: {_out}
306+
307+
# As of 0.9.0 (see: https://pwwang.github.io/python-varname/CHANGELOG/#v090)
308+
# Can also fetch the source of the argument for
309+
# __getattr__/__getitem__/__setattr/__setitem__/__add__/__lt__, etc.
310+
class Foo:
311+
def __setattr__(self, name, value):
312+
print(argname("name", "value"))
313+
314+
Foo().a = 1 # prints: {_out}
288315
```
289316

290317
### Value wrapper
@@ -369,3 +396,4 @@ For example:
369396
[17]: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
370397
[18]: https://gitter.im/python-varname/community
371398
[19]: https://github.com/alexmojaki/executing#is-it-reliable
399+
[20]: https://stackoverflow.com/a/59364138/5088165

docs/CHANGELOG.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
## 0.9.0
2+
3+
- ⬆️ Upgrade executing to 0.9
4+
- 🗑️ Remove deprecated `argname2`
5+
- ✨ Support constants for `argname` even when `vars_only=True`
6+
- ✨ Support `__getattr__/__setattr__` etc for `argname`
7+
8+
Now you can do:
9+
```python
10+
from varname import argname
11+
12+
class Foo:
13+
def __getattr__(self, name):
14+
"""Similar for `__getitem__`"""
15+
print(argname("name"))
16+
17+
def __setattr__(self, name, value):
18+
"""Similar for `__setitem__`"""
19+
print(argname("name"))
20+
print(argname("value"))
21+
22+
def __add__(self, other):
23+
"""Similar for `__sub__`, `__mul__`, `__truediv__`, `__floordiv__`,
24+
`__mod__`, `__pow__`, `__lshift__`, `__rshift__`, `__matmul__`,
25+
`__and__`, `__xor__`, `__or__`
26+
"""
27+
print(argname("other"))
28+
29+
def __eq__(self, other):
30+
"""Similar for `__lt__`, `__le__`, `__gt__`, `__ge__`, `__ne__`
31+
"""
32+
print(argname("other"))
33+
34+
foo = Foo()
35+
b = 1
36+
foo.x # prints: 'x' (note the quotes)
37+
foo.x = b # prints: 'x' and b
38+
foo + b # prints: b
39+
foo == b # prints: b
40+
```
41+
142
## v0.8.3
243

344
This is more of a housekeeping release:

playground/playground.ipynb

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"from contextlib import contextmanager\n",
2424
"\n",
2525
"from varname import (\n",
26-
" varname, nameof, will, argname, argname2,\n",
26+
" varname, nameof, will, argname,\n",
2727
" config, \n",
2828
" ImproperUseError, VarnameRetrievingError, QualnameNonUniqueError\n",
2929
")\n",
@@ -369,7 +369,7 @@
369369
"text": [
370370
"ImproperUseError(Caller doesn't assign the result directly to variable(s).\n",
371371
"\n",
372-
" <ipython-input-9-aa71568f2d09>:11:12\n",
372+
" /tmp/ipykernel_415/1606438573.py:11:12\n",
373373
" | 9 @decor\n",
374374
" | 10 def func2():\n",
375375
" > | 11 return func()\n",
@@ -382,7 +382,7 @@
382382
"name": "stderr",
383383
"output_type": "stream",
384384
"text": [
385-
"/home/pwwang/github/python-varname/varname/ignore.py:171: MaybeDecoratedFunctionWarning: You asked varname to ignore function 'wrapper', which may be decorated. If it is not intended, you may need to ignore all intermediate frames with a tuple of the function and the number of its decorators.\n",
385+
"/home/pwwang/github/python-varname/varname/ignore.py:175: MaybeDecoratedFunctionWarning: You asked varname to ignore function 'wrapper', which may be decorated. If it is not intended, you may need to ignore all intermediate frames with a tuple of the function and the number of its decorators.\n",
386386
" warnings.warn(\n"
387387
]
388388
}
@@ -470,12 +470,12 @@
470470
"output_type": "stream",
471471
"text": [
472472
"[varname] DEBUG: >>> IgnoreList initiated <<<\n",
473-
"[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:103]\n",
473+
"[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:106]\n",
474474
"[varname] DEBUG: Ignored by IgnoreModule('module_all_calls') [In 'func' at /home/pwwang/github/python-varname/playground/module_all_calls.py:6]\n",
475475
"[varname] DEBUG: Ignored by IgnoreModule('module_all_calls') [In 'func2' at /home/pwwang/github/python-varname/playground/module_all_calls.py:9]\n",
476476
"[varname] DEBUG: Ignored by IgnoreModule('module_all_calls') [In 'func3' at /home/pwwang/github/python-varname/playground/module_all_calls.py:12]\n",
477-
"[varname] DEBUG: Skipping (0 more to skip) [In 'func' at <ipython-input-11-8d0a7ff1a188>:4]\n",
478-
"[varname] DEBUG: Gotcha! [In '<module>' at <ipython-input-11-8d0a7ff1a188>:7]\n"
477+
"[varname] DEBUG: Skipping (0 more to skip) [In 'func' at /tmp/ipykernel_415/3068660293.py:4]\n",
478+
"[varname] DEBUG: Gotcha! [In '<cell line: 6>' at /tmp/ipykernel_415/3068660293.py:7]\n"
479479
]
480480
},
481481
{
@@ -525,11 +525,11 @@
525525
"output_type": "stream",
526526
"text": [
527527
"[varname] DEBUG: >>> IgnoreList initiated <<<\n",
528-
"[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:103]\n",
528+
"[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:106]\n",
529529
"[varname] DEBUG: Ignored by IgnoreModuleQualname('module_glob_qualname', '_func*') [In '_func' at /home/pwwang/github/python-varname/playground/module_glob_qualname.py:6]\n",
530530
"[varname] DEBUG: Ignored by IgnoreModuleQualname('module_glob_qualname', '_func*') [In '_func2' at /home/pwwang/github/python-varname/playground/module_glob_qualname.py:9]\n",
531531
"[varname] DEBUG: Skipping (0 more to skip) [In 'func3' at /home/pwwang/github/python-varname/playground/module_glob_qualname.py:12]\n",
532-
"[varname] DEBUG: Gotcha! [In '<module>' at <ipython-input-12-07ea87f561cc>:4]\n"
532+
"[varname] DEBUG: Gotcha! [In '<cell line: 3>' at /tmp/ipykernel_415/491507787.py:4]\n"
533533
]
534534
},
535535
{
@@ -610,10 +610,10 @@
610610
"output_type": "stream",
611611
"text": [
612612
"[varname] DEBUG: >>> IgnoreList initiated <<<\n",
613-
"[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:103]\n",
614-
"[varname] DEBUG: Skipping (0 more to skip) [In 'func' at <ipython-input-14-d27bbc23b8df>:2]\n",
615-
"[varname] DEBUG: Ignored by IgnoreOnlyQualname(None, '*<lambda>') [In '<lambda>' at <ipython-input-14-d27bbc23b8df>:4]\n",
616-
"[varname] DEBUG: Gotcha! [In '<module>' at <ipython-input-14-d27bbc23b8df>:7]\n"
613+
"[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:106]\n",
614+
"[varname] DEBUG: Skipping (0 more to skip) [In 'func' at /tmp/ipykernel_415/2761136102.py:2]\n",
615+
"[varname] DEBUG: Ignored by IgnoreOnlyQualname(None, '*<lambda>') [In '<lambda>' at /tmp/ipykernel_415/2761136102.py:4]\n",
616+
"[varname] DEBUG: Gotcha! [In '<cell line: 6>' at /tmp/ipykernel_415/2761136102.py:7]\n"
617617
]
618618
},
619619
{
@@ -662,10 +662,10 @@
662662
"output_type": "stream",
663663
"text": [
664664
"[varname] DEBUG: >>> IgnoreList initiated <<<\n",
665-
"[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:103]\n",
666-
"[varname] DEBUG: Skipping (0 more to skip) [In '__init__' at <ipython-input-15-32fa2de0b542>:8]\n",
665+
"[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:106]\n",
666+
"[varname] DEBUG: Skipping (0 more to skip) [In '__init__' at /tmp/ipykernel_415/641638691.py:8]\n",
667667
"[varname] DEBUG: Ignored by IgnoreStdlib('/home/pwwang/miniconda3/lib/python3.9/') [In '__call__' at /home/pwwang/miniconda3/lib/python3.9/typing.py:670]\n",
668-
"[varname] DEBUG: Gotcha! [In '<module>' at <ipython-input-15-32fa2de0b542>:11]\n"
668+
"[varname] DEBUG: Gotcha! [In '<cell line: 10>' at /tmp/ipykernel_415/641638691.py:11]\n"
669669
]
670670
},
671671
{
@@ -805,11 +805,11 @@
805805
"output_type": "stream",
806806
"text": [
807807
"[varname] DEBUG: >>> IgnoreList initiated <<<\n",
808-
"[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:103]\n",
809-
"[varname] DEBUG: Ignored by IgnoreDecorated('wrapper', 2) [In 'func' at <ipython-input-18-e7a4675a5e8e>:2]\n",
810-
"[varname] DEBUG: Skipping (1 more to skip) [In 'wrapper' at <ipython-input-18-e7a4675a5e8e>:9]\n",
811-
"[varname] DEBUG: Skipping (0 more to skip) [In 'func3' at <ipython-input-18-e7a4675a5e8e>:18]\n",
812-
"[varname] DEBUG: Gotcha! [In '<module>' at <ipython-input-18-e7a4675a5e8e>:21]\n"
808+
"[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:106]\n",
809+
"[varname] DEBUG: Ignored by IgnoreDecorated('wrapper', 2) [In 'func' at /tmp/ipykernel_415/652967550.py:2]\n",
810+
"[varname] DEBUG: Skipping (1 more to skip) [In 'wrapper' at /tmp/ipykernel_415/652967550.py:9]\n",
811+
"[varname] DEBUG: Skipping (0 more to skip) [In 'func3' at /tmp/ipykernel_415/652967550.py:18]\n",
812+
"[varname] DEBUG: Gotcha! [In '<cell line: 20>' at /tmp/ipykernel_415/652967550.py:21]\n"
813813
]
814814
},
815815
{
@@ -988,7 +988,7 @@
988988
"name": "stderr",
989989
"output_type": "stream",
990990
"text": [
991-
"/home/pwwang/github/python-varname/varname/core.py:122: MultiTargetAssignmentWarning: Multiple targets in assignment, variable name on the very right is used. \n",
991+
"/home/pwwang/github/python-varname/varname/core.py:125: MultiTargetAssignmentWarning: Multiple targets in assignment, variable name on the very right is used. \n",
992992
" warnings.warn(\n"
993993
]
994994
}
@@ -1182,7 +1182,16 @@
11821182
"x\n",
11831183
"('y', 'x')\n",
11841184
"('x+y', 'y+x')\n",
1185-
"('x', 'z')\n"
1185+
"('x', 'z')\n",
1186+
"(\"'a'\", '1')\n"
1187+
]
1188+
},
1189+
{
1190+
"name": "stderr",
1191+
"output_type": "stream",
1192+
"text": [
1193+
"/home/pwwang/github/python-varname/varname/utils.py:418: UsingExecWarning: Cannot evaluate node Attribute(value=Call(func=Name(id='Foo', ctx=Load()), args=[], keywords=[]), attr='__setattr__', ctx=Load()) using 'pure_eval'. Using 'eval' to get the function that calls 'argname'. Try calling it using a variable reference to the function, or passing the function to 'argname' explicitly.\n",
1194+
" warnings.warn(\n"
11861195
]
11871196
}
11881197
],
@@ -1211,7 +1220,15 @@
12111220
" # print(argname(args[1], kwargs['c'])) \n",
12121221
" print(argname('args[1]', 'kwargs[c]')) \n",
12131222
"func4(y, x, c=z)\n",
1214-
"\n"
1223+
"\n",
1224+
"# As of 0.9.0\n",
1225+
"# Can also fetch the source of the argument for\n",
1226+
"# __getattr__/__getitem__/__setattr/__setitem__/__add__/__lt__, etc.\n",
1227+
"class Foo:\n",
1228+
" def __setattr__(self, name, value):\n",
1229+
" print(argname(\"name\", \"value\"))\n",
1230+
"\n",
1231+
"Foo().a = 1 # prints: {_out}"
12151232
]
12161233
},
12171234
{
@@ -1235,7 +1252,7 @@
12351252
}
12361253
],
12371254
"source": [
1238-
"# It is easier to wrap argname2\n",
1255+
"# It is easier to wrap argname\n",
12391256
"# You don't have to use the exact signature\n",
12401257
"def argname3(*args):\n",
12411258
" return argname(*args, frame=2)\n",
@@ -1477,11 +1494,9 @@
14771494
}
14781495
],
14791496
"metadata": {
1480-
"interpreter": {
1481-
"hash": "c4cc73b080e063fcebb9afb794613be7caf4b26129562cba1382945a18cc49cc"
1482-
},
14831497
"kernelspec": {
1484-
"display_name": "Python 3.9.5 64-bit ('base': conda)",
1498+
"display_name": "Python 3.9.5 ('base')",
1499+
"language": "python",
14851500
"name": "python3"
14861501
},
14871502
"language_info": {
@@ -1495,6 +1510,11 @@
14951510
"nbconvert_exporter": "python",
14961511
"pygments_lexer": "ipython3",
14971512
"version": "3.9.5"
1513+
},
1514+
"vscode": {
1515+
"interpreter": {
1516+
"hash": "9ed5c94d10bf621c6841991b7e31ffd0f3c8de8ec4167710459737a50edc58e4"
1517+
}
14981518
}
14991519
},
15001520
"nbformat": 4,

0 commit comments

Comments
 (0)