Skip to content

Commit

Permalink
utils.envwrap: fix unused var, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Aug 8, 2023
1 parent 6feef44 commit 1411a0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
25 changes: 25 additions & 0 deletions tests/tests_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from tqdm.utils import envwrap


def test_envwrap(monkeypatch):
"""Test envwrap overrides"""
env_a = 42
env_c = 1337
monkeypatch.setenv('FUNC_A', str(env_a))
monkeypatch.setenv('FUNC_TyPe_HiNt', str(env_c))

@envwrap("FUNC_")
def func(a=1, b=2, type_hint: int = None):
return a, b, type_hint

assert (env_a, 2, 1337) == func(), "expected env override"
assert (99, 2, 1337) == func(a=99), "expected manual override"

env_liTeral = 3.14159
monkeypatch.setenv('FUNC_liTeral', str(env_liTeral))

@envwrap("FUNC_", literal_eval=True, case_sensitive=True)
def another_func(liTeral=1):
return liTeral

assert env_liTeral == another_func()
7 changes: 4 additions & 3 deletions tqdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def envwrap(prefix, case_sensitive=False, literal_eval=False, is_method=False):
is_method : bool, optional
Whether to use `functools.partialmethod`. If (default: False) use `functools.partial`.
Examples:
Examples
--------
```
$ cat foo.py
from tqdm.utils import envwrap
Expand All @@ -70,10 +71,10 @@ def test(a=1, b=2, c=3):
part = partialmethod if is_method else partial

def wrap(func):
params = signature(func).parameters
if literal_eval:
return part(func, **{k: safe_eval(v) for k, v in overrides.items()})
return part(func, **{k: safe_eval(v) for k, v in overrides.items() if k in params})
# use `func` signature to infer env override `type` (fallback to `str`)
params = signature(func).parameters
for k in overrides:
param = params.get(k, None)
if param is not None:
Expand Down

0 comments on commit 1411a0f

Please sign in to comment.