Skip to content

Commit ec5b182

Browse files
committed
Add Flask.config_class feature
1 parent 88b74b3 commit ec5b182

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

flask/app.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ def _set_request_globals_class(self, value):
175175
_set_request_globals_class)
176176
del _get_request_globals_class, _set_request_globals_class
177177

178+
#: The class that is used for the ``config`` attribute of this app.
179+
#: Defaults to :class:`~flask.Config`.
180+
#:
181+
#: Example use cases for a custom class:
182+
#:
183+
#: 1. Default values for certain config options.
184+
#: 2. Access to config values through attributes in addition to keys.
185+
config_class = Config
186+
178187
#: The debug flag. Set this to `True` to enable debugging of the
179188
#: application. In debug mode the debugger will kick in when an unhandled
180189
#: exception occurs and the integrated server will automatically reload
@@ -609,7 +618,7 @@ def make_config(self, instance_relative=False):
609618
root_path = self.root_path
610619
if instance_relative:
611620
root_path = self.instance_path
612-
return Config(root_path, self.default_config)
621+
return self.config_class(root_path, self.default_config)
613622

614623
def auto_find_instance_path(self):
615624
"""Tries to locate the instance path if it was not provided to the

flask/testsuite/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ def assert_in(self, x, y):
157157
def assert_not_in(self, x, y):
158158
self.assertNotIn(x, y)
159159

160+
def assert_isinstance(self, obj, cls):
161+
self.assertIsInstance(obj, cls)
162+
160163
if sys.version_info[:2] == (2, 6):
161164
def assertIn(self, x, y):
162165
assert x in y, "%r unexpectedly not in %r" % (x, y)

flask/testsuite/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def test_config_missing(self):
9999
self.assert_true(0, 'expected config')
100100
self.assert_false(app.config.from_pyfile('missing.cfg', silent=True))
101101

102+
def test_custom_config_class(self):
103+
class Config(flask.Config):
104+
pass
105+
class Flask(flask.Flask):
106+
config_class = Config
107+
app = Flask(__name__)
108+
self.assert_isinstance(app.config, Config)
109+
app.config.from_object(__name__)
110+
self.common_object_test(app)
111+
102112
def test_session_lifetime(self):
103113
app = flask.Flask(__name__)
104114
app.config['PERMANENT_SESSION_LIFETIME'] = 42

0 commit comments

Comments
 (0)