Skip to content

Commit a1efeae

Browse files
committed
Stop normalizing lowercase prefixes
The initial inspiration for this feature was the way Viper (a popular Go package for configuration) handled environment variables. Sadly, our use cases for prefixes differ enough where modifying the prefix isn't a viable option. Viper can get away with it since its prefix is only used to generate its default environment variable names, whereas ours are used for both generating implicit environment variable names and configuring explicit ones. Unless we disallow lowercase environment variables (a bold option considering no other packages do this), keeping the prefix and passed name as-is is the best option.
1 parent 7028faf commit a1efeae

File tree

5 files changed

+7
-15
lines changed

5 files changed

+7
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
2020

2121
- Excluded nonupper attributes from default_settings.
2222
- Restricted the settings module to the {module}:{class} formatting.
23+
- Stopped normalizing lowercase prefixes.
2324

2425
## [0.1.3] - Unreleased
2526

class_settings/env.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from . import parsers
1010
from .options import Options
11-
from .utils import missing, normalize_prefix
11+
from .utils import missing
1212

1313

1414
class Env:
@@ -42,7 +42,7 @@ def __call__(self, name=None, *, prefix=missing, default=missing, optional=False
4242
if name is None or optional:
4343
kwargs = {"name": name, "prefix": prefix, "default": default}
4444
return DeferredEnv(self, kwargs=kwargs, optional=optional)
45-
prefix = normalize_prefix(
45+
prefix = (
4646
prefix
4747
if prefix is not missing
4848
else self._prefix
@@ -77,7 +77,6 @@ def read_env(file=None):
7777

7878
@contextlib.contextmanager
7979
def prefixed(self, prefix):
80-
prefix = normalize_prefix(prefix)
8180
old_prefix = self._prefix
8281
if not old_prefix or prefix is None:
8382
self._prefix = prefix

class_settings/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Options:
55
defaults = {
66
"default_settings": global_settings,
77
"inject_settings": False,
8-
"env_prefix": "django",
8+
"env_prefix": "DJANGO_",
99
}
1010

1111
def __init__(self, meta):

class_settings/utils.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,3 @@ def __bool__(self):
44

55

66
missing = Missing()
7-
8-
9-
def normalize_prefix(prefix):
10-
if prefix is None:
11-
return prefix
12-
if prefix.islower() and not prefix.endswith("_"):
13-
prefix += "_"
14-
return prefix.upper()

tests/test_env.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TestSettings(Settings):
4040
)
4141
def test_env_prefix(self, env):
4242
class TestSettings(Settings):
43-
SECRET_KEY = env("SECRET_KEY", prefix="django")
43+
SECRET_KEY = env("SECRET_KEY", prefix="DJANGO_")
4444
CUSTOM = env("CUSTOM", prefix="CUSTOM")
4545

4646
settings = TestSettings()
@@ -79,7 +79,7 @@ class TestSettings(Settings):
7979
)
8080
def test_env_prefixed(self, env):
8181
class TestSettings(Settings):
82-
with env.prefixed("django"):
82+
with env.prefixed("DJANGO_"):
8383
SECRET_KEY = env("SECRET_KEY")
8484
with env.prefixed("CUSTOM"):
8585
CUSTOM = env("CUSTOM")
@@ -211,7 +211,7 @@ class TestSettings(Settings):
211211
CUSTOM = env("CUSTOM")
212212

213213
class Meta:
214-
env_prefix = "custom"
214+
env_prefix = "CUSTOM_"
215215

216216
settings = TestSettings()
217217

0 commit comments

Comments
 (0)