Skip to content

Commit 2e720fe

Browse files
authored
Pump to 0.5.6 (#38)
* Deprecate inject_varname, use register instead. * 0.5.6 * Update CHANGELOG and README
1 parent bd6b2d5 commit 2e720fe

File tree

6 files changed

+60
-17
lines changed

6 files changed

+60
-17
lines changed

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
6262

6363
```python
6464
def function():
65-
# I know that at which stack this will be called
66-
return varname(caller=3)
65+
# I know that at which frame this will be called
66+
return varname(3)
67+
# with v0.5.6+ now you can also specify a list of intermediate
68+
# calls to be ignored in counting:
69+
# module = sys.modules[__name__]
70+
# return varname(ignore=[(module, 'function1'), (module, 'function2)])
6771

6872
def function1():
6973
return function()
@@ -74,6 +78,24 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
7478
func = function2() # func == 'func'
7579
```
7680

81+
- `varname` in type annotation or async context
82+
```python
83+
import typing
84+
class Foo:
85+
def __init__(self):
86+
self.id = varname(ignore=[typing])
87+
88+
foo: Foo = Foo() # foo.id == 'foo'
89+
```
90+
91+
```python
92+
import asyncio
93+
async def func():
94+
return varname(ignore=[asyncio])
95+
96+
x = asyncio.run(func()) # x == 'x'
97+
```
98+
7799
- Retrieving instance name of a class
78100

79101
```python
@@ -88,6 +110,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
88110
return copied
89111

90112
k = Foo() # k.id == 'k'
113+
# see also register __varname__ to classes
91114

92115
k2 = k.copy() # k2.id == 'k2'
93116
```
@@ -213,12 +236,12 @@ awesome.permit() # AttributeError: Should do something with AwesomeClass object
213236
awesome.permit().do() == 'I am doing!'
214237
```
215238

216-
### Injecting `__varname__` to classes
239+
### Register `__varname__` to classes
217240

218241
```python
219-
from varname import inject_varname
242+
from varname import register
220243

221-
@inject_varname
244+
@register
222245
class Dict(dict):
223246
pass
224247

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.5.6
2+
- Add `ignore` argument to `varname` to ignore frames that are not counted by caller
3+
- Deprecate `inject_varname`, use `register` instead
4+
15
## v0.5.5
26
- Deprecate inject and use inject_varname decorator instead
37

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

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
setup(
2525
long_description=readme,
2626
name='varname',
27-
version='0.5.5',
27+
version='0.5.6',
2828
description='Dark magics about variable names in python.',
2929
python_requires='==3.*,>=3.6.0',
3030
project_urls={"homepage": "https://github.com/pwwang/python-varname", "repository": "https://github.com/pwwang/python-varname"},

tests/test_bytecode_nameof.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
def nameof_both(*args):
77
"""Test both implementations at the same time"""
8-
# No need caller=2 anymore, since internal calls from varname are ignored
9-
# now by default
108
result = nameof(*args, caller=2)
119
if len(args) == 1:
1210
assert result == _bytecode_nameof(caller=2)

varname.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
import executing
1313

14-
__version__ = "0.5.5"
14+
__version__ = "0.5.6"
1515
__all__ = [
1616
"VarnameRetrievingError", "varname", "will", "inject_varname",
17-
"inject", "nameof", "namedtuple", "Wrapper", "debug"
17+
"register", "inject", "nameof", "namedtuple", "Wrapper", "debug"
1818
]
1919

2020
# To show how the desired frame is selected and other frames are skipped
@@ -181,7 +181,7 @@ def will(caller: int = 1, raise_exc: bool = True) -> Optional[str]:
181181
# ast.Attribute
182182
return node.attr
183183

184-
def inject_varname(
184+
def register(
185185
cls: type = None, *,
186186
caller: int = 1,
187187
multi_vars: bool = False,
@@ -201,23 +201,24 @@ def inject_varname(
201201
to retrieve the name.
202202
203203
Examples:
204-
>>> @inject_varname
204+
>>> @varname.register
205205
>>> class Foo: pass
206206
>>> foo = Foo()
207207
>>> # foo.__varname__ == 'foo'
208208
209209
Returns:
210210
The wrapper function or the class itself if it is specified explictly.
211211
"""
212+
212213
if cls is not None:
213-
# Used as @inject_varname directly
214-
return inject_varname(
214+
# Used as @register directly
215+
return register(
215216
caller=caller,
216217
multi_vars=multi_vars,
217218
raise_exc=raise_exc
218219
)(cls)
219220

220-
# Used as @inject_varname(multi_vars=..., raise_exc=...)
221+
# Used as @register(multi_vars=..., raise_exc=...)
221222
def wrapper(cls):
222223
"""The wrapper function to wrap a class and inject `__varname__`"""
223224
orig_init = cls.__init__
@@ -237,6 +238,23 @@ def wrapped_init(self, *args, **kwargs):
237238

238239
return wrapper
239240

241+
def inject_varname(
242+
cls: type = None, *,
243+
caller: int = 1,
244+
multi_vars: bool = False,
245+
raise_exc: bool = True
246+
) -> Union[type, Callable[[type], type]]:
247+
"""Alias of register. Will be deprecated"""
248+
warnings.warn("Decorator inject_varname will be removed in 0.6.0. "
249+
"Use varname.register to decorate your class.",
250+
DeprecationWarning)
251+
return register(
252+
cls,
253+
caller=caller,
254+
multi_vars=multi_vars,
255+
raise_exc=raise_exc
256+
)
257+
240258
def inject(obj: object) -> object:
241259
"""Inject attribute `__varname__` to an object
242260
@@ -268,7 +286,7 @@ def inject(obj: object) -> object:
268286
The object with __varname__ injected
269287
"""
270288
warnings.warn("Function inject will be removed in 0.6.0. Use "
271-
"varname.inject_varname to decorate your class.",
289+
"varname.register to decorate your class.",
272290
DeprecationWarning)
273291
vname = varname(caller=0)
274292
try:

0 commit comments

Comments
 (0)