varname 0.12.2__py3-none-any.whl → 0.13.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
varname/__init__.py CHANGED
@@ -13,4 +13,4 @@ from .utils import (
13
13
  )
14
14
  from .core import varname, nameof, will, argname
15
15
 
16
- __version__ = "0.12.2"
16
+ __version__ = "0.13.1"
varname/py.typed ADDED
@@ -0,0 +1 @@
1
+
varname/utils.py CHANGED
@@ -185,7 +185,10 @@ def lookfor_parent_assign(node: ast.AST, strict: bool = True) -> AssignType:
185
185
  return None
186
186
 
187
187
 
188
- def node_name(node: ast.AST) -> Union[str, Tuple[Union[str, Tuple], ...]]:
188
+ def node_name(
189
+ node: ast.AST,
190
+ subscript_slice: bool = False,
191
+ ) -> Union[str, Tuple[Union[str, Tuple], ...]]:
189
192
  """Get the node node name.
190
193
 
191
194
  Raises ImproperUseError when failed
@@ -193,15 +196,50 @@ def node_name(node: ast.AST) -> Union[str, Tuple[Union[str, Tuple], ...]]:
193
196
  if isinstance(node, ast.Name):
194
197
  return node.id
195
198
  if isinstance(node, ast.Attribute):
196
- return node.attr
197
- if isinstance(node, (ast.List, ast.Tuple)):
199
+ return f"{node_name(node.value)}.{node.attr}"
200
+ if isinstance(node, ast.Constant):
201
+ return repr(node.value)
202
+ if isinstance(node, (ast.List, ast.Tuple)) and not subscript_slice:
198
203
  return tuple(node_name(elem) for elem in node.elts)
204
+ if isinstance(node, ast.List):
205
+ return f"[{', '.join(node_name(elem) for elem in node.elts)}]" # type: ignore
206
+ if isinstance(node, ast.Tuple):
207
+ if len(node.elts) == 1:
208
+ return f"({node_name(node.elts[0])},)"
209
+ return f"({', '.join(node_name(elem) for elem in node.elts)})" # type: ignore
199
210
  if isinstance(node, ast.Starred):
200
211
  return f"*{node_name(node.value)}"
212
+ if isinstance(node, ast.Slice):
213
+ return (
214
+ f"{node_name(node.lower)}:{node_name(node.upper)}:{node_name(node.step)}"
215
+ if node.lower is not None
216
+ and node.upper is not None
217
+ and node.step is not None
218
+ else f"{node_name(node.lower)}:{node_name(node.upper)}"
219
+ if node.lower is not None and node.upper is not None
220
+ else f"{node_name(node.lower)}:"
221
+ if node.lower is not None
222
+ else f":{node_name(node.upper)}"
223
+ if node.upper is not None
224
+ else ":"
225
+ )
226
+
227
+ name = type(node).__name__
228
+ if isinstance(node, ast.Subscript):
229
+ try:
230
+ return f"{node_name(node.value)}[{node_name(node.slice, True)}]"
231
+ except ImproperUseError:
232
+ name = f"{node_name(node.value)}[{type(node.slice).__name__}]"
201
233
 
202
234
  raise ImproperUseError(
203
- f"Can only get name of a variable or attribute, "
204
- f"not {ast.dump(node)}"
235
+ f"Node {name!r} detected, but only following nodes are supported: \n"
236
+ " - ast.Name (e.g. x)\n"
237
+ " - ast.Attribute (e.g. x.y, x be other supported nodes)\n"
238
+ " - ast.Constant (e.g. 1, 'a')\n"
239
+ " - ast.List (e.g. [x, y, z])\n"
240
+ " - ast.Tuple (e.g. (x, y, z))\n"
241
+ " - ast.Starred (e.g. *x)\n"
242
+ " - ast.Subscript with slice of the above nodes (e.g. x[y])"
205
243
  )
206
244
 
207
245
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: varname
3
- Version: 0.12.2
3
+ Version: 0.13.1
4
4
  Summary: Dark magics about variable names in python.
5
5
  Home-page: https://github.com/pwwang/python-varname
6
6
  License: MIT
@@ -98,6 +98,12 @@ Thanks goes to these awesome people/projects:
98
98
  <br /><sub><b>@LawsOfSympathy</b></sub>
99
99
  </a>
100
100
  </td>
101
+ <td align="center" style="min-width: 75px">
102
+ <a href="https://github.com/elliotgunton">
103
+ <img src="https://avatars.githubusercontent.com/u/17798778?s=400&v=4" width="50px;" alt=""/>
104
+ <br /><sub><b>@elliotgunton</b></sub>
105
+ </a>
106
+ </td>
101
107
  </tr>
102
108
  </table>
103
109
 
@@ -222,7 +228,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
222
228
  func = function2() # func == 'func'
223
229
 
224
230
  a = lambda: 0
225
- a.b = function() # a.b == 'b'
231
+ a.b = function() # a.b == 'a.b'
226
232
  ```
227
233
 
228
234
  ### The decorator way to register `__varname__` to functions/classes
@@ -0,0 +1,10 @@
1
+ varname/__init__.py,sha256=Y3H1NDja1WelUPmgRVUikMOYdK31QRIL0MhfOH9hDRg,369
2
+ varname/core.py,sha256=nntOVpiavXP0EXijTNF6xIe4mxvz_FRpBjrL9z_JHJ0,19311
3
+ varname/helpers.py,sha256=ho5X2t3PiSYDexwpA4TO_l68VWh3HER7bsUKzcbiCNM,9242
4
+ varname/ignore.py,sha256=-VC3oqag44y2UlAw0ErYKHotx616qJL3Sjb_5y7Tw1c,14400
5
+ varname/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
6
+ varname/utils.py,sha256=ByHbUjbdMlHHckW4y77Jr_DHhkSmk8TabTy3INDJWsY,20971
7
+ varname-0.13.1.dist-info/LICENSE,sha256=3bS8O2tMbBPz8rWmZBAOzkHQjcK-b7KwFHyyghEZ-Ak,1063
8
+ varname-0.13.1.dist-info/METADATA,sha256=TFZcuBRdqmLps6fUjWOJbCoLWULzeg8PxSH_x2NcjxE,12845
9
+ varname-0.13.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
10
+ varname-0.13.1.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.8.1
2
+ Generator: poetry-core 1.9.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,9 +0,0 @@
1
- varname/__init__.py,sha256=_--tA4u7XQB0HB4dT3j_f52p-DL2rgwT2TyaOsvo0B0,369
2
- varname/core.py,sha256=nntOVpiavXP0EXijTNF6xIe4mxvz_FRpBjrL9z_JHJ0,19311
3
- varname/helpers.py,sha256=ho5X2t3PiSYDexwpA4TO_l68VWh3HER7bsUKzcbiCNM,9242
4
- varname/ignore.py,sha256=-VC3oqag44y2UlAw0ErYKHotx616qJL3Sjb_5y7Tw1c,14400
5
- varname/utils.py,sha256=f0dDRYfOeebdDvEwVSQVXNpHC1sB3-1qNE8mICg1IcE,19296
6
- varname-0.12.2.dist-info/LICENSE,sha256=3bS8O2tMbBPz8rWmZBAOzkHQjcK-b7KwFHyyghEZ-Ak,1063
7
- varname-0.12.2.dist-info/METADATA,sha256=JFiq7RcC9LJF2NZ__AQ5GYaIXMP9gTIyF2LoTv1Hsqg,12578
8
- varname-0.12.2.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
9
- varname-0.12.2.dist-info/RECORD,,