From 3465b06191e172772644bfbd16b811c52ad75525 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:54:17 -0600 Subject: [PATCH] Make repositories read environment keys consistently --- decouple.py | 6 ++++++ tests/test_env.py | 6 ++++++ tests/test_ini.py | 6 ++++++ tests/test_secrets.py | 9 +++++++++ 4 files changed, 27 insertions(+) diff --git a/decouple.py b/decouple.py index 9873fc9..7bdeab0 100644 --- a/decouple.py +++ b/decouple.py @@ -134,6 +134,8 @@ def __contains__(self, key): self.parser.has_option(self.SECTION, key)) def __getitem__(self, key): + if key in os.environ: + return os.environ[key] try: return self.parser.get(self.SECTION, key) except NoOptionError: @@ -163,6 +165,8 @@ def __contains__(self, key): return key in os.environ or key in self.data def __getitem__(self, key): + if key in os.environ: + return os.environ[key] return self.data[key] @@ -185,6 +189,8 @@ def __contains__(self, key): return key in os.environ or key in self.data def __getitem__(self, key): + if key in os.environ: + return os.environ[key] return self.data[key] diff --git a/tests/test_env.py b/tests/test_env.py index a91c95c..2d326c5 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -137,6 +137,12 @@ def test_env_with_quote(config): assert '''"Y\'''' == config('KeyHasMixedQuotesAsData1') assert '''\'Y"''' == config('KeyHasMixedQuotesAsData2') +def test_env_repo_reads_os_environ(config): + os.environ['KeyOnlyEnviron'] = 'This' + assert 'This' == config.repository['KeyOnlyEnviron'] + del os.environ['KeyOnlyEnviron'] + + def test_env_repo_keyerror(config): with pytest.raises(KeyError): config.repository['UndefinedKey'] diff --git a/tests/test_ini.py b/tests/test_ini.py index f610078..d2457eb 100644 --- a/tests/test_ini.py +++ b/tests/test_ini.py @@ -123,6 +123,12 @@ def test_ini_empty_string_means_false(config): assert False is config('KeyEmpty', cast=bool) +def test_ini_repo_reads_os_environ(config): + os.environ['KeyOnlyEnviron'] = 'This' + assert 'This' == config.repository['KeyOnlyEnviron'] + del os.environ['KeyOnlyEnviron'] + + def test_ini_repo_keyerror(config): with pytest.raises(KeyError): config.repository['UndefinedKey'] diff --git a/tests/test_secrets.py b/tests/test_secrets.py index 7d6185b..e37c3f4 100644 --- a/tests/test_secrets.py +++ b/tests/test_secrets.py @@ -30,6 +30,15 @@ def test_secret_overriden_by_environ(): assert 'hi' == config('db_user') del os.environ['db_user'] +def test_secret_repo_reads_os_environ(): + path = os.path.join(os.path.dirname(__file__), 'secrets') + repo = RepositorySecret(path) + + os.environ['KeyOnlyEnviron'] = 'SOMETHING' + assert 'SOMETHING' == repo['KeyOnlyEnviron'] + del os.environ['KeyOnlyEnviron'] + + def test_secret_repo_keyerror(): path = os.path.join(os.path.dirname(__file__), 'secrets') repo = RepositorySecret(path)