2023-08-09 18:20:12 +00:00
|
|
|
from ast import literal_eval
|
|
|
|
from collections import defaultdict
|
|
|
|
from typing import Union # py<3.10
|
2023-08-08 20:02:39 +00:00
|
|
|
|
2023-08-09 18:20:12 +00:00
|
|
|
from tqdm.utils import envwrap
|
2023-08-08 19:42:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_envwrap(monkeypatch):
|
2023-08-09 18:20:12 +00:00
|
|
|
"""Test @envwrap (basic)"""
|
|
|
|
monkeypatch.setenv('FUNC_A', "42")
|
|
|
|
monkeypatch.setenv('FUNC_TyPe_HiNt', "1337")
|
2023-08-08 20:02:39 +00:00
|
|
|
monkeypatch.setenv('FUNC_Unused', "x")
|
2023-08-08 19:42:14 +00:00
|
|
|
|
|
|
|
@envwrap("FUNC_")
|
|
|
|
def func(a=1, b=2, type_hint: int = None):
|
|
|
|
return a, b, type_hint
|
|
|
|
|
2023-08-09 18:20:12 +00:00
|
|
|
assert (42, 2, 1337) == func()
|
|
|
|
assert (99, 2, 1337) == func(a=99)
|
2023-08-08 19:42:14 +00:00
|
|
|
|
2023-08-08 20:02:39 +00:00
|
|
|
|
2023-08-09 18:20:12 +00:00
|
|
|
def test_envwrap_types(monkeypatch):
|
|
|
|
"""Test @envwrap(types)"""
|
|
|
|
monkeypatch.setenv('FUNC_notype', "3.14159")
|
2023-08-08 20:02:39 +00:00
|
|
|
|
2023-08-09 18:20:12 +00:00
|
|
|
@envwrap("FUNC_", types=defaultdict(lambda: literal_eval))
|
|
|
|
def func(notype=None):
|
|
|
|
return notype
|
2023-08-08 20:02:39 +00:00
|
|
|
|
2023-08-09 18:20:12 +00:00
|
|
|
assert 3.14159 == func()
|
2023-08-08 20:02:39 +00:00
|
|
|
|
2023-08-09 18:20:12 +00:00
|
|
|
monkeypatch.setenv('FUNC_number', "1")
|
|
|
|
monkeypatch.setenv('FUNC_string', "1")
|
2023-08-08 19:42:14 +00:00
|
|
|
|
2023-08-09 18:20:12 +00:00
|
|
|
@envwrap("FUNC_", types={'number': int})
|
|
|
|
def nofallback(number=None, string=None):
|
|
|
|
return number, string
|
2023-08-08 19:42:14 +00:00
|
|
|
|
2023-08-09 18:20:12 +00:00
|
|
|
assert 1, "1" == nofallback()
|
|
|
|
|
|
|
|
|
|
|
|
def test_envwrap_annotations(monkeypatch):
|
|
|
|
"""Test @envwrap with typehints"""
|
|
|
|
monkeypatch.setenv('FUNC_number', "1.1")
|
|
|
|
monkeypatch.setenv('FUNC_string', "1.1")
|
|
|
|
|
|
|
|
@envwrap("FUNC_")
|
|
|
|
def annotated(number: Union[int, float] = None, string: int = None):
|
|
|
|
return number, string
|
|
|
|
|
|
|
|
assert 1.1, "1.1" == annotated()
|