Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add validator argument #12

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions envier/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(
type, # type: Union[object, Type[T]]
name, # type: str
parser=None, # type: Optional[Callable[[str], T]]
validator=None, # type: Optional[Callable[[T], None]]
map=None, # type: Optional[MapType]
default=NoDefault, # type: Union[T, NoDefaultType]
deprecations=None, # type: Optional[List[DeprecationInfo]]
Expand All @@ -63,11 +64,12 @@ def __init__(
self.type = type
self.name = name
self.parser = parser
self.validator = validator
self.map = map
self.default = default
self.deprecations = deprecations

def __call__(self, env, prefix):
def _retrieve(self, env, prefix):
# type: (Env, str) -> T
source = env.source

Expand Down Expand Up @@ -142,6 +144,15 @@ def __call__(self, env, prefix):

return self.type(raw) # type: ignore[call-arg,operator]

def __call__(self, env, prefix):
# type: (Env, str) -> T
value = self._retrieve(env, prefix)

if self.validator is not None:
self.validator(value)

return value


class DerivedVariable(Generic[T]):
def __init__(self, type, derivation):
Expand Down Expand Up @@ -234,25 +245,27 @@ def var(
type, # type: Type[T]
name, # type: str
parser=None, # type: Optional[Callable[[str], T]]
validator=None, # type: Optional[Callable[[T], None]]
map=None, # type: Optional[MapType]
default=NoDefault, # type: Union[T, NoDefaultType]
deprecations=None, # type: Optional[List[DeprecationInfo]]
):
# type: (...) -> EnvVariable[T]
return EnvVariable(type, name, parser, map, default, deprecations)
return EnvVariable(type, name, parser, validator, map, default, deprecations)

@classmethod
def v(
cls,
type, # type: Union[object, Type[T]]
name, # type: str
parser=None, # type: Optional[Callable[[str], T]]
validator=None, # type: Optional[Callable[[T], None]]
map=None, # type: Optional[MapType]
default=NoDefault, # type: Union[T, NoDefaultType]
deprecations=None, # type: Optional[List[DeprecationInfo]]
):
# type: (...) -> EnvVariable[T]
return EnvVariable(type, name, parser, map, default, deprecations)
return EnvVariable(type, name, parser, validator, map, default, deprecations)

@classmethod
def der(cls, type, derivation):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,29 @@ class DictConfig(Env):
foo = Env.der(Optional[int], lambda _: value)

assert DictConfig().foo is value


@pytest.mark.parametrize(
"value,exc",
[
(0, None),
(512, None),
(-1, ValueError),
(513, ValueError),
],
)
def test_env_validator(monkeypatch, value, exc):
monkeypatch.setenv("FOO", str(value))

class Config(Env):
def validate(value):
if not (0 <= value <= 512):
raise ValueError("Value must be between 0 and 512")

foo = Env.var(int, "FOO", validator=validate)

if exc is not None:
with pytest.raises(exc):
Config()
else:
assert Config().foo == value