Skip to content

Commit

Permalink
fix: handle empty prefix in subconfig correctly (#13)
Browse files Browse the repository at this point in the history
This change ensures that sub-configurations with an empty prefix don't end up looking for environment variables with a double underscore as separator.
  • Loading branch information
P403n1x87 authored Jul 11, 2022
1 parent 2a39ed2 commit bc3c483
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion envier/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def __init__(self, source=None, parent=None):
) + _normalized(
self.__prefix__
) # type: str
if self._full_prefix:
if self._full_prefix and not self._full_prefix.endswith("_"):
self._full_prefix += "_"

self.spec = self.__class__
Expand Down
9 changes: 6 additions & 3 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,14 @@ class Config(Env):
assert Config().foo == 24


def test_env_nested_config(monkeypatch):
monkeypatch.setenv("MYAPP_SERVICE_PORT", "8080")
@pytest.mark.parametrize(
"prefix,var", [("", "MYAPP_PORT"), ("service", "MYAPP_SERVICE_PORT")]
)
def test_env_nested_config(monkeypatch, prefix, var):
monkeypatch.setenv(var, "8080")

class ServiceConfig(Env):
__prefix__ = "service"
__prefix__ = prefix

host = Env.var(str, "host", default="localhost")
port = Env.var(int, "port", default=3000)
Expand Down

0 comments on commit bc3c483

Please sign in to comment.