Skip to content

Commit 059f7f7

Browse files
authored
0.8.0 (#63)
* Prepare 0.8.0 - Remove NonVariableArgumentError, use ImproperUseError - Default `strict` of `varname()` to True - Use the very right variable for multiple-target assignment for `varname()` - Replace `argname()` by `argname2()` and deprecate `argname2()` - Add `UsingExecWarning` when `exec` is used to get function for `argname()` - Limit context lines of `rich_exc_message()` * Add make_readme to pre-commit so that we know if changes break README examples * Fix playground for 0.8 * 0.8.0
1 parent 67ee70f commit 059f7f7

21 files changed

+1374
-1042
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,5 @@ docs/api/
112112

113113
# vscode's local history extension
114114
.history/
115+
116+
playground/playground.nbconvert.ipynb

.pre-commit-config.yaml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,29 @@ repos:
2828
args: [varname]
2929
- repo: local
3030
hooks:
31-
- id: poetry2setuppy
32-
name: Convert pyproject.toml to setup.py
33-
entry: dephell deps convert --from=poetry --to=setup.py
31+
- id: compile-readme
32+
name: Make README.md
33+
entry: python make_readme.py README.raw.md > README.md
3434
language: system
35-
files: pyproject.toml
35+
files: README.raw.md
3636
pass_filenames: false
37-
- id: poetry2requirements
38-
name: Convert pyproject.toml to requirements.txt
39-
entry: dephell deps convert --from=poetry --to=requirements.txt
37+
always_run: true
38+
- id: poetry2setuppy
39+
# keep setup.py for github to trace dependencies/dependents
40+
# and for "pip install -e ." to work
41+
name: Convert pyproject.toml to setup.py
42+
entry: |
43+
bash -c "poetry build --format sdist;
44+
tar --wildcards -xvf dist/*-`poetry version -s`.tar.gz -O '*/setup.py' > setup.py"
4045
language: system
4146
files: pyproject.toml
4247
pass_filenames: false
48+
# - id: poetry2requirements
49+
# name: Convert pyproject.toml to requirements.txt
50+
# entry: poetry export -f requirements.txt --without-hashes -o requirements.txt
51+
# language: system
52+
# files: pyproject.toml
53+
# pass_filenames: false
4354
- id: mypycheck
4455
name: Type checking by mypy
4556
entry: mypy
@@ -55,3 +66,9 @@ repos:
5566
args: [tests/]
5667
pass_filenames: false
5768
files: ^tests/.+$|^varname/.+$
69+
- id: execute-playground
70+
name: Run playground notebook
71+
entry: jupyter nbconvert playground/playground.ipynb --execute --to notebook
72+
language: system
73+
files: playground/playground.ipynb
74+
pass_filenames: false

README.md

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ pip install -U varname
2020
- Retrieving names of variables a function/class call is assigned to from inside it, using `varname`.
2121
- Retrieving variable names directly, using `nameof`
2222
- Detecting next immediate attribute name, using `will`
23-
- Fetching argument names/sources passed to a function using `argname2`
24-
(`argname` is superseded by `argname2`)
23+
- Fetching argument names/sources passed to a function using `argname`
2524

2625
- Other helper APIs (built based on core features):
2726

@@ -144,7 +143,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
144143
def func():
145144
return varname(multi_vars=True)
146145

147-
a = func() # a == ('a', )
146+
a = func() # a == ('a',)
148147
a, b = func() # (a, b) == ('a', 'b')
149148
[a, b] = func() # (a, b) == ('a', 'b')
150149

@@ -155,14 +154,15 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
155154
- Some unusual use
156155

157156
```python
158-
def function():
159-
return varname()
157+
def function(**kwargs):
158+
return varname(strict=False)
160159

161-
func = func1 = function() # func == func1 == 'func'
160+
func = func1 = function() # func == func1 == 'func1'
161+
# if varname < 0.8: func == func1 == 'func'
162162
# a warning will be shown
163-
# since you may not want func1 to be 'func'
163+
# since you may not want func to be 'func1'
164164

165-
x = func(y = func()) # x == 'x'
165+
x = function(y = function()) # x == 'x'
166166

167167
# get part of the name
168168
func_abc = function()[-3:] # func_abc == 'abc'
@@ -173,16 +173,6 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
173173

174174
a = lambda: 0
175175
a.b = function() # a.b == 'b'
176-
177-
# Since v0.1.3
178-
# We can ask varname to raise exceptions
179-
# if it fails to detect the variable name
180-
def get_name(raise_exc):
181-
return varname(raise_exc=raise_exc)
182-
183-
a = {}
184-
a['b'] = get_name(True) # VarnameRetrievingError
185-
a['b'] = get_name(False) # None
186176
```
187177

188178
### The decorator way to register `__varname__` to functions/classes
@@ -272,29 +262,33 @@ awesome.permit() # AttributeError: Should do something with AwesomeClass object
272262
awesome.permit().do() == 'I am doing!'
273263
```
274264

275-
### Fetching argument names/sources using `argname2`
265+
### Fetching argument names/sources using `argname`
276266
```python
277-
from varname import argname2
267+
from varname import argname
278268

279269
def func(a, b=1):
280-
print(argname2('a'))
270+
print(argname('a'))
281271

282272
x = y = z = 2
283273
func(x) # prints: x
284274

275+
285276
def func2(a, b=1):
286-
print(argname2('a', 'b'))
277+
print(argname('a', 'b'))
287278
func2(y, b=x) # prints: ('y', 'x')
288279

280+
289281
# allow expressions
290282
def func3(a, b=1):
291-
print(argname2('a', 'b', vars_only=False))
283+
print(argname('a', 'b', vars_only=False))
292284
func3(x+y, y+x) # prints: ('x+y', 'y+x')
293285

286+
294287
# positional and keyword arguments
295288
def func4(*args, **kwargs):
296-
print(argname2('args[1]', 'kwargs["c"]'))
289+
print(argname('args[1]', 'kwargs[c]'))
297290
func4(y, x, c=z) # prints: ('x', 'z')
291+
298292
```
299293

300294
### Value wrapper
@@ -321,19 +315,22 @@ mydict = values_to_dict(foo, bar)
321315
from varname.helpers import debug
322316

323317
a = 'value'
324-
b = object()
325-
debug(a) # DEBUG: a='value'
326-
debug(b) # DEBUG: b=<object object at 0x2b70580e5f20>
318+
b = ['val']
319+
debug(a)
320+
# "DEBUG: a='value'\n"
321+
debug(b)
322+
# "DEBUG: b=['val']\n"
327323
debug(a, b)
328-
# DEBUG: a='value'
329-
# DEBUG: b=<object object at 0x2b70580e5f20>
324+
# "DEBUG: a='value'\nDEBUG: b=['val']\n"
330325
debug(a, b, merge=True)
331-
# DEBUG: a='value', b=<object object at 0x2b70580e5f20>
332-
debug(a, repr=False, prefix='') # a=value
326+
# "DEBUG: a='value', b=['val']\n"
327+
debug(a, repr=False, prefix='')
328+
# 'a=value\n'
333329
# also debug an expression
334-
debug(a+a) # DEBUG: a+a='valuevalue'
330+
debug(a+a)
331+
# "DEBUG: a+a='valuevalue'\n"
335332
# If you want to disable it:
336-
debug(a+a, vars_only=True) # error
333+
debug(a+a, vars_only=True) # ImproperUseError
337334
```
338335

339336
## Reliability and limitations

0 commit comments

Comments
 (0)