Skip to content

Commit e4a0153

Browse files
committed
feat: support vars_only to keep parents of an attribute for helpers.jsobj() (#110)
1 parent 7d4a36f commit e4a0153

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

tests/test_helpers.py

+13
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ def test_jsobj():
8484
obj = jsobj(a, b, c=3)
8585
assert obj == {"a": 1, "b": 2, "c": 3}
8686

87+
x = lambda: None
88+
x.b = 2
89+
obj = jsobj(a, x.b)
90+
assert obj == {"a": 1, "b": 2}
91+
obj1 = jsobj(a, x.b, vars_only=False)
92+
assert obj1 == {"a": 1, "x.b": 2}
93+
94+
vars_only = 3
95+
obj2 = jsobj(vars_only, x.b)
96+
assert obj2 == {"vars_only": 3, "b": 2}
97+
obj3 = jsobj(vars_only, x.b, vars_only=False)
98+
assert obj3 == {"vars_only": 3, "x.b": 2}
99+
87100

88101
def test_register_to_function():
89102
@register

varname/helpers.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def __repr__(self) -> str:
153153
)
154154

155155

156-
def jsobj(*args: Any, **kwargs: Any) -> Dict[str, Any]:
156+
def jsobj(*args: Any, vars_only: bool = True, **kwargs: Any) -> Dict[str, Any]:
157157
"""A wrapper to create a JavaScript-like object
158158
159159
When an argument is passed as positional argument, the name of the variable
@@ -172,11 +172,12 @@ def jsobj(*args: Any, **kwargs: Any) -> Dict[str, Any]:
172172
Args:
173173
*args: The positional arguments
174174
**kwargs: The keyword arguments
175+
vars_only: Whether to only include variables in the output
175176
176177
Returns:
177178
A dict-like object
178179
"""
179-
argnames: Tuple[str, ...] = argname("args") # type: ignore
180+
argnames: Tuple[str, ...] = argname("args", vars_only=vars_only) # type: ignore
180181
out = dict(zip(argnames, args))
181182
out.update(kwargs)
182183
return out

0 commit comments

Comments
 (0)