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

add support for callable default #8

Merged
merged 2 commits into from
Apr 18, 2024
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
10 changes: 8 additions & 2 deletions ragdoll/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ def __init__(

@property
def _default(self) -> Any:
if callable(self._default_value):
value = self._default_value()
else:
value = self._default_value

if self._process_default_value:
return self.to_python(self._default_value)
return self._default_value
return self.to_python(value)

return value

@property
def name(self):
Expand Down
6 changes: 0 additions & 6 deletions ragdoll/utils.py

This file was deleted.

7 changes: 7 additions & 0 deletions tests/test_env/test_bool_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ class MyEnvSetting(EnvSetting):
FOO = BoolEnv(False)

assert MyEnvSetting.__dict__["FOO"].name == "FOO"


def test_bool_env_callable_default():
class MyEnvSetting(EnvSetting):
FOO = BoolEnv(lambda: False)

assert MyEnvSetting.FOO is False
7 changes: 7 additions & 0 deletions tests/test_env/test_int_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ def test_process_default_value_choices_error():

class MyEnvSetting(EnvSetting):
FOO = IntEnv("0", process_default_value=True, choices=[1])


def test_int_env_callable_default():
class MyEnvSetting(EnvSetting):
FOO = IntEnv(lambda: 300)

assert MyEnvSetting.FOO == 300
7 changes: 7 additions & 0 deletions tests/test_env/test_str_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ class MyEnvSetting(EnvSetting):
FOO = StrEnv("")

assert MyEnvSetting.__dict__["FOO"].name == "FOO"


def test_str_env_callable_default():
class MyEnvSetting(EnvSetting):
FOO = StrEnv(lambda: "")

assert MyEnvSetting.FOO == ""
Loading