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
pull/44386/merge
Matt Martz 6 years ago committed by GitHub
parent dcc4a38f98
commit 2bf6507c44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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)

@ -34,7 +34,7 @@ import time
import uuid import uuid
import yaml import yaml
from collections import MutableMapping, MutableSequence from collections import MutableMapping
import datetime import datetime
from functools import partial from functools import partial
from random import Random, SystemRandom, shuffle, random from random import Random, SystemRandom, shuffle, random
@ -462,7 +462,7 @@ def flatten(mylist, levels=None):
if element in (None, 'None', 'null'): if element in (None, 'None', 'null'):
# ignore undefined items # ignore undefined items
break break
elif isinstance(element, MutableSequence): elif is_sequence(element):
if levels is None: if levels is None:
ret.extend(flatten(element)) ret.extend(flatten(element))
elif levels >= 1: elif levels >= 1:

@ -176,6 +176,7 @@
flat_full: '{{orig_list|flatten}}' flat_full: '{{orig_list|flatten}}'
flat_one: '{{orig_list|flatten(levels=1)}}' flat_one: '{{orig_list|flatten(levels=1)}}'
flat_two: '{{orig_list|flatten(levels=2)}}' flat_two: '{{orig_list|flatten(levels=2)}}'
flat_tuples: '{{ [1,3] | zip([2,4]) | list | flatten }}'
- name: Verify flatten filter works as expected - name: Verify flatten filter works as expected
assert: assert:
@ -183,6 +184,7 @@
- flat_full == [1, 2, 3, 4, 5, 6, 7] - flat_full == [1, 2, 3, 4, 5, 6, 7]
- flat_one == [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_two == [1, 2, 3, 4, [5], 6, 7]
- flat_tuples == [1, 2, 3, 4]
vars: vars:
orig_list: [1, 2, [3, [4, [5]], 6], 7] orig_list: [1, 2, [3, [4, [5]], 6], 7]

Loading…
Cancel
Save