8000 Merge pull request #153 from b0o/ini-keyerror · HBNetwork/python-decouple@dc6d2e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc6d2e6

Browse files
Merge pull request #153 from b0o/ini-keyerror
Raise KeyError when key is not found in ini repositories
2 parents 9cc2f7e + d596515 commit dc6d2e6

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

decouple.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212

1313
if PYVERSION >= (3, 0, 0):
14-
from configparser import ConfigParser
14+
from configparser import ConfigParser, NoOptionError
1515
text_type = str
1616
else:
17-
from ConfigParser import SafeConfigParser as ConfigParser
17+
from ConfigParser import SafeConfigParser as ConfigParser, NoOptionError
1818
text_type = unicode
1919

2020
if PYVERSION >= (3, 2, 0):
@@ -134,7 +134,10 @@ def __contains__(self, key):
134134
self.parser.has_option(self.SECTION, key))
135135

136136
def __getitem__(self, key):
137-
return self.parser.get(self.SECTION, key)
137+
try:
138+
return self.parser.get(self.SECTION, key)
139+
except NoOptionError:
140+
raise KeyError(key)
138141

139142

140143
class RepositoryEnv(RepositoryEmpty):

tests/test_env.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,7 @@ def test_env_with_quote(config):
136136
assert '"Y"' == config('KeyHasTwoDoubleQuote')
137137
assert '''"Y\'''' == config('KeyHasMixedQuotesAsData1')
138138
assert '''\'Y"''' == config('KeyHasMixedQuotesAsData2')
139+
140+
def test_env_repo_keyerror(config):
141+
with pytest.raises(KeyError):
142+
config.repository['UndefinedKey']

tests/test_ini.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,8 @@ def test_ini_undefined_but_present_in_os_environ(config):
121121

122122
def test_ini_empty_string_means_false(config):
123123
assert False is config('KeyEmpty', cast=bool)
124+
125+
126+
def test_ini_repo_keyerror(config):
127+
with pytest.raises(KeyError):
128+
config.repository['UndefinedKey']

tests/test_secrets.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding: utf-8
22
import os
3+
import pytest
34

45
from decouple import Config, RepositorySecret
56

@@ -28,3 +29,10 @@ def test_secret_overriden_by_environ():
2829
os.environ['db_user'] = 'hi'
2930
assert 'hi' == config('db_user')
3031
del os.environ['db_user']
32+
33+
def test_secret_repo_keyerror():
34+
path = os.path.join(os.path.dirname(__file__), 'secrets')
35+
repo = RepositorySecret(path)
36+
37+
with pytest.raises(KeyError):
38+
repo['UndefinedKey']

0 commit comments

Comments
 (0)
0