Skip to content

Commit

Permalink
[stable-2.5] Use newer is_sequence function instead of MutableSequence (
Browse files Browse the repository at this point in the history
ansible#44331)

* Use newer is_sequence function instead of MutableSequence. Fixes ansible#44327

* Add changelog for ansible#44331

* Update changelog fragment to describe the fix in more detail
(cherry picked from commit 2bf6507)

Co-authored-by: Matt Martz <matt@sivel.net>
  • Loading branch information
sivel committed Aug 21, 2018
1 parent b23b1c2 commit 624dd23
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/flatten-better-type-check.yml
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions lib/ansible/plugins/filter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
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
Expand Down Expand Up @@ -474,7 +474,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:
Expand Down
2 changes: 2 additions & 0 deletions test/integration/targets/filters/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@
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:
that:
- 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]

0 comments on commit 624dd23

Please sign in to comment.