Skip to content

Commit 362a8a9

Browse files
authored
0.11.0 (#92)
* 🏷️ Overload types for nameof and argname * πŸ’₯ Drop python <3.8 to prepare 0.11 * 🏷️ Fix return types of argname overload * πŸ”– 0.11.0
1 parent 5095f2c commit 362a8a9

File tree

9 files changed

+253
-360
lines changed

9 files changed

+253
-360
lines changed

β€Ž.github/workflows/build.ymlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-20.04 # lastest doesn't support python3.6
1414
strategy:
1515
matrix:
16-
python-version: [3.6, 3.7, 3.8, 3.9, "3.10", 3.11-dev]
16+
python-version: [3.8, 3.9, "3.10", 3.11-dev]
1717

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

β€ŽREADME.mdβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ Dark magics about variable names in python
99
[CHANGELOG][16] | [API][15] | [Playground][11] | :fire: [StackOverflow answer][20]
1010

1111
## Installation
12+
1213
```shell
1314
pip install -U varname
1415
```
1516

17+
Note if you use `python < 3.8`, install `varname < 0.11`
18+
1619
## Features
1720

1821
- Core features:
@@ -90,6 +93,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
9093
```
9194

9295
When there are intermediate frames:
96+
9397
```python
9498
def wrapped():
9599
return function()
@@ -102,6 +106,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
102106
```
103107

104108
Or use `ignore` to ignore the wrapped frame:
109+
105110
```python
106111
def wrapped():
107112
return function()
@@ -113,6 +118,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
113118
```
114119

115120
Calls from standard libraries are ignored by default:
121+
116122
```python
117123
import asyncio
118124

@@ -124,6 +130,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
124130

125131
Use `strict` to control whether the call should be assigned to
126132
the variable directly:
133+
127134
```python
128135
def function(strict):
129136
return varname(strict=strict)
@@ -256,6 +263,7 @@ nameof(func.a.b, vars_only=False) # 'func.a.b'
256263
```
257264

258265
### Detecting next immediate attribute name
266+
259267
```python
260268
from varname import will
261269
class AwesomeClass:
@@ -281,6 +289,7 @@ awesome.permit().do() == 'I am doing!'
281289
```
282290

283291
### Fetching argument names/sources using `argname`
292+
284293
```python
285294
from varname import argname
286295

@@ -339,6 +348,7 @@ mydict = values_to_dict(foo, bar)
339348
```
340349

341350
### Debugging with `debug`
351+
342352
```python
343353
from varname.helpers import debug
344354

@@ -362,6 +372,7 @@ debug(a+a, vars_only=True) # ImproperUseError
362372
```
363373

364374
## Reliability and limitations
375+
365376
`varname` is all depending on `executing` package to look for the node.
366377
The node `executing` detects is ensured to be the correct one (see [this][19]).
367378

@@ -373,6 +384,7 @@ it at your own risk.
373384
For example:
374385

375386
- This will not work with `pytest`:
387+
376388
```python
377389
a = 1
378390
assert nameof(a) == 'a' # pytest manipulated the ast here

β€ŽREADME.raw.mdβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ Dark magics about variable names in python
99
[CHANGELOG][16] | [API][15] | [Playground][11] | :fire: [StackOverflow answer][20]
1010

1111
## Installation
12+
1213
```shell
1314
pip install -U varname
1415
```
1516

17+
Note if you use `python < 3.8`, install `varname < 0.11`
18+
1619
## Features
1720

1821
- Core features:
@@ -90,6 +93,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
9093
```
9194

9295
When there are intermediate frames:
96+
9397
```python
9498
def wrapped():
9599
return function()
@@ -102,6 +106,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
102106
```
103107

104108
Or use `ignore` to ignore the wrapped frame:
109+
105110
```python
106111
def wrapped():
107112
return function()
@@ -113,6 +118,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
113118
```
114119

115120
Calls from standard libraries are ignored by default:
121+
116122
```python
117123
import asyncio
118124

@@ -124,6 +130,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
124130

125131
Use `strict` to control whether the call should be assigned to
126132
the variable directly:
133+
127134
```python
128135
def function(strict):
129136
return varname(strict=strict)
@@ -256,6 +263,7 @@ nameof(func.a.b, vars_only=False) # {_expr!r}
256263
```
257264

258265
### Detecting next immediate attribute name
266+
259267
```python
260268
from varname import will
261269
class AwesomeClass:
@@ -281,6 +289,7 @@ awesome.permit().do() == 'I am doing!'
281289
```
282290

283291
### Fetching argument names/sources using `argname`
292+
284293
```python
285294
from varname import argname
286295

@@ -334,6 +343,7 @@ mydict = values_to_dict(foo, bar)
334343
```
335344

336345
### Debugging with `debug`
346+
337347
```python
338348
from varname.helpers import debug
339349

@@ -357,6 +367,7 @@ debug(a+a, vars_only=True) # {_exc}
357367
```
358368

359369
## Reliability and limitations
370+
360371
`varname` is all depending on `executing` package to look for the node.
361372
The node `executing` detects is ensured to be the correct one (see [this][19]).
362373

@@ -368,6 +379,7 @@ it at your own risk.
368379
For example:
369380

370381
- This will not work with `pytest`:
382+
371383
```python
372384
a = 1
373385
assert nameof(a) == 'a' # pytest manipulated the ast here

β€Ždocs/CHANGELOG.mdβ€Ž

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## v0.11.0
4+
5+
- πŸ“ Update README for shields badges (#91)
6+
- 🏷️ Overload types for nameof and argname (#77)
7+
- πŸ’₯ Drop python <3.8 for v0.11
8+
If you need support for python <3.8, please use varname <0.11
9+
310
## v0.10.0
411

512
- ✨ Support python 3.11

β€Žmake_readme.pyβ€Ž

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
from io import StringIO
4545
from contextlib import redirect_stdout
4646

47+
4748
def printn(line):
4849
"""Shortcut to print without new line"""
4950
print(line, end="")
5051

52+
5153
class CodeBlock:
5254
"""The code block
5355
@@ -73,70 +75,70 @@ class CodeBlock:
7375
stdout: The standard output of current code piece
7476
It will get overwritten by next code piece
7577
"""
78+
7679
def __init__(self, indent, backticks, lang, index, envs=None):
7780
self.indent = indent
7881
self.backticks = backticks
7982
self.lang = lang
80-
self.codes = ''
83+
self.codes = ""
8184
self.envs = envs or {}
82-
self.produced = ''
85+
self.produced = ""
8386
self.alive = True
8487
self.index = index
8588
self.piece = 0
86-
self.stdout = ''
89+
self.stdout = ""
8790

8891
def compile_exec(self, code):
8992
"""Compile and execute the code"""
9093
sourcefile = path.join(
9194
tempfile._get_default_tempdir(),
9295
f"codeblock_{self.index}_{self.piece}-"
93-
f"{next(tempfile._get_candidate_names())}"
96+
f"{next(tempfile._get_candidate_names())}",
9497
)
95-
with open(sourcefile, 'w') as fsrc:
98+
with open(sourcefile, "w") as fsrc:
9699
fsrc.write(code)
97-
code = compile(code, sourcefile, mode='exec')
100+
code = compile(code, sourcefile, mode="exec")
98101
sio = StringIO()
99102
with redirect_stdout(sio):
100103
exec(code, self.envs)
101104
self.stdout = sio.getvalue()
102105
self.piece += 1
103106

104-
105107
def feed(self, line):
106108
"""Feed a single line to the code block, with line break"""
107-
if self.lang not in ('python', 'python3'):
109+
if self.lang not in ("python", "python3"):
108110
self.produced += line
109111

110112
else:
111-
if not line.strip(): # empty line
113+
if not line.strip(): # empty line
112114
self.codes += "\n"
113115
self.produced += line
114116
else:
115-
line = line[len(self.indent):]
117+
line = line[len(self.indent) :]
116118
if CodeBlock.should_compile(line):
117119
if self.codes:
118120
self.compile_exec(self.codes)
119-
self.codes = ''
121+
self.codes = ""
120122
self.produced += self.indent + self.compile_line(line)
121123
else:
122124
self.codes += line
123125
self.produced += self.indent + line
124126

125127
def _compile_expr(self, line):
126128
"""Compile {_expr}"""
127-
varname = '_expr'
128-
source = f'{varname} = {line}'
129+
varname = "_expr"
130+
source = f"{varname} = {line}"
129131
self.compile_exec(source)
130-
code, comment = line.split('#', 1)
132+
code, comment = line.split("#", 1)
131133
comment = comment.format(**self.envs)
132-
return '#'.join((code, comment))
134+
return "#".join((code, comment))
133135

134136
def _compile_out(self, line):
135137
"""Compile {_out}"""
136138
self.compile_exec(line)
137-
code, comment = line.split('#', 1)
139+
code, comment = line.split("#", 1)
138140
comment = comment.format(**self.envs, _out=self.stdout)
139-
return '#'.join((code, comment))
141+
return "#".join((code, comment))
140142

141143
def _compile_exc(self, line, msg=False):
142144
"""Compile {_exc} and {_exc_msg}"""
@@ -153,36 +155,36 @@ def _compile_exc(self, line, msg=False):
153155
source += " + ': ' + str(exc)"
154156
source += "\n"
155157
self.compile_exec(source)
156-
code, comment = line.split('#', 1)
158+
code, comment = line.split("#", 1)
157159
comment = comment.format(**self.envs)
158-
return '#'.join((code, comment))
160+
return "#".join((code, comment))
159161

160162
def _compile_hidden(self, line):
161163
"""Compile {_hidden}"""
162164
self.compile_exec(line)
163-
return ''
165+
return ""
164166

165167
def _compile_var(self, line):
166168
"""Compile variables"""
167169
self.compile_exec(line)
168-
code, comment = line.split('#', 1)
170+
code, comment = line.split("#", 1)
169171
comment = comment.format(**self.envs)
170-
return '#'.join((code, comment))
172+
return "#".join((code, comment))
171173

172174
def compile_line(self, line):
173175
"""Compile a single line"""
174-
code, comment = line.split('#', 1)
176+
code, comment = line.split("#", 1)
175177
if not code:
176-
return '#' + comment.format(**self.envs, _out=self.stdout)
177-
if '{_hidden}' in comment:
178+
return "#" + comment.format(**self.envs, _out=self.stdout)
179+
if "{_hidden}" in comment:
178180
return self._compile_hidden(line)
179-
if re.search(r'\{_expr(?:!\w+)?\}', comment):
181+
if re.search(r"\{_expr(?:!\w+)?\}", comment):
180182
return self._compile_expr(line)
181-
if re.search(r'\{_out(?:!\w+)?\}', comment):
183+
if re.search(r"\{_out(?:!\w+)?\}", comment):
182184
return self._compile_out(line)
183-
if '{_exc}' in comment:
185+
if "{_exc}" in comment:
184186
return self._compile_exc(line)
185-
if '{_exc_msg}' in comment:
187+
if "{_exc_msg}" in comment:
186188
return self._compile_exc(line, msg=True)
187189

188190
return self._compile_var(line)
@@ -207,7 +209,9 @@ def try_open(cls, line, envs, index):
207209
return None
208210

209211
indent, backticks, lang = (
210-
matches.group(1), matches.group(2), matches.group(3),
212+
matches.group(1),
213+
matches.group(2),
214+
matches.group(3),
211215
)
212216
return cls(indent, backticks, lang, index, envs)
213217

@@ -218,19 +222,19 @@ def close(self, line):
218222
self.alive = False
219223
if self.codes:
220224
self.compile_exec(self.codes)
221-
self.codes = ''
225+
self.codes = ""
222226
return True
223227

224228
return False
225229

226230
@staticmethod
227231
def should_compile(line):
228232
"""Whether we should compile a line or treat it as a plain line"""
229-
if '#' not in line:
233+
if "#" not in line:
230234
return False
231235

232-
comment = line.split('#', 1)[1]
233-
return re.search(r'\{[^}]+\}', comment)
236+
comment = line.split("#", 1)[1]
237+
return re.search(r"\{[^}]+\}", comment)
234238

235239

236240
def compile_readme(rawfile):
@@ -254,6 +258,7 @@ def compile_readme(rawfile):
254258
if maybe_codeblock:
255259
codeblock = maybe_codeblock
256260

261+
257262
if __name__ == "__main__":
258263
if len(sys.argv) < 2:
259264
print("Usage: make-readme.py README.raw.md > README.md")

0 commit comments

Comments
Β (0)