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

Sign up to get free protection for your applications and to get access to all the features.
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.0"
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.0
4
4
  Summary: Dark magics about variable names in python.
5
5
  Home-page: https://github.com/pwwang/python-varname
6
6
  License: MIT
@@ -222,7 +222,7 @@ Special thanks to [@HanyuuLu][2] to give up the name `varname` in pypi for this
222
222
  func = function2() # func == 'func'
223
223
 
224
224
  a = lambda: 0
225
- a.b = function() # a.b == 'b'
225
+ a.b = function() # a.b == 'a.b'
226
226
  ```
227
227
 
228
228
  ### The decorator way to register `__varname__` to functions/classes
@@ -0,0 +1,9 @@
1
+ varname/__init__.py,sha256=UTuY7fG_AbLwpbhBnup66zRlEvl0WnG6exf5AvOyyp0,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=ByHbUjbdMlHHckW4y77Jr_DHhkSmk8TabTy3INDJWsY,20971
6
+ varname-0.13.0.dist-info/LICENSE,sha256=3bS8O2tMbBPz8rWmZBAOzkHQjcK-b7KwFHyyghEZ-Ak,1063
7
+ varname-0.13.0.dist-info/METADATA,sha256=6YjfqWLcmjJ9TXHN5yLlAn0DHrBg3escnFyzqLgKdWM,12580
8
+ varname-0.13.0.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
9
+ varname-0.13.0.dist-info/RECORD,,
@@ -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,,