Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify what app.config.from_object accepts #1457

Closed
tony opened this issue May 11, 2015 · 11 comments
Closed

Clarify what app.config.from_object accepts #1457

tony opened this issue May 11, 2015 · 11 comments
Labels

Comments

@tony
Copy link
Contributor

tony commented May 11, 2015

$ python
Python 2.7.9 (default, Apr 29 2015, 18:34:06) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>> conf = {"DEBUG": True}
>>> app = flask.Flask(__name__)                                                
>>> app.config.from_object(conf)
>>> app.debug
False

Expected behavior would be to accept to setting in the flask application object.

>>> flask.__version__
'0.10.1'
>>> conf = {"SECRET_KEY": "honey"}                                                                                                                                      
>>> app = flask.Flask(__name__)                                                                                                                                         
>>> app.config.from_object(conf)                                                                                                                                        
>>> app.secret_key
@untitaker
Copy link
Contributor

from_object doesn't read from dictionary keys, instead it traverses dir(obj). This is useful if you pass a module object with global vars in it.

This is something that should be clarified in the docs though.

@untitaker untitaker added the docs label May 11, 2015
@untitaker untitaker changed the title Flask 0.10 from_object does not read DEBUG or SECRET_KEY Clarify what app.config.from_object accepts May 11, 2015
@tony
Copy link
Contributor Author

tony commented May 11, 2015

@untitaker :

Thanks for getting back so quickly

I happen to be working inside of a larger example that should be turning the dict into an attribute dict. Let me look closer to verify that functionality is working as expected.

edited

@untitaker
Copy link
Contributor

Could you post a link to that example? AFAIK from_object was never supposed to work that way.

@tony
Copy link
Contributor Author

tony commented May 11, 2015

Yeah, it may be a bigger oversight on part. Checking it.

@tony
Copy link
Contributor Author

tony commented May 11, 2015

What I was doing is totally wrong then.

https://github.com/tony/cookiecutter-flask-pythonic/blob/master/%7B%7Bcookiecutter.repo_name%7D%7D/%7B%7Bcookiecutter.repo_name%7D%7D/%7B%7Bcookiecutter.repo_name%7D%7D.py#L32

app.config.from_object(dict(self.config)).

If I wanted to go that approach, I think my only option to write my own config parser.

@untitaker
Copy link
Contributor

According to the code above, self.config already has the wished attributes? So calling dict on it is the problem, nothing else.

@tony
Copy link
Contributor Author

tony commented May 11, 2015

self.config is this:

class AttributeDict(dict):

    def __getattr__(self, name):
        if not name in self:
            raise AttributeError("'{}' object has no attribute '{}'".format(
                self.__class__,
                name,
            ))

        return self[name]

@tony
Copy link
Contributor Author

tony commented May 11, 2015

Removed the dict part, still not getting anything because of dir's results.

Perhaps if I gave the AttributeDict a __dir__ dunder?

@untitaker
Copy link
Contributor

You could also just set your attributes on your AttributeDict instance instead of defining __getattr__, provided your config is readonly.

tony added a commit to tony/cookiecutter-flask-pythonic that referenced this issue May 11, 2015
@jasonbrazeal
Copy link
Contributor

looking at this for #pycon2016 sprints

@davidism
Copy link
Member

davidism commented Jun 2, 2016

#1870

@davidism davidism closed this as completed Jun 2, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants