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-table-output-mixed-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "``table`` output",
"description": "Fix ``ValueError`` when ``--output table`` renders a list that contains both a scalar and an object, which a ``--query`` multi-select list such as ``[InstanceId, State]`` produces",
"type": "bugfix"
}
21 changes: 19 additions & 2 deletions awscli/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,27 @@ def _build_table(self, title, current, indent_level=0):
if title is not None:
self.table.new_section(title, indent_level=indent_level)
if isinstance(current, list):
if isinstance(current[0], dict):
if all(isinstance(el, dict) for el in current):
self._build_sub_table_from_list(current, indent_level, title)
else:
for item in current:
# The elements don't all share a shape, so a dict can't be
# rendered as a row alongside them. Give it a section of
# its own, and start a new one for whatever follows it.
needs_new_section = False
for i, item in enumerate(current):
if isinstance(item, dict):
if i > 0:
self.table.new_section(
title, indent_level=indent_level
)
self._build_sub_table_from_dict(item, indent_level)
needs_new_section = True
continue
if needs_new_section:
self.table.new_section(
title, indent_level=indent_level
)
needs_new_section = False
if self._scalar_type(item):
self.table.add_row([item])
elif all(self._scalar_type(el) for el in item):
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/output/test_table_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,41 @@
"""


# A --query multi-select list such as '[InstanceId, State]' returns a list
# holding both a scalar and an object.
MIXED_SCALAR_FIRST = ["i-123", {"Code": 16, "Name": "running"}]

MIXED_SCALAR_FIRST_TABLE = """\
---------------------
| OperationName |
+-------------------+
| i-123 |
+-------------------+
| OperationName |
+-------+-----------+
| Code | Name |
+-------+-----------+
| 16 | running |
+-------+-----------+
"""

MIXED_DICT_FIRST = [{"Code": 16, "Name": "running"}, "i-123"]

MIXED_DICT_FIRST_TABLE = """\
---------------------
| OperationName |
+-------+-----------+
| Code | Name |
+-------+-----------+
| 16 | running |
+-------+-----------+
| OperationName |
+-------------------+
| i-123 |
+-------------------+
"""


class Object(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
Expand Down Expand Up @@ -425,3 +460,11 @@ def test_jmespath_filtered_response(self):
def test_jmespath_filtered_dict_response(self):
self.assert_data_renders_to(data=JMESPATH_FILTERED_RESPONSE_DICT,
table=JMESPATH_FILTERED_RESPONSE_DICT_TABLE)

def test_mixed_list_with_leading_scalar(self):
self.assert_data_renders_to(data=MIXED_SCALAR_FIRST,
table=MIXED_SCALAR_FIRST_TABLE)

def test_mixed_list_with_leading_dict(self):
self.assert_data_renders_to(data=MIXED_DICT_FIRST,
table=MIXED_DICT_FIRST_TABLE)