Skip to content

Commit d3182fd

Browse files
authored
0.7.2 (#59)
* Add strict mode to varname (#57) * Update CI configuration so that it can be triggered in PR by assign and label. * Support the walrus operator (:=) (#58) * Prepare 0.7.2 - Update README to add breuleux as contributor - Don't upload coverage in CI with PRs - Update some docs for varname() * 0.7.2 Co-authored-by: Olivier Breuleux <breuleux AT gmail.com>
1 parent 867e4ae commit d3182fd

File tree

15 files changed

+186
-54
lines changed

15 files changed

+186
-54
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: Build and Deploy
22

33
on:
44
push:
5+
pull_request:
6+
types: [assigned, labeled]
57
release:
68
types: [published]
79

@@ -41,7 +43,7 @@ jobs:
4143
run: |
4244
export CODACY_PROJECT_TOKEN=${{ secrets.CODACY_PROJECT_TOKEN }}
4345
bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r .coverage.xml
44-
if: matrix.python-version == 3.7
46+
if: matrix.python-version == 3.9 && github.event_name != 'pull_request'
4547

4648
deploy:
4749
needs: build

.pylintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ disable=print-statement,
160160
unused-arguments,
161161
fixme,
162162
consider-using-dict-items,
163+
unexpected-keyword-arg,
163164
C0330
164165

165166
# Enable the message, report, category or checker with the given id(s). You can

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ Thanks goes to these awesome people/projects:
4141
<br /><sub><b>@alexmojaki</b></sub>
4242
</a>
4343
</td>
44+
<td align="center" style="min-width: 75px">
45+
<a href="https://github.com/breuleux">
46+
<img src="https://avatars.githubusercontent.com/u/599820?s=400&v=4" width="50px;" alt=""/>
47+
<br /><sub><b>@breuleux</b></sub>
48+
</a>
49+
</td>
4450
<td align="center" style="min-width: 75px">
4551
<a href="https://github.com/alexmojaki/executing">
46-
<img src="https://via.placeholder.com/50?text=executing" width="50px;" alt=""/>
52+
<img src="https://ui-avatars.com/api/?color=3333ff&background=ffffff&bold=true&name=e&size=400" width="50px;" alt=""/>
4753
<br /><sub><b>executing</b></sub>
4854
</a>
4955
</td>
@@ -99,6 +105,20 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
99105
func = asyncio.run(function()) # func == 'func'
100106
```
101107

108+
Use `strict` to control whether the call should be assigned to
109+
the variable directly:
110+
```python
111+
def function(strict):
112+
return varname(strict=strict)
113+
114+
func = function(True) # OK, direct assignment, func == 'func'
115+
116+
func = [function(True)] # Not a direct assignment, raises ImproperUseError
117+
func = [function(False)] # OK, func == ['func']
118+
119+
func = function(False), function(False) # OK, func = ('func', 'func')
120+
```
121+
102122
- Retrieving name of a class instance
103123

104124
```python
@@ -138,12 +158,6 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
138158
def function():
139159
return varname()
140160

141-
func = [function()] # func == ['func']
142-
143-
func = [function(), function()] # func == ['func', 'func']
144-
145-
func = function(), function() # func = ('func', 'func')
146-
147161
func = func1 = function() # func == func1 == 'func'
148162
# a warning will be shown
149163
# since you may not want func1 to be 'func'

README.rst

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,15 @@ Thanks goes to these awesome people/projects:
9090
<br /><sub><b>@alexmojaki</b></sub>
9191
</a>
9292
</td>
93+
<td align="center" style="min-width: 75px">
94+
<a href="https://github.com/breuleux">
95+
<img src="https://avatars.githubusercontent.com/u/599820?s=400&v=4" width="50px;" alt=""/>
96+
<br /><sub><b>@breuleux</b></sub>
97+
</a>
98+
</td>
9399
<td align="center" style="min-width: 75px">
94100
<a href="https://github.com/alexmojaki/executing">
95-
<img src="https://via.placeholder.com/50?text=executing" width="50px;" alt=""/>
101+
<img src="https://ui-avatars.com/api/?color=3333ff&background=ffffff&bold=true&name=e&size=400" width="50px;" alt=""/>
96102
<br /><sub><b>executing</b></sub>
97103
</a>
98104
</td>
@@ -156,6 +162,21 @@ Retrieving the variable names using ``varname(...)``
156162
157163
func = asyncio.run(function()) # func == 'func'
158164
165+
Use ``strict`` to control whether the call should be assigned to
166+
the variable directly:
167+
168+
.. code-block:: python
169+
170+
def function(strict):
171+
return varname(strict=strict)
172+
173+
func = function(True) # OK, direct assignment, func == 'func'
174+
175+
func = [function(True)] # Not a direct assignment, raises ImproperUseError
176+
func = [function(False)] # OK, func == ['func']
177+
178+
func = function(False), function(False) # OK, func = ('func', 'func')
179+
159180
*
160181
Retrieving name of a class instance
161182

@@ -199,12 +220,6 @@ Retrieving the variable names using ``varname(...)``
199220
def function():
200221
return varname()
201222
202-
func = [function()] # func == ['func']
203-
204-
func = [function(), function()] # func == ['func', 'func']
205-
206-
func = function(), function() # func = ('func', 'func')
207-
208223
func = func1 = function() # func == func1 == 'func'
209224
# a warning will be shown
210225
# since you may not want func1 to be 'func'

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.7.2
2+
- Add `strict` mode to `varname()` (#57)
3+
- Support the walrus operator (`:=`) (#58)
4+
15
## v0.7.1
26
- Add `ignore` argument to `argname2()`
37
- Fix Fix utils.get_argument_sources() when kwargs is given as `**kwargs`.

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 = "varname"
7-
version = "0.7.1"
7+
version = "0.7.2"
88
description = "Dark magics about variable names in python."
99
authors = [ "pwwang <[email protected]>",]
1010
license = "MIT"

setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
setup(
2525
long_description=readme,
2626
name='varname',
27-
version='0.7.1',
27+
version='0.7.2',
2828
description='Dark magics about variable names in python.',
2929
python_requires='==3.*,>=3.6.0',
30-
project_urls={"homepage": "https://github.com/pwwang/python-varname",
31-
"repository": "https://github.com/pwwang/python-varname"},
30+
project_urls={"homepage": "https://github.com/pwwang/python-varname", "repository": "https://github.com/pwwang/python-varname"},
3231
author='pwwang',
3332
author_email='[email protected]',
3433
license='MIT',

tests/named_expr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Contains code that is only importable with Python >= 3.8."""
2+
3+
from varname import varname
4+
5+
def function():
6+
return varname()
7+
8+
a = [b := function(), c := function()]

tests/test_argname.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,12 @@ def test_argname2_frame_error():
376376
def func(x):
377377
return argname2('x', frame=2)
378378

379-
with pytest.raises(ValueError):
379+
with pytest.raises(ValueError, match="is not a valid argument"):
380380
func(1)
381381

382382
def test_argname2_ignore():
383383
def target(*args):
384-
return argname2('*args', ignore=wrapper)
384+
return argname2('*args', ignore=(wrapper, 0))
385385

386386
def wrapper(*args):
387387
return target(*args)

tests/test_varname.py

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,6 @@ def function():
2222
func = function()
2323
assert func == 'func'
2424

25-
func = [function()]
26-
assert func == ['func']
27-
28-
func = [function(), function()]
29-
assert func == ['func', 'func']
30-
31-
func = (function(), )
32-
assert func == ('func', )
33-
34-
func = (function(), function())
35-
assert func == ('func', 'func')
36-
3725
def test_function_with_frame_arg():
3826

3927
def function():
@@ -132,14 +120,62 @@ def test_raise_exc():
132120
def get_name(raise_exc):
133121
return varname(raise_exc=raise_exc)
134122

135-
with pytest.raises(VarnameRetrievingError):
123+
with pytest.raises(ImproperUseError):
136124
get_name(True)
137125

138126
name = "0"
139127
# we can't get it in such way.
140128
name += str(get_name(False))
141129
assert name == '0None'
142130

131+
def test_strict():
132+
133+
def foo(x):
134+
return x
135+
136+
def function():
137+
return varname(strict=True)
138+
139+
func = function()
140+
assert func == 'func'
141+
142+
with pytest.raises(ImproperUseError):
143+
func = function() + "_"
144+
145+
with pytest.raises(ImproperUseError):
146+
func = foo(function())
147+
148+
with pytest.raises(ImproperUseError):
149+
func = [function()]
150+
151+
def test_not_strict():
152+
153+
def function():
154+
return varname(strict=False)
155+
156+
func = function()
157+
assert func == 'func'
158+
159+
func = [function()]
160+
assert func == ['func']
161+
162+
func = [function(), function()]
163+
assert func == ['func', 'func']
164+
165+
func = (function(), )
166+
assert func == ('func', )
167+
168+
func = (function(), function())
169+
assert func == ('func', 'func')
170+
171+
@pytest.mark.skipif(
172+
sys.version_info < (3, 8),
173+
reason="named expressions require Python >= 3.8"
174+
)
175+
def test_named_expr():
176+
from .named_expr import a
177+
assert a == ["b", "c"]
178+
143179
def test_multiple_targets():
144180

145181
def function():
@@ -160,7 +196,7 @@ def function():
160196
assert xyz == 'z'
161197

162198
x = 'a'
163-
with pytest.raises(VarnameRetrievingError):
199+
with pytest.raises(ImproperUseError):
164200
x += function()
165201
assert x == 'a'
166202

@@ -366,7 +402,7 @@ def foo6():
366402
def foo7():
367403
return varname(ignore=(foo4, 100))
368404

369-
with pytest.raises(VarnameRetrievingError):
405+
with pytest.raises(ImproperUseError):
370406
f6 = foo6()
371407

372408
def test_ignore_dirname(tmp_path):

varname/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
)
1212
from .core import varname, nameof, will, argname, argname2
1313

14-
__version__ = "0.7.1"
14+
__version__ = "0.7.2"

0 commit comments

Comments
 (0)