Skip to content

Commit

Permalink
add support for callable default
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron committed Apr 18, 2024
1 parent 95684ef commit a8fc3c9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
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
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 == ""

0 comments on commit a8fc3c9

Please sign in to comment.