Skip to content

Commit 0d5238c

Browse files
committed
Ready for v0.4.0
1 parent a886277 commit 0d5238c

File tree

4 files changed

+49
-55
lines changed

4 files changed

+49
-55
lines changed

README.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,12 @@ Thanks goes to these awesome people/projects:
126126
# Since v0.1.3
127127
# We can ask varname to raise exceptions
128128
# if it fails to detect the variable name
129-
130-
from varname import VarnameRetrievingError
131-
def get_name():
132-
try:
133-
# if raise_exc is False
134-
# "var_<index>" will be returned
135-
return varname(raise_exc=True)
136-
except VarnameRetrieveingError:
137-
return None
129+
def get_name(raise_exc):
130+
return varname(raise_exc=raise_exc)
138131

139132
a = {}
140-
a['b'] = get_name() # None
133+
a['b'] = get_name(True) # VarnameRetrievingError
134+
a['b'] = get_name(False) # None
141135
```
142136

143137
### Value wrapper
@@ -189,7 +183,7 @@ class AwesomeClass:
189183
self.will = None
190184

191185
def permit(self):
192-
self.will = will()
186+
self.will = will(raise_exc=False)
193187
if self.will == 'do':
194188
# let self handle do
195189
return self
@@ -239,13 +233,18 @@ b.append(1)
239233
a == b
240234
```
241235

242-
## Limitations
236+
## Reliability and limitations
243237
`python-varname` is all depending on `executing` package to look for the node.
244-
It does not work with any environment where `executing` is not able to detect the node.
238+
The node `executing` detects is ensured to be the correct one (see [this][19]).
239+
240+
It partially works with environments where other AST magics apply, including
241+
`pytest`, `ipython`, `macropy`, `birdseye`, `reticulate` with `R`, etc. Neither
242+
`executing` nor `python-varname` is 100% working with those environments. Use
243+
it at your own risk.
244+
245245
For example:
246246

247-
- Environments where other AST magics apply. For example: `pytest`, `ipython`, `macropy`, or `birdseye`.
248-
This will not work with `pytest`:
247+
- This will not work with `pytest`:
249248
```python
250249
a = 1
251250
assert nameof(a) == 'a'
@@ -254,6 +253,13 @@ For example:
254253
name_a = nameof(a)
255254
assert name_a == 'a'
256255
```
256+
257+
- This will also typically fail with `ipython`:
258+
```python
259+
a = 1
260+
for _ in [0]:
261+
print(nameof(a))
262+
```
257263
- `R` with `reticulate`.
258264

259265
[1]: https://github.com/pwwang/python-varname
@@ -272,3 +278,4 @@ For example:
272278
[16]: https://pwwang.github.io/python-varname/CHANGELOG/
273279
[17]: https://img.shields.io/gitter/room/pwwang/python-varname?style=flat-square
274280
[18]: https://gitter.im/python-varname/community
281+
[19]: https://github.com/alexmojaki/executing#is-it-reliable

docs/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@
3232
- Drop support for pytest, don't try to find node when executing fails
3333
- Remodel `will` for better logic
3434
- Support attributes in varname and nameof (#14)
35+
36+
## v0.4.0
37+
- Change default of `raise_exc` to `True` for all related APIs
38+
- Deprecate `var_0`
39+
- Get rid of `VarnameRetrievingWarning`.

tests/test_varname.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
import pytest
55
from varname import (varname,
6-
VarnameRetrievingWarning,
7-
MultipleTargetAssignmentWarning,
86
VarnameRetrievingError,
97
Wrapper,
108
will,
@@ -212,18 +210,23 @@ def function():
212210

213211
def test_raise():
214212

215-
def get_name():
216-
return varname(raise_exc=True)
213+
def get_name(raise_exc):
214+
return varname(raise_exc=raise_exc)
217215

218216
with pytest.raises(VarnameRetrievingError):
219-
get_name()
217+
get_name(True)
218+
219+
name = "0"
220+
# we can't get it in such way.
221+
name += str(get_name(False))
222+
assert name == '0None'
220223

221224
def test_multiple_targets():
222225

223226
def function():
224227
return varname()
225228

226-
with pytest.warns(MultipleTargetAssignmentWarning):
229+
with pytest.warns(UserWarning):
227230
y = x = function()
228231
assert y == x == 'y'
229232

@@ -237,9 +240,9 @@ def function():
237240
assert xyz == 'z'
238241

239242
x = 'a'
240-
with pytest.warns(VarnameRetrievingWarning):
243+
with pytest.raises(VarnameRetrievingError):
241244
x += function()
242-
assert x == 'avar_0'
245+
assert x == 'a'
243246

244247
func = function
245248
x = func()
@@ -431,7 +434,7 @@ def __init__(self):
431434
self.will = None
432435

433436
def permit(self, *_):
434-
self.will = will()
437+
self.will = will(raise_exc=False)
435438
if self.will == 'do':
436439
# let self handle do
437440
return self
@@ -488,9 +491,8 @@ def func(raise_exc):
488491
with pytest.raises(VarnameRetrievingError):
489492
a = func(True)
490493

491-
with pytest.warns(VarnameRetrievingWarning):
492-
b = func(False)
493-
assert b.startswith('var_')
494+
b = func(False)
495+
assert b is None
494496

495497

496498
def test_frame_fail_nameof(no_getframe):

varname.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,10 @@
1111

1212
__version__ = "0.3.0"
1313

14-
VARNAME_INDEX = [-1]
15-
16-
class MultipleTargetAssignmentWarning(Warning):
17-
"""When multiple-target assignment found, i.e. y = x = func()"""
18-
19-
class VarnameRetrievingWarning(Warning):
20-
"""When varname retrieving failed for whatever reason"""
21-
2214
class VarnameRetrievingError(Exception):
2315
"""When failed to retrieve the varname"""
2416

25-
def varname(caller: int = 1, raise_exc: bool = False) -> str:
17+
def varname(caller: int = 1, raise_exc: bool = True) -> str:
2618
"""Get the variable name that assigned by function/class calls
2719
2820
Args:
@@ -32,9 +24,7 @@ def varname(caller: int = 1, raise_exc: bool = False) -> str:
3224
to retrieve the name.
3325
3426
Returns:
35-
str|None: The variable name, or
36-
`var_<index>` (will be deprecated in `v0.4.0`) if failed.
37-
It returns `None` when `raise_exc` is `False` and
27+
str|None: The variable name, or `None` when `raise_exc` is `False` and
3828
we failed to retrieve the variable name.
3929
4030
Raises:
@@ -44,44 +34,34 @@ def varname(caller: int = 1, raise_exc: bool = False) -> str:
4434
is set to `True`.
4535
4636
Warns:
47-
MultipleTargetAssignmentWarning: When there are multiple target
37+
UserWarning: When there are multiple target
4838
in the assign node. (e.g: `a = b = func()`, in such a case,
4939
`b == 'a'`, may not be the case you want)
50-
VarnameRetrievingWarning: When `var_0` and alike returned.
51-
This will be deprecated in `v0.4.0`
5240
"""
5341
node = _get_node(caller)
5442
if not node:
5543
if raise_exc:
5644
raise VarnameRetrievingError("Unable to retrieve the ast node.")
57-
VARNAME_INDEX[0] += 1
58-
warnings.warn(f"var_{VARNAME_INDEX[0]} used, "
59-
"which will be deprecated in v0.4.0",
60-
VarnameRetrievingWarning)
61-
return f"var_{VARNAME_INDEX[0]}"
45+
return None
6246

6347
node = _lookfor_parent_assign(node)
6448
if not node:
6549
if raise_exc:
6650
raise VarnameRetrievingError(
6751
'Failed to retrieve the variable name.'
6852
)
69-
VARNAME_INDEX[0] += 1
70-
warnings.warn(f"var_{VARNAME_INDEX[0]} used, "
71-
"which will be deprecated in v0.4.0",
72-
VarnameRetrievingWarning)
73-
return f"var_{VARNAME_INDEX[0]}"
53+
return None
7454

7555
# Need to actually check that there's just one
7656
# give warnings if: a = b = func()
7757
if len(node.targets) > 1:
7858
warnings.warn("Multiple targets in assignment, variable name "
7959
"on the very left will be used.",
80-
MultipleTargetAssignmentWarning)
60+
UserWarning)
8161
target = node.targets[0]
8262
return _node_name(target)
8363

84-
def will(caller: int = 1, raise_exc: bool = False) -> str:
64+
def will(caller: int = 1, raise_exc: bool = True) -> str:
8565
"""Detect the attribute name right immediately after a function call.
8666
8767
Examples:
@@ -259,7 +239,7 @@ class Wrapper:
259239
value (any): The value this wrapper wraps
260240
"""
261241

262-
def __init__(self, value: any, raise_exc: bool = False):
242+
def __init__(self, value: any, raise_exc: bool = True):
263243
self.name: str = varname(raise_exc=raise_exc)
264244
self.value: any = value
265245

0 commit comments

Comments
 (0)