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] filter_strategy config not working #760

Closed
rdettai opened this issue Jun 23, 2022 · 2 comments
Closed

[bug] filter_strategy config not working #760

rdettai opened this issue Jun 23, 2022 · 2 comments
Labels
bug HIGH High Priority

Comments

@rdettai
Copy link

rdettai commented Jun 23, 2022

Describe the bug
It seems that the filter_strategy config (which by the way is misspelled int the docs) is not taken into account when used in the Dynaconf constructor.

To Reproduce
Steps to reproduce the behavior:

  1. Having the following folder structure
    Just a plain python script.

  2. Having the following config files:
    No config file, just using env variables

  3. Having the following app code:

Code

/test/test.py

import dynaconf

class CustomFilter:
    def __call__(self, data):
        print("this is never called")
        return {
            k: v
            for k, v in data.items()
            if k.startswith("PREFIX")
        }


if __name__ == "__main__":
    dc = dynaconf.Dynaconf(
        envvar_prefix=False,
        filter_strategy=CustomFilter(),
    )
    print(dc.as_dict())
  1. Executing under the following environment
Execution
PREFIX_VAR="HELLO" OTHER_VAR="WORLD" python test.py

Expected behavior
CustomFilter should be called ("this is never called" should be displayed) and only the PREFIX_VAR should be in dict, not OTHER_VAR

Environment (please complete the following information):

  • OS: Linux version 5.10.60.1-microsoft-standard-WSL2
  • Dynaconf Version 3.1.9
  • Framework: None

Context

I was looking for a way to filter out empty environment variables.

@rdettai rdettai added the bug label Jun 23, 2022
@rdettai rdettai changed the title [bug] [bug] filter_strategy config not working Jun 23, 2022
@rochacbruno rochacbruno added the HIGH High Priority label Jul 8, 2022
@limeiralucas
Copy link
Contributor

limeiralucas commented Jul 13, 2022

I would like to fix this issue. Adding a check and execution for the filter_strategy in the env_loader seems to fix it.

diff --git a/dynaconf/loaders/env_loader.py b/dynaconf/loaders/env_loader.py
index 660bd27..3e61396 100644
--- a/dynaconf/loaders/env_loader.py
+++ b/dynaconf/loaders/env_loader.py
@@ -76,6 +76,9 @@ def load_from_env(
         }
         # Update the settings space based on gathered data from environment.
         if data:
+            filter_strategy = obj.get("FILTER_STRATEGY")
+            if filter_strategy:
+                data = filter_strategy(data)
             obj.update(data, loader_identifier=identifier)
 

Please, let me know if that's seems okay. If so, should I open a PR with the fix? 😃 (That's my first contribution to the project)

limeiralucas added a commit to limeiralucas/dynaconf that referenced this issue Jul 13, 2022
@rochacbruno
Copy link
Member

Thanks @limeiralucas

rochacbruno added a commit that referenced this issue Sep 22, 2022
Shortlog of commits since last release:

    Amadou Crookes (1):
          envars.md typo fix (#786)

    Bruno Rocha (19):
          Release version 3.1.9
          Bump dev version to 3.1.10
          Update badges
          demo repo will be replaced by a video tutorial soon
          Fix CI
          New data key casing must adapt to existing key casing (#795)
          Add test and docs about includes (#796)
          Removed vendor_src folder (#798)
          Replacing rochacbruno/ with dynaconf/ (#800)
          Fix codecov (#801)
          Parse negative numbers from envvar Fix #799 and Fix #585 (#802)
          Fix get command with Django (#804)
          Add a functional test runner (#805)
          Test runner docs and styling (#806)
          Allow merge_unique on lists when merge_enabled=True (#810)
          Rebind current env when forced for Pytest Fix #728 (#809)
          AUTO_CAST can be enabled on instance (#811)
          Ensure pyminify is on release script
          Add missing tomllib to monify script

    Gaurav Talreja (1):
          Fix #807 Use client.auth.approle.login instead of client.auth_approle (#808)

    Jitendra Yejare (1):
          Fix #768 of kv property depreciation from client object (#769)

    Joren Retel (2):
          Feature/detect casting comb token from converters (#784)
          Adding documentation and example to makefile. (#791)

    João Gustavo A. Amorim (1):
          Add pyupgrade hook (#759)

    Kian-Meng Ang (1):
          Fix typos (#788)

    Lucas Limeira (1):
          Using filter_strategy in env_loader to fix #760 (#767)

    Nicholas Nadeau, Ph.D., P.Eng (1):
          fix: typo (#766)

    Oleksii Baranov (2):
          Bump codecov action version (#775)
          Fix cli init command for flask (#705) (#774)

    Pedro de Medeiros (1):
          documentation fixes (#771)

    The Gitter Badger (1):
          Add a Gitter chat badge to README.md (#776)

    Théo Melo (1):
          Fixing a typo on the readme file (#763)

    Vicente Marçal (1):
          docs(pt-br): Docs Translation to brazilian portugues. (#787)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug HIGH High Priority
Projects
None yet
Development

No branches or pull requests

3 participants