Skip to content

Commit ea1ec82

Browse files
authored
0.13.0 (#105)
* style: change max line length to 88 * style: clean up test code styles * feat: support subscript node for varname * docs: update README.md * ci: remove python3.8 from CI * 0.13.0
1 parent be560cc commit ea1ec82

13 files changed

+415
-275
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
python-version: [3.8, 3.9, "3.10", "3.11", "3.12-dev"]
16+
python-version: [3.9, "3.10", "3.11", "3.12-dev"]
1717

1818
steps:
1919
- uses: actions/checkout@v3

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
199199
func = function2() # func == 'func'
200200

201201
a = lambda: 0
202-
a.b = function() # a.b == 'b'
202+
a.b = function() # a.b == 'a.b'
203203
```
204204

205205
### The decorator way to register `__varname__` to functions/classes

README.raw.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
199199
func = function2() # func == 'func'
200200

201201
a = lambda: 0
202-
a.b = function() # a.b == 'b'
202+
a.b = function() # a.b == 'a.b'
203203
```
204204

205205
### The decorator way to register `__varname__` to functions/classes

docs/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
## 0.13.0
4+
5+
- style: change max line length to 88
6+
- style: clean up test code styles
7+
- feat: support subscript node for varname (#104)
8+
- ci: remove python3.8 from CI
9+
- breaking!: `varname` of `a.b` now returns `"a.b"` instead of `"a"`
10+
311
## 0.12.2
412

513
- Add `helpers.exec_code` function to replace `exec` so that source code available at runtime

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"
44

55
[tool.poetry]
66
name = "varname"
7-
version = "0.12.2"
7+
version = "0.13.0"
88
description = "Dark magics about variable names in python."
99
authors = [ "pwwang <[email protected]>",]
1010
license = "MIT"
@@ -42,6 +42,6 @@ show_error_codes = true
4242
strict_optional = false
4343

4444
[tool.black]
45-
line-length = 80
45+
line-length = 88
4646
target-version = ['py37', 'py38', 'py39', 'py310']
4747
include = '\.pyi?$'

tests/test_argname.py

+10-47
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
from functools import singledispatch
33

44
import pytest
5-
from varname import *
5+
from varname import (
6+
argname,
7+
UsingExecWarning,
8+
ImproperUseError,
9+
VarnameRetrievingError,
10+
)
611

712

813
def test_argname():
@@ -72,7 +77,7 @@ def func(a):
7277

7378

7479
def test_argname_non_argument():
75-
x = 1
80+
x = 1 # noqa F841
7681
y = lambda: argname("x")
7782
with pytest.raises(ImproperUseError, match="'x' is not a valid argument"):
7883
y()
@@ -96,7 +101,7 @@ def func(a, b, c, d=4):
96101

97102

98103
def test_argname_funcnode_not_call():
99-
x = 1
104+
x = 1 # noqa F841
100105

101106
class Foo:
102107

@@ -194,7 +199,7 @@ def func(*args, **kwargs):
194199

195200
def test_argname_argname_node_na():
196201
source = textwrap.dedent(
197-
f"""\
202+
"""\
198203
from varname import argname
199204
def func(a):
200205
return argname(a)
@@ -222,17 +227,6 @@ def func(a):
222227
exec("x=1; func(x)")
223228

224229

225-
def test_argname_func_na():
226-
def func(a):
227-
return argname("a")
228-
229-
with pytest.raises(
230-
VarnameRetrievingError,
231-
match="The source code of 'argname' calling is not available",
232-
):
233-
exec("x=1; func(x)")
234-
235-
236230
def test_argname_wrapper():
237231
def decorator(f):
238232
def wrapper(arg, *more_args):
@@ -264,7 +258,7 @@ def func(a, *args, **kwargs):
264258

265259
def test_argname_nosuch_varpos_arg():
266260
def func(a, *args):
267-
another = []
261+
another = [] # noqa F841
268262
return argname("a", "*another")
269263

270264
x = y = 1
@@ -283,36 +277,6 @@ def func(a, b):
283277
assert names == "x"
284278

285279

286-
def test_argname_singledispatched():
287-
# GH53
288-
@singledispatch
289-
def add(a, b):
290-
aname = argname("a", "b", func=add.dispatch(object))
291-
return aname + (1,) # distinguish
292-
293-
@add.register(int)
294-
def add_int(a, b):
295-
aname = argname("a", "b", func=add_int)
296-
return aname + (2,)
297-
298-
@add.register(str)
299-
def add_str(a, b):
300-
aname = argname("a", "b", dispatch=str)
301-
return aname + (3,)
302-
303-
x = y = 1
304-
out = add(x, y)
305-
assert out == ("x", "y", 2)
306-
307-
t = s = "a"
308-
out = add(t, s)
309-
assert out == ("t", "s", 3)
310-
311-
p = q = 1.2
312-
out = add(p, q)
313-
assert out == ("p", "q", 1)
314-
315-
316280
def test_argname_func_na():
317281
def func(a):
318282
return argname("a")
@@ -427,7 +391,6 @@ def __setitem__(self, name, value) -> None:
427391
self.__dict__["meta"]["name2"] = argname("name", vars_only=False)
428392
self.__dict__["meta"]["value"] = argname("value")
429393

430-
431394
a = A()
432395
out = a.x
433396
assert out == "'x'"

tests/test_bytecode_nameof.py

+33-26
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import pytest
44
import unittest
55
from varname.utils import bytecode_nameof as bytecode_nameof_cached
6-
from varname import *
6+
from varname import nameof, varname, ImproperUseError, VarnameRetrievingError
7+
78
# config.debug = True
89

10+
911
def bytecode_nameof(frame):
1012
frame = sys._getframe(frame)
1113
return bytecode_nameof_cached(frame.f_code, frame.f_lasti)
1214

15+
1316
def nameof_both(var, *more_vars):
1417
"""Test both implementations at the same time"""
1518
result = nameof(var, *more_vars, frame=2)
@@ -18,58 +21,62 @@ def nameof_both(var, *more_vars):
1821
assert result == bytecode_nameof(frame=2)
1922
return result
2023

24+
2125
class Weird:
2226
def __add__(self, other):
2327
bytecode_nameof(frame=2)
2428

29+
2530
class TestNameof(unittest.TestCase):
2631
def test_original_nameof(self):
2732
x = 1
28-
self.assertEqual(nameof(x), 'x')
29-
self.assertEqual(nameof_both(x), 'x')
30-
self.assertEqual(bytecode_nameof(x), 'x')
33+
self.assertEqual(nameof(x), "x")
34+
self.assertEqual(nameof_both(x), "x")
35+
self.assertEqual(bytecode_nameof(x), "x")
3136

3237
def test_bytecode_nameof_wrong_node(self):
3338
with pytest.raises(
34-
VarnameRetrievingError,
35-
match="Did you call 'nameof' in a weird way",
39+
VarnameRetrievingError,
40+
match="Did you call 'nameof' in a weird way",
3641
):
3742
Weird() + Weird()
3843

3944
def test_bytecode_pytest_nameof_fail(self):
4045
with pytest.raises(
41-
VarnameRetrievingError,
42-
match=("Found the variable name '@py_assert2' "
43-
"which is obviously wrong."),
46+
VarnameRetrievingError,
47+
match=(
48+
"Found the variable name '@py_assert2' " "which is obviously wrong."
49+
),
4450
):
4551
lam = lambda: 0
4652
lam.a = 1
47-
assert bytecode_nameof(lam.a) == 'a'
53+
assert bytecode_nameof(lam.a) == "a"
4854

4955
def test_nameof(self):
5056
a = 1
5157
b = nameof_both(a)
52-
assert b == 'a'
58+
assert b == "a"
5359
nameof2 = nameof_both
5460
c = nameof2(a, b)
55-
assert b == 'a'
56-
assert c == ('a', 'b')
61+
assert b == "a"
62+
assert c == ("a", "b")
63+
5764
def func():
58-
return varname() + 'abc'
65+
return varname() + "abc"
5966

6067
f = func()
61-
assert f == 'fabc'
68+
assert f == "fabc"
6269

63-
self.assertEqual(nameof_both(f), 'f')
64-
self.assertEqual('f', nameof_both(f))
70+
self.assertEqual(nameof_both(f), "f")
71+
self.assertEqual("f", nameof_both(f))
6572
self.assertEqual(len(nameof_both(f)), 1)
6673

6774
fname1 = fname = nameof_both(f)
68-
self.assertEqual(fname, 'f')
69-
self.assertEqual(fname1, 'f')
75+
self.assertEqual(fname, "f")
76+
self.assertEqual(fname1, "f")
7077

7178
with pytest.raises(ImproperUseError):
72-
nameof_both(a==1)
79+
nameof_both(a == 1)
7380

7481
with pytest.raises(VarnameRetrievingError):
7582
bytecode_nameof(a == 1)
@@ -79,30 +86,30 @@ def func():
7986
# nameof_both()
8087

8188
def test_nameof_statements(self):
82-
a = {'test': 1}
89+
a = {"test": 1}
8390
test = {}
8491
del a[nameof_both(test)]
8592
assert a == {}
8693

8794
def func():
8895
return nameof_both(test)
8996

90-
assert func() == 'test'
97+
assert func() == "test"
9198

9299
def func2():
93100
yield nameof_both(test)
94101

95-
assert list(func2()) == ['test']
102+
assert list(func2()) == ["test"]
96103

97104
def func3():
98105
raise ValueError(nameof_both(test))
99106

100107
with pytest.raises(ValueError) as verr:
101108
func3()
102-
assert str(verr.value) == 'test'
109+
assert str(verr.value) == "test"
103110

104111
for i in [0]:
105-
self.assertEqual(nameof_both(test), 'test')
112+
self.assertEqual(nameof_both(test), "test")
106113
self.assertEqual(len(nameof_both(test)), 4)
107114

108115
def test_nameof_expr(self):
@@ -126,4 +133,4 @@ def test_nameof_expr(self):
126133
self.assertEqual(nameof_both(lam.lam.lam.lam), "lam")
127134
self.assertEqual(nameof_both(lams[0].lam), "lam")
128135
self.assertEqual(nameof_both(lams[0].lam.a), "a")
129-
self.assertEqual(nameof_both((lam() or lams[0]).lam.a), "a")
136+
self.assertEqual(nameof_both((lam() or lams[0]).lam.a), "a")

0 commit comments

Comments
 (0)