File tree Expand file tree Collapse file tree 4 files changed +23
-3
lines changed
Expand file tree Collapse file tree 4 files changed +23
-3
lines changed Original file line number Diff line number Diff line change 1111
1212
1313if PYVERSION >= (3 , 0 , 0 ):
14- from configparser import ConfigParser
14+ from configparser import ConfigParser , NoOptionError
1515 text_type = str
1616else :
17- from ConfigParser import SafeConfigParser as ConfigParser
17+ from ConfigParser import SafeConfigParser as ConfigParser , NoOptionError
1818 text_type = unicode
1919
2020if 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
140143class RepositoryEnv (RepositoryEmpty ):
Original file line number Diff line number Diff 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' ]
Original file line number Diff line number Diff line change @@ -121,3 +121,8 @@ def test_ini_undefined_but_present_in_os_environ(config):
121121
122122def 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' ]
Original file line number Diff line number Diff line change 11# coding: utf-8
22import os
3+ import pytest
34
45from 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' ]
You can’t perform that action at this time.
0 commit comments