From 2bf6507c44e05af1db6bec0612014f4b97e070f8 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Tue, 21 Aug 2018 10:42:11 -0500 Subject: [PATCH] Use newer is_sequence function instead of MutableSequence (#44331) * Use newer is_sequence function instead of MutableSequence. Fixes #44327 * Add changelog for #44331 * Update changelog fragment to describe the fix in more detail --- changelogs/fragments/flatten-better-type-check.yml | 2 ++ lib/ansible/plugins/filter/core.py | 4 ++-- test/integration/targets/filters/tasks/main.yml | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/flatten-better-type-check.yml diff --git a/changelogs/fragments/flatten-better-type-check.yml b/changelogs/fragments/flatten-better-type-check.yml new file mode 100644 index 0000000000..405988a8d1 --- /dev/null +++ b/changelogs/fragments/flatten-better-type-check.yml @@ -0,0 +1,2 @@ +bugfixes: +- flatten filter - use better method of type checking allowing flattening of mutable and non-mutable sequences (https://github.com/ansible/ansible/pull/44331) diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 0b9366ba92..aa1e04a174 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -34,7 +34,7 @@ import time import uuid import yaml -from collections import MutableMapping, MutableSequence +from collections import MutableMapping import datetime from functools import partial from random import Random, SystemRandom, shuffle, random @@ -462,7 +462,7 @@ def flatten(mylist, levels=None): if element in (None, 'None', 'null'): # ignore undefined items break - elif isinstance(element, MutableSequence): + elif is_sequence(element): if levels is None: ret.extend(flatten(element)) elif levels >= 1: diff --git a/test/integration/targets/filters/tasks/main.yml b/test/integration/targets/filters/tasks/main.yml index baaa4fa784..69162c5132 100644 --- a/test/integration/targets/filters/tasks/main.yml +++ b/test/integration/targets/filters/tasks/main.yml @@ -176,6 +176,7 @@ flat_full: '{{orig_list|flatten}}' flat_one: '{{orig_list|flatten(levels=1)}}' flat_two: '{{orig_list|flatten(levels=2)}}' + flat_tuples: '{{ [1,3] | zip([2,4]) | list | flatten }}' - name: Verify flatten filter works as expected assert: @@ -183,6 +184,7 @@ - flat_full == [1, 2, 3, 4, 5, 6, 7] - flat_one == [1, 2, 3, [4, [5]], 6, 7] - flat_two == [1, 2, 3, 4, [5], 6, 7] + - flat_tuples == [1, 2, 3, 4] vars: orig_list: [1, 2, [3, [4, [5]], 6], 7]