jay
jay

Reputation: 665

Python @property in Flask configs?

I'm currently learning Flask and I just set up a config file I load into the app with:

app.config.from_object('myconfigmodule')

The config module has two classes in it, Config and DebugConfig and DebugConfig inherits Config. I'd like to use @property getters to get config variables rather than accessing them with app.config['myvar'] because it makes for cleaner code. I set this up and app.config does not see the properties but I can still access the config class members with app.config['myvar']

This is the error I get when I start my app:

Traceback (most recent call last):
File "runserver.py", line 3, in <module>
app.run(host=app.config['_APP_HOST'], debug=app.config.Debug)
AttributeError: 'Config' object has no attribute 'Debug'

In the config class the Debug property is as follows:

class Config (object):
    _APP_DEBUG = False

    @property
    def Debug (self):
        return self._APP_DEBUG

Am I doing something wrong here or does Flask just not like properties in configs for some reason? Thanks for any help!

Upvotes: 2

Views: 4740

Answers (1)

miku
miku

Reputation: 188034

Flask has it's own Config class (a dict subclass) and it will pick out the attributes of the object given to from_object, rather that using the given object as is, as can be seen in the source code:

# class Config(dict):
# ...
    for key in dir(obj):
        if key.isupper():
            self[key] = getattr(obj, key)

As you can see, it will only use uppercase attributes.

Here's an example by hand:

>>> from flask import config
>>> class X(object):
...     REGULAR = True
...     ignored = "not uppercase"
...     def __init__(self):
...         self.not_used = "because lowercase"
...         self.OK = True
...     
...     @property
...     def UPPER_PROP(self):
...         return True
...     
...     @property
...     def MIXED_case(self):
...         return "wont work"
... 
>>> x = X()
>>> c = config.Config(None)
>>> c.from_object(x)
>>> c
<Config {'REGULAR': True, 'OK': True, 'UPPER_PROP': True}>

That said, nothing will hold you back, if you want to implement something like a dot-dict'd subclass of flasks' Config. Whether the potential confusion caused by a non-standard approach outweighs the gains in code readability is something you can decide based on the scope of your project.

Upvotes: 3

Related Questions