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

Data filter falsey #52499

merged 8 commits into from Apr 24, 2019


Copy link

@github-abcde github-abcde commented Apr 11, 2019

What does this PR do?

This adds a function filter_falsey to The function can be used to filter values from a list or dict that evaluate to false (but are not bool(False)). Optionally, a level of recursion can be set to process nested dicts/lists. Also optionally, a list of types to not filter can be passed in ignore_types.

What issues does this PR fix or reference?

None that I know of.

Previous Behavior

The functionality does not exist.

New Behavior

We are now able to filter falsey values from dicts or lists.
This is especially handy when having to process a lot of kwargs that are explicitly specified in the function argument list, which default to None, but need not be passed on if they are None.
For example:

def some_function(foo=None, bar=None, baz=None):
    new_kwargs = {}
    if foo is not None:
        new_kwargs.update('Foo': foo)
    if bar is not None:
        new_kwargs.update('Bar': bar)
    if baz is not None:
        new_kwargs.update('Baz': baz)

will become

def some_function(foo=None, bar=None, baz=None):
    new_kwargs ={
        'Foo': foo,
        'Bar': bar,
        'Baz': baz,

Which is a typical use-case for most Boto calls.

Tests written?


Commits signed with GPG?


salt/utils/ Show resolved Hide resolved
Copy link
Contributor Author

@twangboy I've implemented the changes you requested. Is there anything else I can do for this PR?

@twangboy twangboy merged commit f11b41f into saltstack:develop Apr 24, 2019
Copy link

@github-abcde Looks good. Thanks!

@github-abcde github-abcde deleted the data_filter_falsey branch April 25, 2019 07:43
@waynew waynew added this to PR needs port to master in PRs to port to master Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
PRs to port to master
PR needs port to master

Successfully merging this pull request may close these issues.

None yet

3 participants