Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-text-output-mixed-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "``text`` output",
"description": "Fix ``AttributeError`` when ``--output text`` renders a list that contains both a scalar and an object, which a ``--query`` multi-select list such as ``[InstanceId, State]`` produces",
"type": "bugfix"
}
5 changes: 5 additions & 0 deletions awscli/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def _format_dict(scalar_keys, item, identifier, stream):
def _all_scalar_keys(list_of_dicts):
keys_seen = set()
for item_dict in list_of_dicts:
if not isinstance(item_dict, dict):
# The list is only required to contain *at least* one dict,
# so skip over any elements that aren't dicts. They are
# rendered on their own by _format_text.
continue
for key, value in item_dict.items():
if not isinstance(value, (dict, list)):
keys_seen.add(key)
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,22 @@ def test_deeply_nested_with_identifier(self):
'FOO\th\n'
)

def test_dicts_mixed_with_scalars(self):
# A --query multi-select list such as '[InstanceId, State]'
# produces a list holding both a scalar and a dict.
self.assert_text_renders_to(
['i-123', dict(Code=16, Name='running')],
'i-123\n'
'16\trunning\n'
)

def test_dicts_mixed_with_lists(self):
self.assert_text_renders_to(
[['a', 'b'], dict(Code=16, Name='running')],
'a\tb\n'
'16\trunning\n'
)


if __name__ == '__main__':
unittest.main()