Skip to content

Commit 6d20658

Browse files
committed
docs: remove deprecated nameof examples from README
1 parent 13bc550 commit 6d20658

File tree

3 files changed

+33
-67
lines changed

3 files changed

+33
-67
lines changed

README.md

+16-33
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Note if you use `python < 3.8`, install `varname < 0.11`
2121
- Core features:
2222

2323
- Retrieving names of variables a function/class call is assigned to from inside it, using `varname`.
24-
- Retrieving variable names directly, using `nameof`
2524
- Detecting next immediate attribute name, using `will`
2625
- Fetching argument names/sources passed to a function using `argname`
2726

@@ -245,31 +244,6 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
245244
# foo.__varname__ == 'foo'
246245
```
247246

248-
### Getting variable names directly using `nameof`
249-
250-
```python
251-
from varname import varname, nameof
252-
253-
a = 1
254-
nameof(a) # 'a'
255-
256-
b = 2
257-
nameof(a, b) # ('a', 'b')
258-
259-
def func():
260-
return varname() + '_suffix'
261-
262-
f = func() # f == 'f_suffix'
263-
nameof(f) # 'f'
264-
265-
# get full names of (chained) attribute calls
266-
func.a = func
267-
nameof(func.a, vars_only=False) # 'func.a'
268-
269-
func.a.b = 1
270-
nameof(func.a.b, vars_only=False) # 'func.a.b'
271-
```
272-
273247
### Detecting next immediate attribute name
274248

275249
```python
@@ -415,22 +389,27 @@ obj.argnames # ['1', '2']
415389
`varname` is all depending on `executing` package to look for the node.
416390
The node `executing` detects is ensured to be the correct one (see [this][19]).
417391

418-
It partially works with environments where other AST magics apply, including
419-
`pytest`, `ipython`, `macropy`, `birdseye`, `reticulate` with `R`, etc. Neither
392+
It partially works with environments where other AST magics apply, including [`exec`][24] function,
393+
[`macropy`][21], [`birdseye`][22], [`reticulate`][23] with `R`, etc. Neither
420394
`executing` nor `varname` is 100% working with those environments. Use
421395
it at your own risk.
422396

423397
For example:
424398

425-
- This will not work with `pytest`:
399+
- This will not work:
426400

427401
```python
402+
from varname import argname
403+
404+
def getname(x):
405+
print(argname("x"))
406+
428407
a = 1
429-
assert nameof(a) == 'a' # pytest manipulated the ast here
408+
exec("getname(a)") # Cannot retrieve the node where the function is called.
430409

431-
# do this instead
432-
name_a = nameof(a)
433-
assert name_a == 'a'
410+
## instead
411+
# from varname.helpers import exec_code
412+
# exec_code("getname(a)")
434413
```
435414

436415
[1]: https://github.com/pwwang/python-varname
@@ -452,3 +431,7 @@ For example:
452431
[17]: https://img.shields.io/pypi/dm/varname?style=flat-square
453432
[19]: https://github.com/alexmojaki/executing#is-it-reliable
454433
[20]: https://stackoverflow.com/a/59364138/5088165
434+
[21]: https://github.com/lihaoyi/macropy
435+
[22]: https://github.com/alexmojaki/birdseye
436+
[23]: https://rstudio.github.io/reticulate/
437+
[24]: https://docs.python.org/3/library/functions.html#exec

README.raw.md

+16-33
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Note if you use `python < 3.8`, install `varname < 0.11`
2121
- Core features:
2222

2323
- Retrieving names of variables a function/class call is assigned to from inside it, using `varname`.
24-
- Retrieving variable names directly, using `nameof`
2524
- Detecting next immediate attribute name, using `will`
2625
- Fetching argument names/sources passed to a function using `argname`
2726

@@ -245,31 +244,6 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
245244
# foo.__varname__ == {foo.__varname__!r}
246245
```
247246

248-
### Getting variable names directly using `nameof`
249-
250-
```python
251-
from varname import varname, nameof
252-
253-
a = 1
254-
nameof(a) # {_expr!r}
255-
256-
b = 2
257-
nameof(a, b) # {_expr!r}
258-
259-
def func():
260-
return varname() + '_suffix'
261-
262-
f = func() # f == {f!r}
263-
nameof(f) # {_expr!r}
264-
265-
# get full names of (chained) attribute calls
266-
func.a = func
267-
nameof(func.a, vars_only=False) # {_expr!r}
268-
269-
func.a.b = 1
270-
nameof(func.a.b, vars_only=False) # {_expr!r}
271-
```
272-
273247
### Detecting next immediate attribute name
274248

275249
```python
@@ -410,22 +384,27 @@ obj.argnames # ['1', '2']
410384
`varname` is all depending on `executing` package to look for the node.
411385
The node `executing` detects is ensured to be the correct one (see [this][19]).
412386

413-
It partially works with environments where other AST magics apply, including
414-
`pytest`, `ipython`, `macropy`, `birdseye`, `reticulate` with `R`, etc. Neither
387+
It partially works with environments where other AST magics apply, including [`exec`][24] function,
388+
[`macropy`][21], [`birdseye`][22], [`reticulate`][23] with `R`, etc. Neither
415389
`executing` nor `varname` is 100% working with those environments. Use
416390
it at your own risk.
417391

418392
For example:
419393

420-
- This will not work with `pytest`:
394+
- This will not work:
421395

422396
```python
397+
from varname import argname
398+
399+
def getname(x):
400+
print(argname("x"))
401+
423402
a = 1
424-
assert nameof(a) == 'a' # pytest manipulated the ast here
403+
exec("getname(a)") # Cannot retrieve the node where the function is called.
425404

426-
# do this instead
427-
name_a = nameof(a)
428-
assert name_a == 'a'
405+
## instead
406+
# from varname.helpers import exec_code
407+
# exec_code("getname(a)")
429408
```
430409

431410
[1]: https://github.com/pwwang/python-varname
@@ -447,3 +426,7 @@ For example:
447426
[17]: https://img.shields.io/pypi/dm/varname?style=flat-square
448427
[19]: https://github.com/alexmojaki/executing#is-it-reliable
449428
[20]: https://stackoverflow.com/a/59364138/5088165
429+
[21]: https://github.com/lihaoyi/macropy
430+
[22]: https://github.com/alexmojaki/birdseye
431+
[23]: https://rstudio.github.io/reticulate/
432+
[24]: https://docs.python.org/3/library/functions.html#exec

make_readme.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def close(self, line):
220220
line = line.rstrip()
221221
if line == f"{self.indent}{self.backticks}":
222222
self.alive = False
223-
if self.codes:
223+
if self.codes and "{" in self.codes and "}" in self.codes:
224224
self.compile_exec(self.codes)
225225
self.codes = ""
226226
return True

0 commit comments

Comments
 (0)