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

[bug] FlaskDynaconf breaks default getter of flask config #521

Closed
shsmad opened this issue Feb 26, 2021 · 3 comments · Fixed by #522
Closed

[bug] FlaskDynaconf breaks default getter of flask config #521

shsmad opened this issue Feb 26, 2021 · 3 comments · Fixed by #522
Labels

Comments

@shsmad
Copy link

shsmad commented Feb 26, 2021

Describe the bug
In pure Flask config if you query config for nonexisting key you get KeyError exception.
If use FlaskDynaconf if you query config for nonexisting key you get value None.

Such differences break thirdparty libraries, which suppose that flask config will raise KeyError when queriing nonexisting key (for example, python-social-auth: https://github.com/python-social-auth/social-core/blob/master/social_core/strategy.py#L45 which goes to https://github.com/python-social-auth/social-app-flask/blob/master/social_flask/strategy.py#L19)

To Reproduce
Correct behavior:

from flask import Flask

app = Flask(__name__)
print("NON_EXISTING" in app.config)
print(app.config["NON_EXISTING"])

will give you

False
Traceback (most recent call last):
  File "dc_poc/1_no_dynaconf.py", line 6, in <module>
    print(app.config["NON_EXISTING"])
KeyError: 'NON_EXISTING'

Incorrect behavior:

from flask import Flask
from dynaconf import FlaskDynaconf

app = Flask(__name__)
FlaskDynaconf(app=app)
print("NON_EXISTING" in app.config)
print(app.config["NON_EXISTING"])

will give you

False
None
  1. Having the following folder structure
Project structure
.
├── 1_no_dynaconf.py
└── 2_dynaconf.py

0 directories, 2 files
  1. Having the following config files:
Config files No config files used at all
  1. Having the following app code:
Code

1_no_dynaconf.py

from flask import Flask

app = Flask(__name__)
print("NON_EXISTING" in app.config)
print(app.config["NON_EXISTING"])

2_dynaconf.py

from flask import Flask
from dynaconf import FlaskDynaconf

app = Flask(__name__)
FlaskDynaconf(app=app)
print("NON_EXISTING" in app.config)
print(app.config["NON_EXISTING"])
  1. Executing under the following environment
Execution

used Python 3.8.5, Flask 1.1.2, dynaconf 3.1.2

# other commands and details?
# virtualenv activation?

$ python 1_no_dynaconf.py
$ python 2_dynaconf.py

Expected behavior
Queriing non existing key via config[key] should raise KeyError, not returning None.

Environment (please complete the following information):

  • OS: Linux/Kubuntu20.04
  • Dynaconf Version 3.1.2
  • Flask 1.1.2

Additional context
Add any other context about the problem here.

@shsmad shsmad added the bug label Feb 26, 2021
@rochacbruno
Copy link
Member

rochacbruno commented Feb 26, 2021

Duplicate of #501 already merged in #502 and coming in the next release

@shsmad
Copy link
Author

shsmad commented Feb 26, 2021

Duplicate of #501 already merged in #502 and coming in the next release

Not quite duplicate. #501 and #502 are about in syntax, which is __contains__ for. But my case is accessing variable[key], so DynaconfConfig.__getitem__ is used, which calls DynaconfConfig.get which calls flask.config.Config.get with default value set to None. But accessing real flask.config.Config via [] does not uses default value.

@rochacbruno
Copy link
Member

@shsmad you are right, I just added #522 to fix this problem, the new release must come monday at morning. (I usually don't do releases on friday)

rochacbruno added a commit that referenced this issue Feb 26, 2021
…522)

* Fix #521 - FlaskDynaconf should raise KeyError for non existing keys

* Test coverage got dotted get
rochacbruno added a commit that referenced this issue Mar 1, 2021
Shortlog of commits since last release:

    Bruno Rocha (11):
          Release version 3.1.2
          Fix #445 casting on dottet get. (#446)
          Fix docs regarding --django argument on cli (#477)
          Fix #521 - FlaskDynaconf should raise KeyError for non existing keys (#522)
          Case insensitive envvar traversal (#524)
          Allow load_file to accept pathlib.Path (#525)
          Allow Title case lookup and validation. (#526)
          Fix #482 - formatter case insensitive (#527)
          Fix #449 - Django lazy templating Fix #449 (#528)
          Added a test to reproduce #492 (not able to reproduce) (#530)
          Fix #511 allow user to specify loaders argument to execute_loaders (#531)

    FrankBattaglia (1):
          Specify flask extension initializers by entry point object reference (#456)

    Ilito Torquato (3):
          fix merging hyperlink to fix  #454 (#458)
          Changed enabled_core_loaders elements to be upper case to fix #455 (#457)
          Fix doc secrets from vault #403 (#459)

    Marcelo Lino (1):
          Add __contains__ to Dynaconf (#502)

    Michal Odnous (1):
          Fix: Environment variables filtering #470 (#474)

    dependabot-preview[bot] (5):
          Bump mkdocs-material from 6.0.2 to 6.1.0 (#453)
          Bump mkdocs-git-revision-date-localized-plugin from 0.5.2 to 0.7.3 (#463)
          Bump mkdocs-material from 6.1.0 to 6.1.5 (#473)
          Bump mkdocs-versioning from 0.2.1 to 0.3.1 (#475)
          Bump mkdocs-material from 6.1.5 to 6.1.6 (#476)

    mirrorrim (1):
          Fix reading secret from Vault kv v2 (#483) (#487)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants