Skip to content

Commit cb2009f

Browse files
committed
0.4.0
1 parent a593ef5 commit cb2009f

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

README.rst

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,12 @@ Retrieving the variable names from inside a function call/class instantiation
177177
# Since v0.1.3
178178
# We can ask varname to raise exceptions
179179
# if it fails to detect the variable name
180-
181-
from varname import VarnameRetrievingError
182-
def get_name():
183-
try:
184-
# if raise_exc is False
185-
# "var_<index>" will be returned
186-
return varname(raise_exc=True)
187-
except VarnameRetrieveingError:
188-
return None
180+
def get_name(raise_exc):
181+
return varname(raise_exc=raise_exc)
189182
190183
a = {}
191-
a['b'] = get_name() # None
184+
a['b'] = get_name(True) # VarnameRetrievingError
185+
a['b'] = get_name(False) # None
192186
193187
Value wrapper
194188
^^^^^^^^^^^^^
@@ -244,7 +238,7 @@ Detecting next immediate attribute name
244238
self.will = None
245239
246240
def permit(self):
247-
self.will = will()
241+
self.will = will(raise_exc=False)
248242
if self.will == 'do':
249243
# let self handle do
250244
return self
@@ -296,16 +290,21 @@ Injecting ``__varname__``
296290
b.append(1)
297291
a == b
298292
299-
Limitations
300-
-----------
293+
Reliability and limitations
294+
---------------------------
301295

302296
``python-varname`` is all depending on ``executing`` package to look for the node.
303-
It does not work with any environment where ``executing`` is not able to detect the node.
297+
The node ``executing`` detects is ensured to be the correct one (see `this <https://github.com/alexmojaki/executing#is-it-reliable>`_\ ).
298+
299+
It partially works with environments where other AST magics apply, including
300+
``pytest``\ , ``ipython``\ , ``macropy``\ , ``birdseye``\ , ``reticulate`` with ``R``\ , etc. Neither
301+
``executing`` nor ``python-varname`` is 100% working with those environments. Use
302+
it at your own risk.
303+
304304
For example:
305305

306306

307307
*
308-
Environments where other AST magics apply. For example: ``pytest``\ , ``ipython``\ , ``macropy``\ , or ``birdseye``.
309308
This will not work with ``pytest``\ :
310309

311310
.. code-block:: python
@@ -317,4 +316,13 @@ For example:
317316
name_a = nameof(a)
318317
assert name_a == 'a'
319318
319+
*
320+
This will also typically fail with ``ipython``\ :
321+
322+
.. code-block:: python
323+
324+
a = 1
325+
for _ in [0]:
326+
print(nameof(a))
327+
320328
* ``R`` with ``reticulate``.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"
44

55
[tool.poetry]
66
name = "python-varname"
7-
version = "0.3.0"
7+
version = "0.4.0"
88
description = "Retrieving variable names of function or class calls."
99
authors = [ "pwwang <pwwang@pwwang.com>",]
1010
license = "MIT"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
setup(
2222
long_description=readme,
2323
name='python-varname',
24-
version='0.3.0',
24+
version='0.4.0',
2525
description='Retrieving variable names of function or class calls.',
2626
python_requires='==3.*,>=3.6.0',
2727
project_urls={

varname.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import executing
1111

12-
__version__ = "0.3.0"
12+
__version__ = "0.4.0"
1313

1414
class VarnameRetrievingError(Exception):
1515
"""When failed to retrieve the varname"""

0 commit comments

Comments
 (0)