Skip to content

Commit 5256347

Browse files
committed
feat: support frame to allow wrapping for helpers.jsobj() (#111)
1 parent 14b5c41 commit 5256347

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

tests/test_helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ def test_jsobj():
9898
assert obj3 == {"vars_only": 3, "x.b": 2}
9999

100100

101+
def test_jsobj_wrapping():
102+
def myjsobj(*args, **kwargs):
103+
return jsobj(*args, vars_only=False, frame=2, **kwargs)
104+
105+
x = lambda: None
106+
x.b = 2
107+
obj = myjsobj(x.b)
108+
assert obj == {"x.b": 2}
109+
110+
101111
def test_register_to_function():
102112
@register
103113
def func():

varname/helpers.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ def __repr__(self) -> str:
153153
)
154154

155155

156-
def jsobj(*args: Any, vars_only: bool = True, **kwargs: Any) -> Dict[str, Any]:
156+
def jsobj(
157+
*args: Any,
158+
vars_only: bool = True,
159+
frame: int = 1,
160+
**kwargs: Any,
161+
) -> Dict[str, Any]:
157162
"""A wrapper to create a JavaScript-like object
158163
159164
When an argument is passed as positional argument, the name of the variable
@@ -171,13 +176,19 @@ def jsobj(*args: Any, vars_only: bool = True, **kwargs: Any) -> Dict[str, Any]:
171176
172177
Args:
173178
*args: The positional arguments
174-
**kwargs: The keyword arguments
175179
vars_only: Whether to only include variables in the output
180+
frame: The call stack index. You can understand this as the number of
181+
wrappers around this function - 1.
182+
**kwargs: The keyword arguments
176183
177184
Returns:
178185
A dict-like object
179186
"""
180-
argnames: Tuple[str, ...] = argname("args", vars_only=vars_only) # type: ignore
187+
argnames: Tuple[str, ...] = argname(
188+
"args",
189+
vars_only=vars_only,
190+
frame=frame,
191+
) # type: ignore
181192
out = dict(zip(argnames, args))
182193
out.update(kwargs)
183194
return out

0 commit comments

Comments
 (0)