Skip to content

Commit

Permalink
add support for callable default (#8)
Browse files Browse the repository at this point in the history
* add support for callable default

* remove unused file

---------

Co-authored-by: Aaron <[email protected]>
  • Loading branch information
rabbit-aaron and Aaron authored Apr 18, 2024
1 parent 95684ef commit 97a2673
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 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
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 == ""

0 comments on commit 97a2673

Please sign in to comment.